hdu6370 并查集+dfs
Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2363 Accepted Submission(s): 713
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.
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"}
2
2 werewolf
1 werewolf
Source
#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的更多相关文章
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck Accept:1733 Submit:14538 Time Limit: 3000 mSec Problem Description The Ce ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- POJ 3728 The merchant(并查集+DFS)
[题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- spring的开发
spring与web的整合 1. 整合的原理: Spring容器随着tomcat容器ServletContext的启动而启动,并且在初始化完成后放到整个应用都可以访问的范围. ApplicationC ...
- JS教程之实现加载图片时百分比进度
思路:思路其实很简单,ajax执行时,会生成一个event对象,其中会包含要加载的文件的大小和当前已经加载完成部分的大小,通过这两个值即可计算出百分比 事件介绍onprogress 当浏览器正在加载媒 ...
- day5-基础 函数
函数 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序 ...
- Linux下实现免密码登录
1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...
- 查看oracle固定目录下日志和trace文件大小脚本
python刚入门,在Oracle官网看到个小脚本,感觉挺有意思,经过测试切实可行. [oracle@ycr python]$ more 5.py import datetimeimport osim ...
- 团队的初体验与Scrum的初识
一. 队名及宣言 队名: the better for you 宣言: Change our lives with code 二. 队员及分工 a.承担软件工程的角色 姓名 学号 角色 蒋 婷 B20 ...
- IOS 集成友盟分享
#import <Foundation/Foundation.h> @interface UMSocialSinaHandler : NSObject +(void)openSSOWith ...
- 可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题
如果学不会也不要打我. 假设你会线段树 开始! --- 主席树也叫可持久化线段树 顾名思义,它能够保存线段树在每个时刻的版本. 什么叫每个时刻的版本?你可能对一棵普通线段树进行各种修改,这每种样子就是 ...
- 如何学好Spring
要学好Spring,首先要明确Spring是个什么东西,能帮我们做些什么事情,知道了这些然后做个简单的例子,这样就基本知道怎么使用Spring了.Spring核心是IoC容器,所以一定要透彻理解什么是 ...
- [luoguP4306][JSOI2010]连通数
\[Yeasion\] \[Nein\] 其实我很奇怪为什么我的正解和输出\(N \times N\)的效果是一样的.....嗯,大概是\(RP\)问题吧.... 嗯首先来看一下题目: 题目描述: 度 ...