HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数。
只能确定狼的身份,因为只能找到谁说了谎。但一个人是否是民,无法确定。
将人视作点,指认关系视作边,有狼边和民边两种边。
确定狼的方法只有两种:
1. 在一个仅由一条狼边组成的环中,狼边指向的那个点必定是狼。
2. 环外指认铁狼为民的也必定是狼。
所以用原图找环求情况1中的铁狼,反向建图找情况2中的狼。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
const int INF =0x3f3f3f3f;
struct Edge{
int v;bool w;
};
Edge G[maxn];
vector<Edge> rG[maxn];
int pre[maxn],dfn;
bool isw[maxn];
queue<int> Q;
int res; void init(int N){
res=;
memset(pre,,sizeof(pre));
memset(isw,,sizeof(isw));
for(int i=;i<=N;++i) rG[i].clear();
} void AddEdge(int u,int v,bool w){
G[u] = (Edge){v,w};
rG[v].push_back((Edge){u,w});
} void BFS()
{
while(!Q.empty()){
int u = Q.front();Q.pop();
for(int i=;i<rG[u].size();++i){
Edge &e =rG[u][i];
if(!e.w && !isw[e.v]){
Q.push(e.v);
isw[e.v] = true;
}
}
}
} void Tarjan(int u){
int v;bool w;
pre[u]=;
v= G[u].v;
w = G[u].w;
if(!pre[v])
Tarjan(v);
else if(pre[v]==){ //找到环
int cnt=, tar,t;
for(t=v;;t= G[t].v){
Edge &e = G[t];
if(e.w){
cnt++;
tar = G[t].v;
}
if(e.v==v) break;
}
if(cnt==){ //只有一个狼边才行
isw[tar] = true;
Q.push(tar);
}
}
pre[u]=;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("1009.out","w",stdout);
#endif
int T,N,u,v,tmp;
char op[];
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init(N);
for(int u=;u<=N;++u){
scanf("%d %s",&v,op);
if(op[]=='w') AddEdge(u,v,);
else AddEdge(u,v,);
} for(int i =;i<=N;++i){
if(!pre[i])
Tarjan(i);
}
BFS();
for(int i=;i<=N;++i)
if(isw[i]) res++;
printf("%d %d\n",,res);
}
return ;
}
HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)的更多相关文章
- HDU 6370 Werewolf 【并查集】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6370 Werewolf Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4930 Fighting the Landlords--2014 Multi-University Training Contest 6
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4930 Fighting the Landlords Time Limit: 2000/1000 MS ...
- HDU 6143 - Killer Names | 2017 Multi-University Training Contest 8
/* HDU 6143 - Killer Names [ DP ] | 2017 Multi-University Training Contest 8 题意: m个字母组成两个长为n的序列,两序列中 ...
- HDU 6074 - Phone Call | 2017 Multi-University Training Contest 4
看标程的代码这么短,看我的.... 难道是静态LCA模板太长了? /* HDU 6074 - Phone Call [ LCA,并查集 ] | 2017 Multi-University Traini ...
- HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4
/* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...
- HDU 6076 - Security Check | 2017 Multi-University Training Contest 4
/* HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4 题意: 给出两个检票序列 A[N], ...
- HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4
/* HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4 题意: 四个点的环,给定相邻两点距离, ...
- HDU 6078 - Wavel Sequence | 2017 Multi-University Training Contest 4
/* HDU 6078 - Wavel Sequence [ DP ] | 2017 Multi-University Training Contest 4 题意: 给定 a[N], b[M] 要求满 ...
- HDU 6070 - Dirt Ratio | 2017 Multi-University Training Contest 4
比赛时会错题意+不知道怎么线段树维护分数- - 思路来自题解 /* HDU 6070 - Dirt Ratio [ 二分,线段树 ] | 2017 Multi-University Training ...
随机推荐
- 第二百一十九节,jQuery EasyUI,DateTimeBox(日期时间输入框)组件
jQuery EasyUI,DateTimeBox(日期时间输入框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 DateTimeBox(日期时间输入框 ...
- php中数组中&的问题
1.代码: <?php $arr = array('one','two','three'); foreach ($arr as $value){ echo 'Value:'.$value.'&l ...
- IOS 分节表视图
分节表视图 简单的表视图仅仅有一个节,它实际上是分节表视图的一个特例.一个表能够有多个节,节也有头有脚,分节是加入索引和分组的前提. 索引的正确使用原则如所看到的: 1.索引标题不能与显示的标题全然一 ...
- IPOL图像处理分析经典在线(文献+源码)
网址: IPOL Journal · Image Processing On Line https://www.ipol.im/ 分类: 搜索: 下载文献和源码: NLM算法:IPOL Journal ...
- C# 计算每周和每月固定日期
最近发现写程序不是简单的实现功能,过程中偶尔伴随者一点小小的算法,比如排序算法,比如周期性的数据等等,发现算法不仅仅需要考虑全面,而且要计算简便.性能优良,而我远远没有达到要求! 一:周.月固定日期 ...
- 【BZOJ3160】万径人踪灭 Manacher+FFT
[BZOJ3160]万径人踪灭 Description Input Output Sample Input Sample Output HINT 题解:自己想出来1A,先撒花~(其实FFT部分挺裸的) ...
- iOS section 随tableview一起滚动
@interface YGSectionHeaderView : UIView @property NSUInteger section; @property (nonatomic, weak) UI ...
- Docker的初体验
这几天捣鼓了一下docker,是真的麻烦啊,网上资料比较杂乱,也很多英文,学起来挺多问题的 Docker下载 win10专业版:开启Hyper-V,下载并安装 docker-for-windows(但 ...
- 哈哈哈 迫于c#的语言特性java才加的注解
- 在Scrapy中使用IP池或用户代理更新版(python3)
middlewares.py # -*- coding: utf-8 -*- # 导入随机模块 import random # 导入有关IP池有关的模块 from scrapy.downloaderm ...