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 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- C#实现Stream与byte[]之间的转换实例教程
一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...
- SQL 创建自定义方法,匹配正则
C#代码 using System.Text.RegularExpressions; public class FunctionRegex { [Microsoft.SqlServer.Server. ...
- 【Leetcode】【Easy】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 学习笔记-java 多线程
背景说明: 多线程并发与管理,是java基础知识里的重点,本文根据<java核心技术第八版>中的多线程技术的学习,对知识点进行整理:这里只对基础知识点进行简单罗列,以达到对知识点有网状关联 ...
- May 16th 2017 Week 20th Tuesday
The most fearful enemy is not having a firm conviction. 最可怕的敌人,就是没有坚强的信念. A firm conviction or belie ...
- Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired,
Did not receive a reply. Possible causes include: the remote application did not send a reply, the m ...
- ACM-ICPC (10/11)
莫比乌斯 今年的多校比赛,莫比乌斯反演的题目经常出现,但是我们队对于这种题可以说是直接放掉,不是因为没学过,多少了解一些,但是也只是皮毛,导致根本就做不出来,其实想一想,其实次数多了,就可以看出原因了 ...
- POJ 3734 生成函数
题意:一排n长度的砖,有四种颜色,红色绿色是偶数,有少染色方式. 分析: 泰勒展开式: chx = (e^x+e^(-x))/2 = 1 + x^2/2! + x^4/4! + x^6/6! + .. ...
- HDU 2647 拓扑排序
题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...
- LA 4015 树形背包
题目链接:https://vjudge.net/contest/164840#problem/D 题意: 给一棵树,每条边上有一些权值,求 长度不超过 x ,最多能走多少个点: 分析: 考虑每一个节点 ...