2018 Multi-University Training Contest 6 1009 (HDU6370) Werewolf 拓扑判环(基环树)

HDU6370 Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 662 Accepted Submission(s): 155

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

题解:
1.根据题意可知,无法找出一个推论证明一个人一定是村民,因此一定是村民的数目为0,而当某一个人所指认的好人或其指认的好人指认的好人指认他是狼人,因为村民不会说谎,所以那么他要么说谎了,要么就是狼人,说谎了也是狼人,所以他一定是狼人。所以我们可以通过这个结论来判断狼人,同时指认狼人为好人的人也是狼人。

2.所以抽象题目意思,将指认好人建好人边,指认狼人建狼人边,我们需要找到一个环,(它同时也是一个基环树),并且该环有且仅有一个狼人边,该狼人边所指向的便是狼人。

3.为了更好的找环,我们先用拓扑排序将所有非环节点标记掉,然后跑环,记录符合条件的环上的狼人,然后利用得到的狼人反向bfs找所有指认他为好人的人,这些人也为狼人。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int maxn = 1e5  + 100;
const int maxm = 1000 + 100;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int,int> P;
//const LL mod = 1e9 + 7;

#define PI 3.1415926
#define sc(x)  scanf("%d",&x)
#define pf(x)  printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%lld\n",x)
#define pfs(x) printf("%d ",x)
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb(x)  push_back(x);

vector <int> G[maxn];
vector <int> lang;
int vis[maxn],a[maxn],ind[maxn];
int n;

void solve(int x)
{
  vis[x] = 1;
  int t = a[x];
  while(1)
  {
    if(t < 0) t *= -1;
    ind[t]--;
    if(ind[t]) return ;
    vis[t] = 1;
    t = a[t];
  }
}

void findc(int x)
{
  int la;
  int cnt = 0;
  int t = x;
  while(1)
  {
    vis[t] = 1;
    t = a[t];
    if(t < 0) t *= -1;
    else la = t,cnt++;
    if(t == x) break;
  }
  if(cnt == 1) lang.pb(la);
}

queue <int> que;
void bfs(int x)
{
  que.push(x);
  while(!que.empty())
  {
    int t = que.front();que.pop();
    rep(i,0,G[t].size())
       {
         int u = G[t][i];
         lang.pb(u);
         que.push(u);
      }
    }
}

int main()
{
    int T;
    sc(T);
    while(T--)
    {
      sc(n);
      mem(vis,-1);
      mem(ind,0);
      rep(i,1,n+1) G[i].clear();
      lang.clear();
      rep(i,1,n+1)
      {
        char s[20];
        sc(a[i]);
        ind[a[i]] ++;
        scanf("%s",s);
        if(s[0] == 'v')
          {
            G[a[i]].pb(i);
            a[i] *= -1;
          }
      }
      rep(i,1,n+1)      if(vis[i] == -1 && ind[i] == 0) solve(i);
      rep(i,1,n+1)      if(vis[i] == -1 && ind[i] == 1) findc(i);
      int len = lang.size();
      rep(i,0,len)  bfs(lang[i]);
      pfs(0);pfn(lang.size());
    }
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值