Werewolf HDU - 6370 (找规律+并查集+bfs)

"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≤101≤T≤10 

1≤N≤100,0001≤N≤100,000 

1≤x≤N1≤x≤N 

S∈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

思路:需要发现,只有找到一个环并且一个环中有且只有一个人被指认为狼,其他人都被指认为村民时,才能确定一只狼,当确定一只狼以后,只要说它是村民的,必然也是狼。

所以对于指认是狼的边,直接连,指认是村民的边,连接后并查集合并(这样就可以省去dfs找环)

最后bfs找其他狼就行。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
#define inf 0x3f3f3f3f
int num;
int head[maxn];
bool vis[maxn];
int f[maxn];
int n;
int Find(int x)
{
    if(f[x]==x) return x;
    else  return f[x]=Find(f[x]);
}
void Union(int x,int y)
{
    int fx=Find(x),fy=Find(y);
    if(fx==fy) return;
    f[fy]=fx;
}
struct Edge
{
    int u,v,next,w;
}edge[maxn<<1];
void addEdge(int u,int v,int w)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
void init()
{
    memset(head,-1,sizeof(head));
    num=0;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++) f[i]=i;
        for(int i=1;i<=n;i++)
        {
            int x;
            char s[20];
            scanf("%d%s",&x,s);
            if(s[0]=='w')
            {
                addEdge(x,i,1);
            }
            else
            {
                addEdge(x,i,0);
                Union(i,x);
            }
        }
        queue<int> que;
        memset(vis,0,sizeof(vis));
        int ans=0;
        for(int i=0;i<=num;i++)
        {
            int u=edge[i].u,v=edge[i].v,w=edge[i].w;
            if(Find(u)==Find(v)&&w==1&&vis[v]==0)
            {
                ans++;
                que.push(u);
                vis[u]=1;
            }
        }   
        while(que.empty()==0)
        {
            int u=que.front();
            que.pop();
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v,w=edge[i].w;
                if(w) continue;
                if(vis[v]) continue;
                ans++;
                que.push(v);
            }
        }
        printf("0 %d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值