Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 770 Accepted Submission(s): 187
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
题意:有n个人玩狼人杀。每个人都会发言 内容是 x是werewolf(狼) 或者 x是villager(村民)。村民不会说谎,狼可能会说谎。每个人都不能说自己。求最终可以确定多少个村民和多少只狼(确定了这个人是几号)。
思路:首先所有人都是狼一定合法,所以村民无法确定。经过一番推理,发现只有两种情况可以确定狼:
1、某些人的发言构成一个环,环上只有一个人发言说xxx是狼,其他的都说是村民。那么被说是狼的这个人一定是狼。
2、由1确定狼以后,有人说1确定的狼是人,那么这个人就一定是狼。(因为村民不会说谎)
其实可以用一些更好的算法找环的,然后2的情况再判断一下就好。我比赛的时候暴力找环暴力找狼904ms卡过。。。
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int n,m,k;
int vis[maxn];
int id[maxn],c[maxn];
bool a[maxn];
int ans,ct,cnt,tmp,f,flag;
char s[maxn];
vector<int>vc[maxn];
queue<int>q;
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ans=0;tmp=0;
for(int i=1;i<=n;i++)
{
vis[i]=0;
a[i]=false;
vc[i].clear();
}
for(int i=1;i<=n;i++)
{
scanf("%d",&id[i]);
scanf("%s",s);
if(s[0]=='w') c[i]=1;
else {c[i]=0;vc[id[i]].push_back(i);}
}
f=0;
for(int i=1;i<=n;i++)
if(!vis[i]){
cnt=0;
int j=id[i];
f++;
vis[i]=f;
while(1)
{
if(vis[j]==inf) break;
if(vis[j]==f)
{
cnt=0;
vis[j]=inf;
int k=id[j];
if(c[j]) cnt++;
while(1)
{
vis[k]=inf;
if(c[k]) {cnt++;flag=id[k];}
k=id[k];
if(k==j) break;
}
if(cnt==1) {tmp++;a[flag]=1;
for(int tt=0;tt<vc[flag].size();tt++)
{
q.push(vc[flag][tt]);
}
}
break;
}
//cout<<"*";
vis[j]=f;
j=id[j];
}
}
while(!q.empty()){
int k=q.front();q.pop();
tmp++;
for(int tt=0;tt<vc[k].size();tt++)
{
q.push(vc[k][tt]);
}
}
printf("%d %d\n",ans,tmp);
// if(flag) puts("Yes"); else puts("No");
}
return 0;
}