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

本文深入解析了狼人杀游戏中玩家角色判断的算法,通过建立玩家间的信任网络,运用图论中的环检测来确定玩家身份,即村民或狼人。文章详细介绍了如何通过玩家之间的指控构建图模型,并利用深度优先搜索算法来识别一定条件下玩家的真实身份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值