-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4238.cpp
More file actions
executable file
·63 lines (57 loc) · 1.21 KB
/
Copy path4238.cpp
File metadata and controls
executable file
·63 lines (57 loc) · 1.21 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
/**
* 并查集 + 路径压缩模板题
**/
#include <iostream>
#include <cstdio>
const int MAXN = 2e5 + 233;
int fa[MAXN], n, m, op, x, y;
bool hide[MAXN];
void read(int &x){
x = 0;
char ch;
while (ch = getchar(), (ch < '0' || ch > '9'));
x = ch - '0';
while(ch = getchar(), ch >= '0' && ch <= '9') x = 10 * x + ch - '0';
}
int init() {
for(int i = 1;i <= n;++ i)
fa[i] = i;
}
int find(int x) {
int son = x;
// 路径压缩迭代写法
// if(fa[x] == x) return x;
// else return fa[x] = find(fa[x]);
// 路径压缩循环写法
while(fa[x] != x) {
x = fa[x];
}
while(son != x) {
int tmp = fa[son];
fa[son] = x;
son = tmp;
}
return x;
}
int bind(int x, int y) {
fa[find(x)] = find(y);
}
int main() {
read(n); read(m);
init();
for(int i = 0;i < m;++ i) {
read(op);
if(op == 1) {
read(x); read(y);
bind(x, y);
} else if(op == 2) {
read(x); read(y);
if(hide[x] | hide[y]) printf("NO\n");
else if(find(x) != find(y)) printf("NO\n");
else printf("YES\n");
} else {
read(x);
hide[x] = 1;
}
}
}