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: ...
随机推荐
- SqlServer function 函数
SqlServer的数据库Tsql还是很强大,以此来纪念下表值函数的语法吧. -- ============================================= -- Author: & ...
- WPF binding<一> Data Binding在WPF中的地位
在代码中看到 <Image Source="{Binding ElementName=LBoxImages, Path=SelectedItem.Source}" /> ...
- 一、URL和URLConnection
一.简述: 在Java网络编程中,我们最常听到的一个单词是URL.URL标识了一个资源,并可以通过URL来获取这个资源.我们不知道资源具体是什么,也不需要关心怎么获取.你只需要拿到一个URL,你就可以 ...
- No.5一步步学习vuejs之事件监听和组件
一监听事件 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码. <div id="demo1"> <button v-on ...
- 初步理解impress.js
1.认识impress.js impress.js 采用 CSS3 与 JavaScript 语言完成的一个可供开发者使用的表现层框架(演示工具). 现在普通开发者可以利用 impress.js 自己 ...
- .net项目常用的库
1.单元测试框架 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 2.Entity Framework 框架
- JavaEE之JavaWeb核心之Servlet
Servlet简介: Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的 ...
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
/// <summary> ///參考: http://james.newtonking.com/json/help/index.html# /// 塗聚文(Geovin Du) 2014 ...
- JavaScript中实现DI的原理(二)
JavaScript中实现DI的原理 在JavaScript中实现DI,看起来难,实际上原理很简单,它的核心技术是Function对象的toString().我们都知道,对一个函数对象执行toStri ...
- 利用 Task\Query 实现定位 , FeatureLayer 的属性查询
放纵了几天,又有了学习的动力.今天实现了利用对 FeatureLayer 进行属性查询在地图上进行跳转. 一.我下载了一幅浙江省的县界面地图,存在NAME字段,利用Name就能进行查询了: var n ...