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. Ruby在Windows上安装

    Ruby在Windows下安装windows下的rails2.02环境搭建 ROR本地安装的技术含量比较高的 一.安装Ruby1.下载Ruby()安装包双击安装,安装过程中注意选中"Enab ...

  2. JS实现“双色球”

    需求: 双色球玩法简单介绍: “双色球”彩票投注区分为红色球号码区和蓝色球号码区.每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中选择:蓝色球号码从1--16中选择.程序要求 ...

  3. Win+Tab键实现自定义程序列表间的窗口切换

    程序是用AutoHotkey语言写的, 说明: 以自己使用频率的顺序在ExeList自定义的程序间切换 切换可以以所有窗口切换,也可以按程序组切换(比如在word窗口间切换) 程序组可以分别定义排除的 ...

  4. [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]

    场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...

  5. Locust性能测试1 脚本编写与运行

    按照官网的quickstart编写脚本并运行 1  编写脚本 2  locust -f  filepath 启动locust 3 浏览器打开localhost:8089,设置并发用户数和每秒启动用户数 ...

  6. C++学习之【使用位操作符求素数分析】

    先放普通代码: #include <iostream> using namespace std; void getPrime_1() { const int MAXN = 100; boo ...

  7. 【洛谷2290】[HNOI2004] 树的计数(Python+利用prufer序列结论求解)

    点此看题面 大致题意: 给定每个点的度数,让你求有多少种符合条件的无根树. \(prufer\)序列 这显然是一道利用\(prufer\)序列求解的裸题. 考虑到由\(prufer\)序列得到的结论: ...

  8. dijkstra 最小费用最大流

    前言:众所周知:spfa他死了 滑稽 dijkstra同样为最短路算法,为什么不能跑费用流qwq 好像是因为有负权边的缘故 但是如果我们如果使用某种玄学的将边权都拉回到正数的话 就可以跑了dijkst ...

  9. python—迭代器

    迭代器 这些可以直接作用于for循环的对象统称为可迭代对象:Iterable. 可以使用isinstance()判断一个对象是否是Iterable对象: >>> from colle ...

  10. html加载顺序以及影响页面二次渲染额的因素

    浏览器请求发往服务器以后,返回HTML页面,页面内容开始渲染,具体的执行顺序为: 1. 浏览器开始载入html代码,发现<head>标签内有一个<link>标签引用外部CSS文 ...