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. Windows远程桌面,出现身份验证错误,要求的函数不正确

    升级windows10 1803后,mstsc远程桌面出现 mstsc 远程桌面要求的函数不受支持,这可能是由于 CredSSP 加密 Oracle 修正.如图所示: 运行(win+r) gpedit ...

  2. ABAP和Java里关于DEFAULT(默认)机制的一些语言特性

    ABAP 740的新语法: 上图的代码相当于: DATA: ls_data LIKE LINE OF it_data. READ TABLE it_data INTO ls_data WITH KEY ...

  3. IOS 通讯录 (访问,添加,修改)

      如何访问用户的通讯录 在iOS中,有2个框架可以访问用户的通讯录 AddressBookUI.framework 提供了联系人列表界面.联系人详情界面.添加联系人界面等 一般用于选择联系人 Add ...

  4. 线程概念的外延 Threading Terminology-What Are Threads

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/AboutThrea ...

  5. HDU 6034 6038

    6034:给每个字母26进制的贪心.例如一个字母 c = 7*26^89 + 6*26^50.... 这个字符串有10^5长度.普通的大整数会超时,这里要稀疏这个大数一个pair<int,int ...

  6. Redux概念简述

    react可以写出一些比较简单的一些项目,但是只能写出很简单的一些项目,原因是什么呢,原因是react是一个非常轻量级的是视图层框架,打开官网可以看到大大的一行字,A JavaScript libra ...

  7. hadoop二次排序

    import java.io.DataInput; import java.io.DataOutput; import java.io.File; import java.io.IOException ...

  8. SQL批量添加,更新,删除

    SQL语句: 存储过程: 代码: 这里说的是关于SQL的批量操作数据. 需要准备: 三层框架:利用 动软代码生成器(可以快速生成增删改查) 生成三层 Model,BLL,Dal ①DbHelpeSQL ...

  9. VisualSVN Server更改SVN版本库存放路径的方法

    来源:http://blog.csdn.net/tcjy1000/article/details/42023849 最近也玩起了SVN软件版本管理,在本机上安装了VisualSVN Server+To ...

  10. C语言输出格式

    1 一般格式    printf(格式控制,输出表列)    例如:printf("i=%d,ch=%c\n",i,ch);    说明:    (1)“格式控制”是用双撇号括起来 ...