-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1717.cpp
More file actions
54 lines (47 loc) · 808 Bytes
/
1717.cpp
File metadata and controls
54 lines (47 loc) · 808 Bytes
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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int arr[1000001];
int find(int a) {
if (arr[a] < 0)return a;
return find(arr[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)return;
if (arr[a] < arr[b]) {
arr[a] += arr[b];
arr[b] = a;
}
else {
arr[b] += arr[a];
arr[a] = b;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fill(&arr[0], &arr[1000001], -1);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int command, a, b;
cin >> command >> a >> b;
if (command == 0) {
merge(a, b);
}
else {
a = find(a);
b = find(b);
if (a == b) {
cout << "yes" << '\n';
}
else {
cout << "no" << '\n';
}
}
}
}