HDU 6370 Werewolf(基环树+树+暴搜)

Werewolf

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


题目链接

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

题意:

n个人,两种身份——村民、狼。村民不会说谎、狼可能会说谎。

每一个人进行一个陈述——说别人是狼/村名(不能说自己)

问你通过判断这些话,最终能确定一定是村民的人的个数和一定是狼的人的个数

解析:

官方题解

N 个人,每个人可能好人可能坏人,每个人说了一句话,好人一定说了真话,坏人不一定。

每句话指定某个人是好人还是坏人,问哪些人一定是好人哪些人一定是坏人。

Part 1

首先,“所有人都是狼人”是合法的,所以第一个答案一定是0。

那么我们的任务就是确认有多少人满足存在一种方案使得这个人是村民,其他人就是铁狼了(即为第二个要求输出的答案)

我们把所有人抽象成点:

  1. 对于每句话,建立一条说话的人指向被说的人的一条有向边。
  2. 如果该句话为说某人是狼人,就称这条边为狼人边。

暂时不考虑狼人边,把图分成若干联通块,这样每个联通块:

  1. 要么点数和边数一致(基环树)
  2. 要么点数比边树多1(树)

对于基环树,由于没有狼人边,所以让他们都是村民是合法的,这些人都不是铁狼。

现在对于树,考虑狼人边(有且仅有一条),狼人边是树根连出去的。

  1. 如果狼人边指向的是其他联通块:
    • 那么让树中的人都是村民,其他人都是狼人显然也合法。
    • 这些人都不是铁狼。
  2. 如果指向树中的某个节点:
    • 如果根是狼人,则其儿子就是狼人,以至于整棵树都是狼人,没有意义。
    • 如果根是村民,则其指向的节点 xxx 是铁狼,则以 xxx 为根的子树都是铁狼。
    • 而让树中的其它节点为村民,此时合法。
    • 所以树中非 xxx 子树的节点都可以是村民。

因此使用记忆化搜索把那些铁狼找出来,统计一下人数就行了。

时间复杂度O(n)。

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
#define pb push_back
using namespace std;

const int MAXN = 1e5+10;

set<int> edge[MAXN];
char str[20];
vector<int> outw[MAXN];
int vis[MAXN];


void dfs(int x)
{
    vis[x]=1;
    for(set<int>::iterator iter=edge[x].begin();iter!=edge[x].end();iter++)
    {
        int v=*iter;
        if(vis[v]) continue;
        dfs(v);
    }
}

int dfsfind(int x,int s)
{
    for(set<int>::iterator iter=edge[x].begin();iter!=edge[x].end();iter++)
    {
        int v=*iter;
        if(v==s) return 1;
        if(dfsfind(v,s)) return 1;
    }
    return 0;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) edge[i].clear(),outw[i].clear(),vis[i]=0;
        for(int i=1;i<=n;i++)
        {
            getchar();
            int x;
            scanf("%d%s",&x,&str);
            if(strcmp(str,"werewolf")==0)        
            {
                outw[i].pb(x);
            }
            else
            {
                edge[x].insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<outw[i].size();j++)
            {
                int v=outw[i][j];
                if(vis[v]) continue;
                if(edge[i].count(v)||dfsfind(i,v))
                {
                    dfs(v);
                }
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]) ans++;
        }
        printf("0 %d\n",ans);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/81532251