HDU 6370 Werewolf 【并查集】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6370
Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2680 Accepted Submission(s): 806
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"}
题意概括:
读题意前要清空一下记忆,这里的狼人杀和实际的狼人杀规则不太一样。
每人按顺序说出一个人的角色(不能说自己的)
1、村民一定说真话
2、狼人有可能说假话。
求:
一定是村民的数量 和 一定是狼人的数量。
解题思路:
模拟了一下,感觉并不会有村民,因为没有方案可以证明出某个人一定是村民,因为狼人有“可能”说谎。
但我们可以确认哪些一定是狼人,即存在环,环的最后是 狼人边,其余都是村民边,那么被狼人边指的一定是狼人。
而用村民边指向这个狼人的角色也一定是狼人。
那么DFS,村民边建正向边,狼人边建反向边。
遍历狼人边,判断是否存在上述的环,如果存在再继续拓展。
大佬的证明:https://blog.csdn.net/weixin_39453270/article/details/81515570
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+; int N, M;
int w[MAXN];
vector<int>fa[MAXN], son[MAXN];
bool vis[MAXN];
int ans[MAXN], cnt; void add1(int a, int b)
{
fa[a].push_back(b);
} bool dfs(int a, int b)
{
bool flag = true;
for(int i = ; i < fa[a].size(); i++){
if(fa[a][i] == b) return false;
flag = dfs(fa[a][i], b);
if(!flag) return false;
}
return flag;
} void dfs2(int x)
{
if(!vis[x]) return;
vis[x] = false;
for(int i = ; i < fa[x].size(); i++){
if(vis[fa[x][i]]){
cnt++;
dfs2(fa[x][i]);
}
}
} int main()
{
//freopen("6370in.txt", "r", stdin);
int T_case, id, tot = ;
char ty[];
scanf("%d", &T_case);
while(T_case--){
cnt = ;
tot = ;
memset(vis, , sizeof(vis));
scanf("%d", &N);
for(int i = ; i <= N; i++){ fa[i].clear();son[i].clear();} for(int i = ; i <= N; i++){
scanf("%d %s", &id, &ty);
if(ty[] == 'w'){
w[++tot] = i;
son[i].push_back(id);
}
else{
add1(id, i);
}
}
for(int i = ; i <= tot; i++){
if(!vis[w[i]]) continue;
for(int k = ; k < son[w[i]].size();k++){
if(!vis[son[w[i]][k]]) continue;
if(!dfs(w[i], son[w[i]][k])){
//puts("zjy");
cnt++;
dfs2(son[w[i]][k]);
}
}
} printf("0 %d\n", cnt);
}
}
HDU 6370 Werewolf 【并查集】的更多相关文章
- HDU 6370 dfs+并查集
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- Bipartite Graph hdu 5313 bitset 并查集 二分图
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 ...
- hdu 3536【并查集】
hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市. Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...
- HDU 1829 分组并查集
题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...
- HDU 1198(并查集)
题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...
随机推荐
- [android] 天气app布局练习(三)
主要练习LinearLayout和layout_weight属性 <RelativeLayout xmlns:android="http://schemas.android.com/a ...
- Collatz 序列、逗号代码、字符图网格
1.collatz序列 编写一个名为 collatz()的函数,它有一个名为 number 的参数.如果参数是偶数, 那么 collatz()就打印出 number // 2,并返回该值.如果 num ...
- mysql存储过程优化
示例 WHILE s <> 1 DO select xxx; insert into xxx; END WHILE; 执行耗时27秒 优化点1: 添加事物 START TRANSACTIO ...
- Redis 常见命令
0. 5种数据类型 String(字符串) List(列表) Hash(字典) Set(集合) Sorted Set(有序集合) 1. String 字符串 set key value 设置key=v ...
- 配置zookeeper集群
创建3台服务,不同ip,相同端口 1.先安装jdk1.8 解压: tar -zxvf jdk-8u11-linux-x64.tar.gz 重新命名文件夹名字: mv jdk1..0_11/ jdk8 ...
- Bzoj1492: [NOI2007]货币兑换Cash(不单调的斜率优化)
题面 传送门 Sol 题目都说了 必然存在一种最优的买卖方案满足: 每次买进操作使用完所有的人民币: 每次卖出操作卖出所有的金券. 设\(f[i]\)表示第\(i\)天可以有的最大钱数 枚举\(j&l ...
- Java基础_day01
一.JRE和JDK的安装, JRE —— Java 运行环境 Java runtime environment 包含Java虚拟机和Java程序的核心类库. ...
- 1-4 Sass的基本特性-基础
[Sass]声明变量 定义变量的语法: 在有些编程语言中(如,JavaScript)声明变量都是使用关键词“var”开头,但是在 Sass 不使用这个关键词,而是使用大家都喜欢的美元符号“$”开头.我 ...
- 005hystrix.stream信息聚合Turbine
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Turbine和Spring Boot Starter Actuator依赖 <dependencies> <!--添 ...
- Codeforces(Round #93) 126 B. Password
B. Password time limit per test 2 seconds memory limit per test 256 megabytes Asterix, Obelix an ...