HDU 6370 Werewolf 【并查集】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6370

Werewolf

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


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:

1T10

1N100,000

1xN

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

题意概括:

读题意前要清空一下记忆,这里的狼人杀和实际的狼人杀规则不太一样。

每人按顺序说出一个人的角色(不能说自己的)

1、村民一定说真话

2、狼人有可能说假话。

求:

一定是村民的数量 和 一定是狼人的数量。

解题思路:

模拟了一下,感觉并不会有村民,因为没有方案可以证明出某个人一定是村民,因为狼人有“可能”说谎。

但我们可以确认哪些一定是狼人,即存在环,环的最后是 狼人边,其余都是村民边,那么被狼人边指的一定是狼人。

而用村民边指向这个狼人的角色也一定是狼人。

那么DFS,村民边建正向边,狼人边建反向边。

遍历狼人边,判断是否存在上述的环,如果存在再继续拓展。

大佬的证明:https://blog.csdn.net/weixin_39453270/article/details/81515570

AC code:

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define LL long long
 4 using namespace std;
 5 const int MAXN = 1e5+10;
 6 
 7 int N, M;
 8 int w[MAXN];
 9 vector<int>fa[MAXN], son[MAXN];
10 bool vis[MAXN];
11 int ans[MAXN], cnt;
12 
13 void add1(int a, int b)
14 {
15     fa[a].push_back(b);
16 }
17 
18 bool dfs(int a, int b)
19 {
20     bool flag = true;
21     for(int i = 0; i < fa[a].size(); i++){
22         if(fa[a][i] == b) return false;
23         flag = dfs(fa[a][i], b);
24         if(!flag) return false;
25     }
26     return flag;
27 }
28 
29 void dfs2(int x)
30 {
31     if(!vis[x]) return;
32     vis[x] = false;
33     for(int i = 0; i < fa[x].size(); i++){
34         if(vis[fa[x][i]]){
35             cnt++;
36             dfs2(fa[x][i]);
37         }
38     }
39 }
40 
41 int main()
42 {
43     //freopen("6370in.txt", "r", stdin);
44     int T_case, id, tot = 0;
45     char ty[10];
46     scanf("%d", &T_case);
47     while(T_case--){
48         cnt = 0;
49         tot = 0;
50         memset(vis, 1, sizeof(vis));
51         scanf("%d", &N);
52         for(int i = 1; i <= N; i++){ fa[i].clear();son[i].clear();}
53 
54         for(int i = 1; i <= N; i++){
55             scanf("%d %s", &id, &ty);
56             if(ty[0] == 'w'){
57                 w[++tot] = i;
58                 son[i].push_back(id);
59             }
60             else{
61                 add1(id, i);
62             }
63         }
64         for(int i = 1; i <= tot; i++){
65             if(!vis[w[i]]) continue;
66             for(int k = 0; k < son[w[i]].size();k++){
67                 if(!vis[son[w[i]][k]]) continue;
68                 if(!dfs(w[i], son[w[i]][k])){
69                     //puts("zjy");
70                     cnt++;
71                     dfs2(son[w[i]][k]);
72                 }
73             }
74         }
75 
76         printf("0 %d\n", cnt);
77     }
78 }

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/10436504.html