杭电多校 Werewolf(狼人杀,并查集+思维)

Werewolf

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


 

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

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6373 6372 6371 6370 6369 

 

 

Statistic | Submit | Discuss | Note

【题意】:一群人玩狼人杀,只有两种角色,平民和狼人,已知每个人说‘一句话’,xx是xx,已知平民一定说真话,狼人不一定说真话还是假话,在所有情况下,一定是狼人的人有多少?一定是平民的人有多少?

【题解】:首先我们要读懂这道题 许多人可能都对这道题意有什么误解。我们输出的是所有情况下都”一定是狼人“的人的数量,必须能够明确指出几号是狼人,而不是至少有多少个狼人(不能确定是谁) 

所有情况下,所有人都是狼人一定都能推的过去,因此没有人一定是平民,这个好想,然后我们需要想的是一定是狼人的人。

我们要想证明一个人一定是狼人,可以用反证法,假设这个人是平民,然后发现与事实不成立,就可以说明这个人是狼人。

就是下图这种情况:

 

请忽略我画的图太丑了,,,能讲清楚就好 (捂脸)

我们假设1号玩家是平民 ,他说2号玩家是平民,(平民只会说真话),2号玩家说三号玩家是平民,则3号玩家说的话也一定是真话,但是3号玩家说1是狼人,于是推翻了我们的假设,1号玩家一定是狼人。 4号玩家说了谎,也一定是狼人,5号玩家也说了谎,也一定是狼人。 这张图就包含了一个人一定是狼人的所有情况,你们可以画画看。当一个人说的话能够反驳他自己,他就是狼人。

做法:我是用了两个并查集,第一个并查集是找出来一定是狼人的人,即上图中的一号,对所有的平民边建边,如果祖先节点发出了一条狼人边且指向并查集内部的人,则被指的这个人一定是狼人。先把所有这种狼人找出来,存到一个集合中。

2、处理小尾巴,即4,5. 如果一个人对一个已经确定是狼人的人发出平民边,则这个人也是狼人。第二个并查集合并有两种情况:一是两个不能确定身份的且通过平民边连接的玩家,另一个是不能确定身份的人向已经确定是狼人的人发出平民边。合并这两种情况,用一个数组统计一下并查集中元素的个数。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+3;
int pre[maxn];
int wolf[maxn];
int vis[maxn];
int cnt[maxn];//记录并查集中元素的数量
struct player
{
    int id;
    char c;
}info[maxn];
int find(int x)
{
    if(x!=pre[x])
        pre[x]=find(pre[x]);
    return pre[x];
}
void joint(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
        {
            pre[fx]=fy;
            cnt[fy]+=cnt[fx];
        }
}
 
int main()
{
    int T,n;
    cin>>T;
    while(T--)
    {
        cin>>n;
        memset(wolf,0,sizeof(wolf));
        memset(vis,0,sizeof(vis));
        memset(cnt,1,sizeof(cnt));
        for(int i=1;i<=n;i++)
            pre[i]=i;
        for(int i=1;i<=n;i++)
        {
            int num;
            char s[10];
            cin>>num>>s;
            info[i].id=num;
            info[i].c=s[0];
            if(s[0]=='v')
                joint(i,num);
        }
        for(int i=1;i<=n;i++)
        {
            int f=find(i);
            if(vis[f]==0&&info[f].c=='w')
            {
                vis[f]=1;
                int p=info[f].id;
                if(find(p)==f)
                    wolf[p]=1;//玩家p是狼
            }
        }
        for(int i=1;i<=n;i++)
            pre[i]=i;
        memset(vis,0,sizeof(vis));
 
        for(int i=1;i<=n;i++)
            cnt[i]=1;
 
        for (int i=1;i<=n;i++)
        {
            if(wolf[i]==0)
            {
                int v=info[i].id;
                char s=info[i].c;
                if(wolf[v]==0&&s=='v')
                    {
                        joint(i,v);
                    //printf("1  i=%d,v=%d\n",i,v);
                    }
                else if(wolf[v]==1&&s=='v')
                    {
                        joint(i,v);
                   // printf("2  i=%d,v=%d\n",i,v);
                    }
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int fari=find(i);
            if(vis[fari]==0&&wolf[fari]==1)
            {
                ans+=cnt[fari];
 
            }
            vis[fari]=1;
        }
        printf("0 %d\n",ans);
    }
    return 0;
}

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值