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. oid和节点名称

    由于单篇文档最大字限制是40000个字符,不能将OID附上,因此写出我是如何得到这些OID的. 1.安装NET-SNMP yum install net-snmp yum install net-sn ...

  2. Django展示第一个网页

    展示一个网页需要三部分组成: urls.py -- 指定网址与对应的视图 views.py -- 创建试图以及指定对应的模板 template/*.html -- 对应的模板 一.urls.py ur ...

  3. Dart开发环境搭建

    一.SDK的安装与环境配置 1. 下载Dark SDK http://www.gekorm.com/dart-windows/ 2.  安装SDK 3.  配置环境变量(一般已经默认生成好了,这里可以 ...

  4. MySQL-07 日志管理

    学习目标 MySQL日志 二进制日志 错误日志 查询通用日志 慢查询日志 MySQL日志 MySQL日志分为四类,说明如下: 错误日志:记录MySQL服务的启动.运行或者停止时出现的问题. 查询日志: ...

  5. [ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not <type 'str'>.

    错误信息 [ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not &l ...

  6. ubuntu install zabbix

    ubuntu install zabbix reference1 reference2 some ERRORS raise during install process, may it help. z ...

  7. 深入Linux内核架构——进程管理和调度(上)

    如果系统只有一个处理器,那么给定时刻只有一个程序可以运行.在多处理器系统中,真正并行运行的进程数目取决于物理CPU的数目.内核和处理器建立了多任务的错觉,是通过以很短的间隔在系统运行的应用程序之间不停 ...

  8. 第二章:C++简单程序设计

    主要内容: 1.C++语言概述 2.基本数据类型和表达式 3.数据的输入与输出 4.算法的基本控制结构 5.自定义数据类型 1.数据类型default is double 2.自定义数据类型就是bui ...

  9. InnoDB体系架构总结(一)

    缓冲池:    是一块内存区域,通过内存的速度来弥补磁盘速度较慢对数据库性能的影响.在数据库中读取的页数据会存放到缓冲池中,下次再读取相同页的时候,会首先判断该页是否在缓冲池中.对于数据库中页的修改操 ...

  10. Could not resolve dependencies for project com.shadow:shlang:jar:1.0-SNAPSHOT:

    maven打包项目出现缺少jar包错误 如果是将本地引用的jar包放在了lib目录下并通过下面方式引入 解决方案为 <dependency>    <groupId>com.o ...