杭电多校练习 Werewolf(狼人杀,并查集+思维)

Werewolf

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


 

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

2018 Multi-University Training Contest 6

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6373 6372 6371 6370 6369 

 

 

Statistic | Submit | Discuss | Note

题意:一群人玩狼人杀,只有两种角色,平民和狼人,已知每个人说一句话,xx是xx,已知平民一定说真话,狼人不一定说真话还是假话,在所有情况下,一定是狼人的人有多少?一定是平民的人有多少?

题解:首先因为狼人是没有限制的,所以肯定有一种情况是所有人都是狼人,所以肯定是农民的人就是0,所以只需要去判断一定是狼人的人即可,首先可以将每一句话建图,i 说 j 是平民 ,则建立一条由i指向j的平民边,就这样由一个人指出的平民边出发,如果走到最后一个人发出的是一条狼人边,若狼人边指向的是这串平民边的始发者,则该人一定是狼人(可以简单推理一下,如果该人是平民,则沿着平民边都成立,那么最后指向该人的狼人边也应该是真的,矛盾),第二条是所有说狼人是平民的一定是狼人。那么就可以根据这个关系求解了(相当不好求好吧!!),直接暴力会超时就用并查集优化一下。

//sheryang
#include
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
typedef pair P;
#define pi acos(-1)
struct node
{
    char s[20];
    int x;
}p[maxn];
int vis[maxn];

int vil[maxn],num[maxn],judge[maxn];

int find1(int x)
{
    return vil[x]==x?x:vil[x]=find1(vil[x]);
}

void mer1(int x,int y)
{
    int fx=find1(x);
    int fy=find1(y);
    if(fx!=fy)
    {
        vil[fx]=fy;
        num[fy]+=num[fx];//记录并查集中的元素数量
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        memset(judge,0,sizeof(judge));

        int n;
        char s[50];
        scanf("%d",&n);

        for(int i=1;i<=n;i++)
            vil[i]=i,num[i]=1;

        for(int i=1;i<=n;i++)
        {
            scanf("%d %s",&p[i].x,p[i].s);
            if(p[i].s[0]=='v')
                mer1(i,p[i].x);//建立平民边组成的并查集
        }

        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int fx=find1(i);
            if(vis[fx]) continue;
            if(p[fx].x==i && p[fx].s[0]=='w')//平民边的最后指向的是首发者
                judge[i]=1,vis[fx]=1;//标记这个人一定是狼人
        }

        for(int i=1;i<=n;i++) vil[i]=i,num[i]=1;
        for(int i=1;i<=n;i++)
        {
            int x=p[i].x;
            if(p[i].s[0]=='w') continue;
            if(judge[i] && judge[x]) continue;
            if(judge[i] && !judge[x]) continue;
            if(!judge[i] && judge[x]) mer1(i,x);
            if(!judge[i] && !judge[x]) mer1(x,i);
        }


        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            if(!judge[i]) continue;
            int fx=find1(i);
            if(vis[fx]) continue;
            ans+=num[fx];
            vis[fx]++;
        }
        printf("0 %d\n",ans);
    }
    return 0;
}

 

你可能感兴趣的:(数据结构,思维)