HDU 6370 Werewolf (并查集+反向建图)*

Werewolf

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


 

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≤xN

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 

 

 

#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6+5;

/*
题目大意:有n个玩家,每个人可能是村民或是狼人,
现在有个有向关系即每个人都说另一个人是村民或者说狼,
村民只能说真话,狼随意,现在要求出一定是狼的人数。

先用并查集正向建图,然后在每个节点都保存反向建图的节点,
由于不只有一个,所以用vector来存储反向图的后继节点。

对于说别人是村民的关系,用并查集维护下,(顺便建立反向图),
然后检查谁说别人是狼,那个被说的人是否在并查集中,
如果在,则以其为根dfs其反向图。

*/

struct node
{
    vector<int> v;///倒建图
    int iswolf;
    int nxt;
    node(){}
};
node edge[maxn];

int n,m;
char seq[20];
int mark[maxn];
int pre[maxn];

int Find(int x)
{
    if(x==pre[x]) return x;
    return pre[x]=Find(pre[x]);
}

void bing(int x,int y)
{
    int tx=Find(x),ty=Find(y);
    if(tx==ty) return ;
    pre[tx]=ty;
}

void dfs(int u)
{
    mark[u]=1;
    int len=edge[u].v.size();
    if(len==0) return ;
    for(int i=0;i<len;i++)
    {
        int x=edge[u].v[i];
        dfs(x);
    }
}


int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);

        for(int i=1;i<=n;i++) edge[i].v.clear();
        for(int i=1;i<=n;i++) pre[i]=i;

        for(int i=1;i<=n;i++)
        {
            scanf("%d%s",&m,seq);
            edge[i].nxt=m;
            if(seq[0]=='w')   edge[i].iswolf=1;
            else
            {
                edge[i].iswolf=0;
                edge[m].v.push_back(i);
                bing(i,m);
            }
        }

        memset(mark,0,sizeof(mark));

        for(int i=1;i<=n;i++)
        {
            int nx=edge[i].nxt;
            if(edge[i].iswolf==0) continue;
            if(Find(nx)==Find(i)) dfs(nx);
        }

        int ans=0;
        for(int i=1;i<=n;i++) ans+=mark[i];

        printf("%d %d\n",0,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值