Repository navigation
Expand file tree
/
Copy pathSolution3289.java
More file actions
58 lines (52 loc) · 1.65 KB
/
Copy pathSolution3289.java
File metadata and controls
58 lines (52 loc) · 1.65 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
package swea;
import java.util.*;
import java.io.*;
public class Solution3289 {
static int[] p;
public static int FindSet(int x) {
if (x == p[x])
return x;
return p[x] = FindSet(p[x]);
}
public static void Union(int x, int y) {
x = FindSet(x);
y = FindSet(y);
if (x == y)
return;
p[y] = x;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb;
int t = Integer.parseInt(br.readLine());
int n, m, a, b, c;
for (int j = 0; j < t; j++) {
st = new StringTokenizer(br.readLine(), " ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
p = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
p[i] = i;
}
sb = new StringBuilder();
sb.append("#").append(j+1).append(" ");
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine(), " ");
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
if (a == 0) {
Union(b, c);
} else {
if (FindSet(b) == FindSet(c)) {
sb.append(1);
} else {
sb.append(0);
}
}
}
System.out.println(sb);
}
}
}