-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq3.cpp
More file actions
74 lines (67 loc) · 1.38 KB
/
q3.cpp
File metadata and controls
74 lines (67 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
// 3. 发现它,抓住它
// 与数据结构基础week7 q3食物链类似,这里更简单一点
int parent[100005];
// 与根结点的相关性,true相关,false不相关
bool tag[100005];
int count[100005];
// 路径压缩
int find(int x) {
if (parent[x] == x) {
return x;
}
int p = parent[x];
parent[x] = find(parent[x]);
// 自顶向下更新父子关系
tag[x] = !(tag[p] ^ tag[x]);
return parent[x];
}
// 加权归并
void aUnion(int x1, int x2) {
int p1 = find(x1);
int p2 = find(x2);
if (count[p1] < count[p2]) {
int temp = p1;
p1 = p2;
p2 = temp;
}
// 更新父子关系
tag[p2] = tag[x1] ^ tag[x2];
parent[p2] = p1;
count[p1] += count[p2];
}
int main(int argc, char *argv[]) {
int t = 0;
cin >> t;
while (t--) {
int n = 0, m = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
parent[i] = i;
tag[i] = true;
count[i] = 1;
}
char flag = '\0';
int x1 = 0, x2 = 0;
for (int i = 0; i < m; i++) {
cin >> flag >> x1 >> x2;
if (flag == 'D') {
aUnion(x1, x2);
} else {
int p1 = find(x1);
int p2 = find(x2);
if (p1 != p2) {
cout << "Not sure yet." << endl;
} else if (tag[x1] != tag[x2]) {
cout << "In different gangs." << endl;
} else if (tag[x1] == tag[x2]) {
cout << "In the same gang." << endl;
}
}
}
}
return 0;
}