-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path집합의 표현.cc
44 lines (33 loc) · 850 Bytes
/
집합의 표현.cc
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
#include <iostream>
#include <cstring>
using namespace std;
int n, m, a, b, cmd;
int parent[1000002];
int find(int node) {
if (node == parent[node]) return node;
return parent[node] = find(parent[node]);
}
void merge(int node1, int node2) {
int parentNode1 = find(node1);
int parentNode2 = find(node2);
if(parentNode1 == parentNode2) return;
parent[parentNode1] = parentNode2;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i=0; i <= n; i++) parent[i] = i;
for(int i=0; i < m; i++) {
cin >> cmd >> a >> b;
if(cmd == 0) {
merge(a, b);
}
else {
if (find(a) == find(b)) cout << "YES\n";
else cout << "NO\n";
}
}
return 0;
}