hdu6370 搜索 + 并查集 Werewolf

本文深入探讨了狼人杀游戏中玩家角色判断的算法实现,通过分析玩家言论,将玩家分为三类:确定村民、确定狼人和不确定身份。文章提供了一种基于并查集的数据结构来解决这一问题,并附带了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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
基环树,基环内向树,基环外向树:
https://blog.youkuaiyun.com/Q1410136042/article/details/81152191
这里写图片描述
赛场上思路出来了,一直没有想到怎么写出来,还是写得少,继续码代码吧

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
typedef struct Node{
    vector<int>ve;
    int iswolf;
    int nxt;
}Node;
Node edge[N];
int b[N];
bool iswolf[N];
int join(int x)
{
    int i = x;
    while(x != b[x])
        x = b[x];
    while(i != b[i])
    {
        int t = b[i];
        b[i] = x;
        i = t;
    }
    return x;
}
void bing(int x,int y)
{
    int p = join(x);
    int q = join(y);
    if(p != q){
        b[p] = b[q];
    }
}

void dfs(int num)
{
    iswolf[num] = true;
    if(edge[num].ve.size() == 0){
        return ;
    }
    int len = edge[num].ve.size();
    for(int i = 0;i < len;++i)
    {
        dfs(edge[num].ve[i]);
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;++i)
            b[i] = i;
        for(int i = 0;i < n;++i)
            edge[i + 1].ve.clear();
        for(int i = 0;i < n;++i)
        {
            char s[15];int m;
            scanf("%d %s",&m,s);
            edge[i + 1].nxt = m;
            if(strcmp(s,"werewolf") == 0){
                edge[i + 1].iswolf = 1;
            }
            else{
                edge[i + 1].iswolf = 0;
                bing(i + 1,m);
                edge[m].ve.push_back(i + 1);
            }
        }
        //system("pause");
        memset(iswolf,false,sizeof(iswolf));
        //printf("0\n");
        for(int i = 0;i < n;++i)
        {
            if(edge[i + 1].iswolf == 1)
            {
                int num = edge[i + 1].nxt;
                if(join(num) == join(i + 1)){
                     dfs(num);
                }
            }
        }
        int cnt = 0;
        for(int i = 1;i <= n;++i)
        {
            if(iswolf[i]){
                cnt++;
            }
        }
        printf("0 %d\n",cnt);
    }
    return 0;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值