-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKATunionfind.cpp
91 lines (79 loc) · 1.9 KB
/
KATunionfind.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define mp make_pair
#define pb push_back
#define f first
#define s second
struct disjoint_set {
int n;
vi parent, size;
disjoint_set(int N) : n(N), size(N), parent(N) {
}
void make_set(int v) {
parent[v] = v;
size[v] = 1;
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void make_union(int a, int b) {
a = find_set(a);
b = find_set(b);
if (b != a) {
if (size[a] < size[b]) swap(a, b);
parent[b] = a;
size[a] += size[b];
size[b] = 0;
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
disjoint_set dsu(n);
for (int i = 0; i < n; i++) dsu.make_set(i);
// literally pure dsu
char a; int u, v;
while (q--) {
cin >> a >> u >> v;
if (a == '?') {
if (dsu.find_set(u) == dsu.find_set(v)) cout << "yes\n";
else cout << "no\n";
}
else {
dsu.make_union(u, v);
}
}
}
/*
USE LONG LONG!!!!
.= , =.
_ _ /'/ )\,/,/(_ \ \
`//-.| ( ,\\)\//\)\/_ ) |
//___\ `\\\/\\/\/\\///' /
,-"~`-._ `"--'_ `"""` _ \`'"~-,_
\ `-. '_`. .'_` \ ,-"~`/
`.__.-'`/ (-\ /-) |-.__,'
|| | \O) /^\ (O/ | . <- BESSIE THE COW
`\\ | / `\ /
\\ \ / `\ /
`\\ `-. /' .---.--.\
`\\/`~(, '() ('
/(O) \\ _,.-.,_)
// \\ `\'` /
/ | || `""""~"`
/' |__||
`o
*/