任意门: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

Problem Description
"The Werewolves" is a popular card game among young people.In the basic game, there are 2 different groups: the werewolves and the villagers.

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.

 
Input
The first line of the input gives the number of test cases T.Then T test cases follow.

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"}

 
Output
For each test case,print the number of type-1 players and the number of type-2 players in one line, separated by white space.
 
Sample Input
1
2
2 werewolf
1 werewolf
 
Sample Output
0 0
 
Source

题意概括:

读题意前要清空一下记忆,这里的狼人杀和实际的狼人杀规则不太一样。

每人按顺序说出一个人的角色(不能说自己的)

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 【并查集】的更多相关文章

  1. HDU 6370 dfs+并查集

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  3. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  4. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  5. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. 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 ...

  7. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

  8. HDU 1829 分组并查集

    题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...

  9. HDU 1198(并查集)

    题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...

随机推荐

  1. [转]Using MVC 6 And AngularJS 2 With .NET Core

    本文转自:http://www.c-sharpcorner.com/article/using-mvc-6-and-angularjs-2-with-net-core/ CoreMVCAngular2 ...

  2. [转]ORA-00979: not a GROUP BY expression报错处理

    本文转自:http://blog.itpub.net/29154652/viewspace-772504/ 环境:Oracle Database 11gR2(11.2.0.2) on Linux  故 ...

  3. 100个实用的CSS技巧,以及遇见的问题和解决方案。

    前言 本篇文章将会持续更新,我会将我遇见的一些问题,和我看到的实用的CSS技巧记录下来,本篇文章会以图文混排的方式写下去.具体什么时候写完我也不清楚,反正我的目标是写100个.  本案例都是经本人实测 ...

  4. 001.開始使用ASP.NET Web API 2(一)

    原文鏈接:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...

  5. golang学习之mgo操作mongodb

    mgo是mongodb的golang驱动,测试代码: // mgotest project main.go package main import ( "fmt" "ti ...

  6. redis(7)LRU缓存

    一.LRU简介 LRU是Least Recently Used的缩写,即:最近最少使用. 它是内存管理中的一种页面置换算法,对于在内存中但是又不用的数据块,操作系统会根据哪些数据属于LRU而将其移除内 ...

  7. springboot aop使用介绍

    第一步:添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  8. jxls实现动态图表

    此文章是基于 jxls实现基于excel模板的报表 一. 制作excel动态图表模板 1. 安装 excel 2003 ,新建文件,命名为:runRecord.xls 2. 创建两个表格,分别命名为: ...

  9. Spring_Spring的特点

    一.非侵入式编程 Spring框架的API不会再业务逻辑上出现,即业务逻辑是POJO(Plain Ordinary Java Object).由于业务逻辑中没有Spring的API,所以业务逻辑可以从 ...

  10. tp3.2开启允许跨域

    在入口文件<?PHP下加上 header('Access-Control-Allow-Origin:*');header("Access-Control-Allow-Headers: ...