hdu 6370 ( 推理 )

 

Werewolf

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


 

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 

感觉思维真的跟不上。

题意: 一堆人在玩狼人杀,每个人只对其他人中的一个人谈论一次,并且不谈论自己, 第i 行表示 i 认为x 是狼 或者是人,人一定说真话,狼可能说假话。题目保证有解,当然也就不会出现n==1 的情况。 你要确定一定是人的数量 和一定是狼的数量。

思路:如果所有的全部是狼,那么一定是成立 的,因为狼可以XJB说,所以能确定一定是人的数量是确定的,就是0。那么问题就转变成了找出一定是狼的个数,也可以理解为找出多少人可能为人。 我们考虑这样的一个问题,如果a说b是人,b说c是人,c说d是人,d说a是狼,那么谁是狼呢? 答案是都是狼,因为人不会说假话,如果我们假设a是人,那么一条链下来全部都是人,那么d就不可能说a 是狼,所以唯一可能的解释就是全是狼,那么如果我们改成d说b是狼呢? 那么答案就肯定变成a b 是狼。 如果d说c是狼呢,那么肯定是abc是狼,而d可能是人。那么就是如果有人指认他的父亲是狼,那么所有认为直接或者间接认为他父亲是人的都在撒谎,也就是都是狼。  这里还有一个问题。   如果a说b 是人 b说c是狼,c说d是人,d说a是狼,那么可以肯定是这个环中的哪些是人,哪些是狼是不能确定的。 也就是说一个环中只能有一条狼人边,否则不能确定那些人一定是狼。

实现: 先拓扑一下,把所有的不是环中的点去掉,然后在环中找到,被人指证为狼的人,那么他的父亲全部是狼(反向建边就是他的孩子全部是狼,除了遇到狼人边)

代码:

#include<bits/stdc++.h>

using namespace std;
const int N =1e5+5;

int inde[N];
int vis[N];
int zhi[N];
int flag[N];
vector<int >ve[N];  /// 反向图
vector<int >wi;
vector<int >mp[N];  /// 正向图
int n;
int ans;

void dfs(int u)
{
    vis[u]=1;
    ans++;
    for(int i=0;i<ve[u].size();i++){
        int v=ve[u][i];
        if(flag[v]) continue;
        if(vis[v]) continue;
        dfs(v);
    }
}

int main()
{
    int T;
    int u,v;
    char op[15];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) ve[i].clear();
        for(int i=1;i<=n;i++) mp[i].clear();
        memset(vis,0,sizeof(vis));
        memset(flag,0,sizeof(flag));
        memset(inde,0,sizeof(inde));
        wi.clear();

        for(int i=1;i<=n;i++){
            scanf("%d %s",&u,op);
            if(op[0]=='w') flag[i]=1;  /// 表示i指证u是狼
            ve[u].push_back(i);
            mp[i].push_back(u);
            inde[u]++;
            zhi[i]=u;
        }

        queue<int >q;
        for(int i=1;i<=n;i++){
            if(inde[i]==0) q.push(i);
        }

        while(!q.empty()){
            u=q.front(); q.pop();
            vis[u]=1;
            for(int i=0;i<mp[u].size();i++){
                v=mp[u][i];
                inde[v]--;
                if(inde[v]==0) q.push(v);
            }
        }

        //for(int i=1;i<=n;i++) if(vis[i]==0) cout<<" *** "<<i<<" "<<zhi[i]<<endl;

        for(int i=1;i<=n;i++){
            if(vis[i]==0){ /// 在环中 判断环中有几个狼人边,如果大于等于两个也不能确定身份
                vis[i]=1;
                int lang=-1;
                int cnt=0;
                int fa=i;

                //cout<<"*** i "<<i<<endl;
                if(flag[i]){
                    lang=zhi[i];
                    cnt++;
                }
                u=zhi[i];

                while(u!=fa){
                    vis[u]=1;
                    if(flag[u]){
                        cnt++;
                        lang=zhi[u];
                    }
                    u=zhi[u];
                }

                //cout<<" cnt "<<cnt<<endl;
                if(cnt==1){
                    wi.push_back(lang);
                }

            }
        }

        ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<wi.size();i++){
            u=wi[i];
            if(vis[u]) continue;
            dfs(u);
        }
        cout<<0<<" "<<ans<<endl;

    }
    return 0;
}

/*

1
8
3 w
1 v
2 v
3 v
2 v
5 v
5 v
4 w

1
8
3 w
1 v
2 v
3 v
3 w
5 v
5 v
4 w



*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值