Werewolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1890 Accepted Submission(s): 537
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 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"}
首先,“所有人都是狼人”是合法的,所以第一个答案一定是0。 那么我们的任务就是确认有多少人满足存在一种方案使得这个人是村民,其他人就是铁狼了(即为第二个要求输出 的答案)
我们把所有人抽象成点:
1. 对于每句话,建立一条说话的人指向被说的人的一条有向边。
2. 如果该句话为说某人是狼人,就称这条边为狼人边。
暂时不考虑狼人边,把图分成若干联通块,这样每个联通块:
1. 要么点数和边数一致(基环树)
2. 要么点数比边树多1(树)
对于基环树,由于没有狼人边,所以让他们都是村民是合法的,这些人都不是铁狼。
现在对于树,考虑狼人边(有且仅有一条),狼人边是树根连出去的。
1. 如果狼人边指向的是其他联通块: 那么让树中的人都是村民,其他人都是狼人显然也合法。 这些人都不是铁狼。
2. 如果指向树中的某个节点:
如果根是狼人,则其儿子就是狼人,以至于整棵树都是狼人,没有意义。
如果根是村民,则其指向的节点 是铁狼,则以 为根的子树都是铁狼。 而让树中的其它节点为村民,此时合法。 所以树中非 子树的节点都可以是村民。
因此使用记忆化搜索把那些铁狼找出来,统计一下人数就行了。
以上时官方题解,简单理解就是:先将所有的村名边扔到并查集里,注意a说b,则b是a的根,同时注意防止形成环。这样就形成了一个个树,树的根节点那个人一定说了某个人(b)是狼,如果他说的这个人在别的树里,就不用管,如果在自己的树里,那么b及其b子树上所有的节点都是铁狼(所有情况下都是狼),统计铁狼的数量,输出。
我统计狼的方法是建立一个额外的并查集数组(par2),先不路径压缩,在遍历狼人边时,将狼人边指向的节点的值置0,然后find 1-n(带路径压缩),然后遍历par2,统计值为0的个数,即为铁狼的个数。
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define maxn 100009
using namespace std;
struct node_edge
{
int a; //a说b是v
int b;
char v;
}e[maxn];
int par[maxn], ranks[maxn],par2[maxn];
void init_bcj(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
par2[i] = i;
ranks[i] = 0;
}
}
int find_bcj(int x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find_bcj(par[x]);
}
}
int find_par2(int x) {
if (par2[x] == x) {
return x;
}
else {
return par2[x] = find_par2(par2[x]);
}
}
void unite_bcj(node_edge ne) {
int x = find_bcj(ne.a);
int y = find_bcj(ne.b);
if (x == y) return;
par2[ne.a] = ne.b;
if (ranks[x] < ranks[y]) {
par[x] = y;
}
else {
par[y] = x;
if (ranks[x] == ranks[y]) ranks[x]++;
}
}
queue<int> q;
int t,n;
int ans;
string s;
node_edge wolfedge;
void setans() {
par2[0] = 0;
for (int i = 1; i <= n; i++) {
find_par2(i);
}
for (int i = 1; i <= n; i++) {
if (par2[i] == 0) {
ans++;
}
}
}
void solve() {
while (!q.empty())
{
wolfedge = e[q.front()];
q.pop();
if (find_bcj(wolfedge.a) == find_bcj(wolfedge.b)) {
par2[wolfedge.b] = 0;
}
}
setans();
}
int main() {
fio;
cin >> t;
while (t--)
{
while (!q.empty()) q.pop();
ans = 0;
cin >> n;
init_bcj(n);
for (int i = 1; i <= n; i++) {
cin >> e[i].b >> s;
e[i].a = i;
e[i].v = s[0];
}
for (int i = 1; i <= n; i++) {
if (e[i].v == 'v') {
unite_bcj(e[i]);
}
else {
q.push(i);
}
}
if (q.size() == n) {
cout << 0 << " " << 0 << endl;
}
else {
solve();
cout << 0 << " " << ans << endl;
}
}
return 0;
}