问题 I: Werewolf(杭电多校第六场1009)

题目描述

"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. 

 

输入

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"} 

 

输出

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.

样例输入

1
2
2 werewolf
1 werewolf

样例输出

0 0

题意:狼人杀,只有两种角色:平民和狼,已知平民一定说真话,狼可能说真话也可能说假话。求确定的村民和狼人的number。

分析:狼可以说真话也可以说假话,所以可以确定的村民数为0.那么讨论确定是狼的个数就ok了

  1. 如果:A 指认 B 为 Villager,B 指认 A 为 werewolf,那么:A —>wolf,B 不确定。
  2. 如果:A is wolf 。B 指认 A 为 Villager。那么:B —>wolf。

根据输入建图,a 说 b 是平民,那么建立 a 指向 b 的边……具体看代码。

AC代码:

/* Lyl */
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;

const int maxn = 1e5+5;

int cnt, head[maxn];
int wolf[maxn], vis[maxn];
int fa[maxn];

struct node{
    int x, vg;
    char ch[10];
}a[maxn];

struct Edge{
    int v, nxt;
}edge[maxn];

void add(int u, int v){
    edge[++cnt].v=v;
    edge[cnt].nxt=head[u];
    head[u] = cnt;
}

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

void Union(int x, int y){
    int fx = Find(x);
    int fy = Find(y);
    if(fx != fy)
        fa[x] = fy;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        cnt = 0; ms(vis, 0);
        int n; ms(head, -1);
        scanf("%d", &n);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i = 1; i <= n; i++){
            scanf("%d%s", &a[i].x, a[i].ch);
            a[i].vg = (a[i].ch[0] == 'v');
            if(a[i].vg == 1)
            {
                add(a[i].x, i); //Build 链式关系结构
                Union(i, a[i].x); //Build Villagers' 并查集
            }
        }
        int num=0; //must Villager' number =0, must wolf' number = ?

        for(int i=1;i<=n;i++) //A->B wolf A,B have same father B->wolf
            if(Find(i) == Find(a[i].x) && a[i].vg == 0){
                wolf[num++] = a[i].x;
                vis[a[i].x] = 1;
            }

        for(int i=0;i<num;i++){
            int x=wolf[i];     //A:wolf. B->A Villager, B->wolf
            for(int j = head[x]; ~j; j = edge[j].nxt){
                int y = edge[j].v;
                if(!vis[y]){
                    wolf[num++] = y;
                    vis[y] = 1;
                }
            }
        }
        printf("0 %d\n", num);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值