杭电多校练习 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
typedef pair<ll,ll> 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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值