-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3289 UnF.cpp
More file actions
50 lines (40 loc) · 997 Bytes
/
3289 UnF.cpp
File metadata and controls
50 lines (40 loc) · 997 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
#include <iostream>
#include <stdio.h>
using namespace std;
int find(int x, int arr[]) {
if (x == arr[x]) return x;
return arr[x] = find(arr[x], arr);
};
void addunion(int x, int y, int arr[]) {
if (find(x, arr) != find(y, arr)) {
arr[find(x, arr)] = find(y, arr);
}
};
int checkunion(int x, int y, int arr[]) {
if (find(x, arr) == find(y, arr)) return 1;
return 0;
};
int unions[1000005];
int main() {
int tc;
cin >> tc;
for (int ro = 1; ro <= tc; ro++) {
int N, M;
cin >> N >> M;
cout << '#' << ro << ' ';
for (int idx=0; idx <= N; idx++) {
unions[idx] = idx;
}
for (int ca=0; ca < M; ca++) {
int c, x, y;
scanf("%d %d %d", &c, &x, &y);
if (c == 1) {
printf("%d", checkunion(x, y, unions));
} else {
addunion(x, y, unions);
}
};
cout << endl;
};
return 0;
}