Werewolf

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2363    Accepted Submission(s): 713

Problem Description
"The Werewolves" is a popular card game among young people.In the basic game, there are 2 different groups: the werewolves and the villagers.

Each player will debate a player they think is a werewolf or not.

Their words are like "Player x is a werewolf." or "Player x is a villager.".

What we know is :

1. Villager won't lie.

2. Werewolf may lie.

Of cause we only consider those situations which obey the two rules above.

It is guaranteed that input data exist at least one situation which obey the two rules above.

Now we can judge every player into 3 types :

1. A player which can only be villager among all situations,

2. A player which can only be werewolf among all situations.

3. A player which can be villager among some situations, while can be werewolf in others situations.

You just need to print out the number of type-1 players and the number of type-2 players.

No player will talk about himself.

 
Input
The first line of the input gives the number of test cases T.Then T test cases follow.

The first line of each test case contains an integer N,indicating the number of players.

Then follows N lines,i-th line contains an integer x and a string S,indicating the i-th players tell you,"Player x is a S."

limits:

1≤T≤10

1≤N≤100,000

1≤x≤N

S∈ {"villager"."werewolf"}

 
Output
For each test case,print the number of type-1 players and the number of type-2 players in one line, separated by white space.
 
Sample Input
1
2
2 werewolf
1 werewolf
 
Sample Output
0 0
 

Source

思路:把狼边和人边分别记录,人边用并查集维护,最后判断狼边两端点是否在同一联通块,是则统计指向狼的人边的端点个数。
PS :统计过程是由dfs和建反向边实现的。
代码:
 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
int n,t;
int cnt,id;
struct P{int nx,to;}e[N];
struct PP{int fm,to;}g[N];
int head[N];
bool vis[N];
int fa[N];
char s[];
int sum[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void add(int u,int v)
{
e[id].to = u;
e[id].nx = head[v];
head[v] = id++;
}
void unio(int x,int y){
int xx,yy;
xx=find(x),yy=find(y);
if(xx!=yy) fa[xx]=yy;
} int dfs(int u)
{
vis[u]=;
for(int i=head[u];~i;i=e[i].nx){
int v=e[i].to;
if(vis[v]) sum[u]+=sum[v];
else sum[u]+=dfs(v);
}
return sum[u];
}
int main(){
ci(t);
while(t--)
{
ci(n);
cnt=id=;
memset(head,-, sizeof(head));
memset(vis,, sizeof(vis));
for(int i=;i<n;i++) fa[i]=i,sum[i]=;//初始化
for(int i=;i<n;i++){
int u;
scanf("%d %s",&u,s);
u--;
if(s[]=='w') g[cnt++]={i,u};
else{
unio(i,u);
add(i,u);//反向边
}
}
int ans=;
for(int i=;i<n;i++) if(!vis[i]) dfs(i);//统计每个点的子节点数
for(int i=;i<cnt;i++){
if(find(g[i].fm)==find(g[i].to)) ans+=sum[g[i].to];
}
printf("0 %d\n",ans);
}
return ;
}
 

hdu6370 并查集+dfs的更多相关文章

  1. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  2. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  3. POJ1291-并查集/dfs

    并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...

  4. F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

    这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...

  5. UVA208-Firetruck(并查集+dfs)

    Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Ce ...

  6. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  7. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  8. POJ 3728 The merchant(并查集+DFS)

    [题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...

  9. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

随机推荐

  1. Winform中 DataGridView控件中的 CheckBox 的值读出来 始终 为 False ,已解决

    private void DGV_DetailsViewer_CellContentClick(object sender, DataGridViewCellEventArgs e) { )) { D ...

  2. 一、选择与安装——Linux学习笔记

    A)为什么要用Linux做服务器? 网络理由: 1.linux本身是网络操作系统,支持所有TCP/IP协议,网络功能是内核中六大模块之一 2.linux和unix兼容,unix是早期的服务器霸主,现在 ...

  3. git相关操作(githug)

    Level 15  restructure 关卡描述 你添加了一些文件到你的仓库,但现在知道你的项目需要进行调整.创建一个新的文件夹命名为“src”,使用git将所有的".html" ...

  4. 如何证明CRM WebClient UI上的应用是有状态(Stateful)的

    随便找一个在CRM WebClient UI里会被频繁调用到的函数,在函数里创建一个隐式增强,打印当前的会话ID. 不关闭浏览器,在浏览器里不断做操作,可以观察到不管做任何操作,每次断点出发后,打印出 ...

  5. bootstrap table 主子表 局部数据刷新(刷新子表)

    1.主表中设置data-detail-view="true",启用主子表模式: <table class="table table-striped" wi ...

  6. 【SNOI2017】炸弹

    题目大意 在一条直线上有\(N\)个炸弹,每个炸弹的坐标是\(X_i\),爆炸半径是 \(R_i\), 当一个炸弹爆炸时,如果另一个炸弹所在位置\(X_j\)满足: $ X_i-R_i\leq X_j ...

  7. SpringBoot使用PageHelper进行分页

    因为SpringBoot就是为了实现没有配置文件,因此之前手动在Mybatis中配置的PageHelper现在需要重新配置,而且配置方式与之前的SSM框架中还是有点点区别.    首先需要在pom文件 ...

  8. 【转】Android Fragment 真正的完全解析(上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  9. Gulp 方法

    Gulp有5个基本方法:src.dest.task.run.watch Gulp.src()      gulp模块的src方法,用于产生数据流.它的参数表示索要处理的文件,一般有以下几种形式: js ...

  10. 【题解】洛谷P1002过河卒

    首先,一道入门DP 然而对于蒟蒻的我已经难到爆了好吗 第一点:动态转移方程 用DP的关键! 这题我们可以发现每一步的方案数由上面的那步加上左边的那步得到 所以自然而然的方程就出来了: f[i][k]= ...