Werewolf HDU - 6370 (dfs )

10 篇文章 0 订阅
9 篇文章 0 订阅

Werewolf

 HDU - 6370 

"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≤101≤T≤10 

1≤N≤100,0001≤N≤100,000 

1≤x≤N1≤x≤N 

S∈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

题意:
有两类人:狼人:可能会说谎。村民:说的都是实话

现在每个人都会指明一个人的身份(不会自己指自己)

让你输出一定是村民的有多少人, 一定是狼人的有多少人

通过简单分析可以发现每个人都可能是狼人, 所以一定是村民的人数为0

现在我们要确定有多少个人一定是狼人

只有一种情况能确定某个人是狼人那就是:当他所说的话为真时所产生的后果与他的话互相矛盾

如A说B是村民, B说C是村民, C说B是狼人;

假设A是村民,那么B也是村民,C也是村民,这与C所说的话有矛盾,所以A不能是村民;

假设B是村民,那么C也是村民,又与C所说的话矛盾, 所以B也不能是村民;

假设C是村民那么不矛盾所以C可以(但不一定)是村民;

那么就能确定A,B一定是狼人了;

那么就按照这种矛盾把所有能确定的狼人标记上,就是答案了

 

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;

char s[100];
int n, x;
vector<int> v[N];
int p[N], in[N];

void dfs_(int k) { // 找出所有确定的狼人 
	in[k] = 1;
	for (int i = 0; i < v[k].size(); ++i) {
		dfs_(v[k][i]);
	}
}

void dfs(int k, int lang) { //假设k是村民, lang是狼  
	
	if (k == lang) { // 与假设矛盾,则k不能是村民 ,k是狼人 
		dfs_(k); //说k是村民的人也是狼人 
		return;
	}
	for (int i = 0; i < v[k].size(); ++i) { //找出说k是村民的人 //假设k是村民的情况 
		dfs(v[k][i], lang);   //向上找 
	}
	
}



int main() {
	
	int t;
	scanf ("%d", &t);
	while(t--) {
		
		memset(v, 0, sizeof(v));
		memset(p, 0, sizeof(p)); 
		memset(in, 0, sizeof(in));
		scanf ("%d", &n);
		for (int i = 1; i <= n; ++i) {
			scanf ("%d %s", &x, s);
			if (s[0] == 'w') {
				p[i] = x; // i说x是狼人 
			} else {
				v[x].push_back(i); // x被i说是村民 
			}
		}
		for (int i = 1; i <= n; ++i) {
			if (p[i]) {  
				dfs(i, p[i]); // i说x是狼人 
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; ++i) {
			if (in[i]) ans++;
		}
		printf ("0 %d\n", ans);
	}
	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值