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. Android开发精品收藏贴

    各种下拉刷新效果: https://github.com/scwang90/SmartRefreshLayout

  2. eclipse搭建android开发环境详细步骤

    搭建android应用的开发环境,一套程序下来也是相当繁琐的,这里我整理下一整套详细流程: 1,下载JDK 去oracle官网下载最新版本的jdk,官网地址 http://www.oracle.com ...

  3. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  4. execve - 执行程序

    总览 (SYNOPSIS) #include <unistd.h> int execve (const char *filename, char *const argv [], char ...

  5. 数组排序 sort

    数组排序 this.dataShow = this.data.sort((a, b) => { return parseInt(a[this.innerOrderBy]) - parseInt( ...

  6. springboot测试的时候插入数据: error performing isolated work; SQL [n/a]; nested exception is org.hibernate...

    上网查了一下,说的是自增惹得麻烦!!在@GeneratedValue后面加上框框中的内容就OK拉!

  7. 打开或关闭CD_ROM

    实现效果: 知识运用: API函数 mciSendString //函数用来向媒体控制接口设备发送命令  声明如下 [DllImport("winmm.dll",EntryPoin ...

  8. Spring-02 Java配置实现IOC

    Java配置 Spring4推荐使用java配置实现IOC Spring boot也推荐采用java配置实现IOC 在实际项目中,一般采用注解配置业务bean,全局配置使用Java配置. Java配置 ...

  9. No-9.vi __终端中的编辑器

    vi —— 终端中的编辑器 01. vi 简介 1.1 学习 vi 的目的 在工作中,要对 服务器 上的文件进行 简单 的修改,可以使用 ssh 远程登录到服务器上,并且使用 vi 进行快速的编辑即可 ...

  10. strong&weak

    copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协议 ...