2018 Multi-University Training Contest 6: I. Werewolf(DFS)

 

Werewolf

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

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

没有题解说得那么玄乎

如果A说B是狼人,那么B到A建一条价值为1的边,如果A说B是平民,那么B到A建一条价值为0的边

之后只要出现一个环总价值刚好为1,也就是环中存在刚好1个点被说是狼人,那么他就一定是狼人

除此之外,只要某个人一定是狼人,那么说他是平民的也一定是狼人

搞定

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
typedef struct
{
	int v;
	int len;
}Res;
Res now;
vector<Res> G[100005];
char str[100005];
int vis[100005], len[100005], col[100005], ans;
void Sech(int u, int flag, int val, int id)
{
	int i;
	vis[u] = flag;
	len[u] = val;
	for(i=0;i<G[u].size();i++)
	{
		now = G[u][i];
		if(vis[now.v]!=0 && vis[now.v]!=flag)
			continue;
		if(vis[now.v]==flag)
		{
			if(len[now.v]+1==val+now.len)
			{
				if(now.len==1)
					id = u;
				col[id] = 1;
			}
			continue;
		}
		if(now.len==0)
			Sech(now.v, flag, val+now.len, id);
		else
			Sech(now.v, flag, val+now.len, u);
	}
}
void Sech2(int u)
{
	int i, v;
	vis[u] = 1;
	for(i=0;i<G[u].size();i++)
	{
		now = G[u][i];
		if(now.len==0 && vis[now.v]==0)
		{
			ans++;
			Sech2(now.v);
		}
	}
}
int main(void)
{
	int T, n, i, x;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(i=1;i<=n;i++)
		{
			G[i].clear();
			vis[i] = len[i] = col[i] = 0;
		}
		for(i=1;i<=n;i++)
		{
			scanf("%d%s", &x, str+1);
			now.v = i, now.len = 0;
			if(str[1]=='w')
				now.len = 1;
			G[x].push_back(now);
		}
		for(i=1;i<=n;i++)
		{
			if(vis[i]==0)
				Sech(i, i, 0, 0);
		}
		for(i=1;i<=n;i++)
			vis[i] = 0;
		ans = 0;
		for(i=1;i<=n;i++)
		{
			if(col[i]==1 && vis[i]==0)
			{
				ans++;
				Sech2(i);
			}
		}
		printf("0 %d\n", ans);
	}
	return 0;
}
/*
3
9
2 wwwww
3 vvvv
1 vvvvv
5 vvvv
6 wwww
7 vvvv
4 vvvvv
6 vvvv
8 vvvv
*/

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/81515476
今日推荐