hdu6370 Werewolf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fadedsun/article/details/81582827

Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2021 Accepted Submission(s): 577

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

题意:

给定n个关系,第 i 个关系表示 i 说 r 是狼人或村民,根据所有关系
看在任意情况下,确定为狼人和确定为村民的人数
狼人可以说真话,也可以说假话
村民一定说真话

思路:

首先假定所有人都是狼人,可以发现关系是可以成立的.
所以没有确定是村民.

然后画几个图,可以发现,当你说他是好人,他说你是狼的时候,
你是没办法当村民,因为村民一定说真话,
然后再推一下,就发现环的情况里,只有一条狼边的话,被指认为狼的就是铁狼.
而所有说他是好人的,也是铁狼.

# include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+111;

int dfn[maxn],low[maxn],vis[maxn],belong[maxn],num[maxn];
int wolf[maxn],out[maxn];
int T,n,m,ans,ret,tot,sccnum;

struct node{
    int to,mark;
};

stack<int> S;
vector<node> edge[maxn],last[maxn];


void tarjan(int x)
{
    dfn[x] = low[x] = ++tot;
    S.push(x); vis[x] = 1;
    for(int i = 0;i < edge[x].size();i++){
        int v = edge[x][i].to;
        if(!dfn[v])
            tarjan(v),low[x] = min(low[x],low[v]);
        else if(vis[v])
            low[x] = min(low[x],dfn[v]);
    }
    if(low[x] == dfn[x]){
        int cnt = 0;
        while(true){
            int now = S.top(); S.pop();
            belong[now] = x;
            vis[x] = 0; cnt++;
            if(now == x) break;
        }
        num[x] = cnt;
    }
}

void dfs(int x)
{
    for(int i = 0;i < last[x].size();i++)
    if(last[x][i].mark == 0) ret++,dfs(last[x][i].to);
}

void init(){
    memset(out,0,sizeof(out)); memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low)); memset(num,0,sizeof(num));
    memset(belong,0,sizeof(belong));
    tot = sccnum = 0;
    for(int i = 0;i <= n;i++)
    {
        last[i].clear();edge[i].clear();
    }
}

int main()
{
    char op[30];
    scanf("%d",&T);
    while(T--){
        ans = 0;
        scanf("%d",&n);
        init();
        for(int i = 1,r;i <= n;i++)
        {
            scanf("%d %s",&r,op);
            if(op[0] == 'w'){
                edge[i].push_back((node){r,1});
                last[r].push_back((node){i,1});
            }else{
                edge[i].push_back((node){r,0});
                last[r].push_back((node){i,0});
            }
        }
        for(int i = 1;i <= n;i++) if(!dfn[i]) tarjan(i);
        for(int i = 1;i <= n;i++)
        {
            for(int j = 0;j < edge[i].size();j++)
            {
                int v = edge[i][j].to;
                if(belong[v] == belong[i])
                    if(edge[i][j].mark){
                         out[belong[i]]++,wolf[belong[i]] = v;
                    }
            }
        }
        for(int i = 1 ;i  <= n;i++)
        {
            if(out[i] == 1 && belong[i] == i && num[i] > 1){
                ans += 1;
                ret = 0;
                dfs(wolf[i]);
                ans += ret;
            }
        }

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

/*
5
2 villager
3 willager
4 verewolf
5 w
4 w

*/

猜你喜欢

转载自blog.csdn.net/fadedsun/article/details/81582827