Werewolf

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

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

解析    题解说的还是比较明白的,村名的数量一定是0  存在铁狼只有一种情况   (狼人边指向树中某个节点)

思路就是 找所有   除了根节点指出的边是狼人边   其他节点指出的边都是好人边组成的基环树   狼人边所指的那个子树大小就是铁狼的数量

比如 2,3,1,4,5,6 是满足条件的 基环树  铁狼的个数就是  sz[4]=4 (方框指向的是另一个联通块 所以不存在铁狼)

只有n条边  并查集  dfs 标记什么的 搞一搞 就过了

AC代码   写的太丑了

 #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
using namespace std;
typedef long long ll;
const ll maxn=1e5+,inf=1e18;
const ll mod=1e9+;
vector<pair<int,int> > g[maxn],gg[maxn];
int vis[maxn],sz[maxn];
int par[maxn];
int _find(int x)
{
return x==par[x]?x:par[x]=_find(par[x]);
}
void unio(int a,int b)
{
int ra=_find(a);
int rb=_find(b);
if(ra!=rb)
par[rb]=ra;
}
void dfs2(int x)//把 子树的大小求出来 并且把树归到一个集合
{
sz[x]=;vis[x]=;
for(int i=;i<gg[x].size();i++)
{
pair<int,int> it=gg[x][i];
if(vis[it.fi]==)
{
dfs2(it.fi);
}
unio(x,it.fi);
sz[x]+=sz[it.fi];
}
}
char s[];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int temp1,temp2;
for(int i=;i<=n;i++)
{
g[i].clear(),gg[i].clear();
par[i]=i;
vis[i]=sz[i]=;
}
for(int i=;i<=n;i++)
{
scanf("%d%s",&temp1,s);
if(s[]=='w')temp2=;
else temp2=;
g[i].pb(mp(temp1,temp2));
if(temp2==)
gg[temp1].pb(mp(i,temp2));
}
int ans=;
for(int i=;i<=n;i++)
{
if(vis[i]==&&gg[i].size()>)//没被访问过 且有子节点
dfs2(i);
}
for(int i=;i<=n;i++)
{
if(_find(g[i][].fi)==_find(i)&&g[i][].se==)//指出的是狼边 且狼边指向自己所在的树
ans+=sz[g[i][].fi];
}
printf("0 %d\n",ans);
}
} //4 v 1
//1 v 2
//1 v 3
//5 v 4
//6 v 5
//4 w 6
//4 w 7
//6 v 8
//10
//8
//4 v
//1 v
//1 v
//5 v
//6 v
//4 w
//4 w
//6 v

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

  1. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  2. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  4. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  5. 分珠(dfs+并查集)

    1140 分珠 时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...

  6. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

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

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  8. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  9. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

随机推荐

  1. -bash: mysql: command not found 之 MAC

    第一次尝试: ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql 提示:Operation not permitted 再次,加sudo附上管理员权限,依旧 ...

  2. Javaweb学习笔记9—过滤器

      今天来讲javaweb的第9阶段学习.   过滤器,我在本次的思维导图中将过滤器和监听器放在一起总结了,监听器比较简单就不单独写了.   老规矩,首先先用一张思维导图来展现今天的博客内容.     ...

  3. hdu 2192 MagicBuilding

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  4. Mysql数据库表的迁移和表的复制

    同一台服务器上的,数据库之间的表的迁移: create table db.tablename as select * from db2.tablename; 此sql使用于mysql,从一台服务器上的 ...

  5. Python——集合与字典练习

    集合与字典练习question1问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最 ...

  6. how to get many stars on Github?

    some key points: 1: make a beautiful README file2: use some GIF (google some tools to convert videos ...

  7. js将时间戳装换成日期格式

    13位时间戳改为yyyy-MM-dd HH-mm-ss 格式 目标时间戳:1516324500000 formatDateTime (unix) { // 转换时间戳 var date = new D ...

  8. error C2143: 语法错误 : 缺少“;”(在“&”的前面)

    报错: error C2143: 语法错误 : 缺少“;”(在“&”的前面) 代码: #include <iostream> ostream & << (ost ...

  9. 错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java.net.BindException: Address already in use: JVM_Bind

    在使用SpringMVC测试的时候, 遇到了这样一个问题, 说的是端口已经被使用了. 代理抛出异常错误: java.rmi.server.ExportException: Port already i ...

  10. Word转html并移植到web项目

    1.打开对应word文件 建议使用web视图查看文档 这样可以提前预览转转成html样式 2.如果有图片修改图片大小及格式 在web视图下,把图片调制适当大小,不然导出的html可能图片较小 3.点击 ...