-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
57 lines (47 loc) · 1.04 KB
/
UnionFind.java
File metadata and controls
57 lines (47 loc) · 1.04 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
class UnionFind {
BufferedWriter bw;
long[] T;
int N, M, K;
int MAX = (int) 1e9;
int TN;
int[] A;
int[] P;
UnionFind() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
A = new int[N];
P = new int[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
A[i] = n;
}
bw.flush();
bw.close();
}
private boolean union(int a, int b) {
int ap = parent(a);
int bp = parent(b);
if (ap != bp) {
P[bp] = ap;
return false;
}
return true;
}
private int parent(int x) {
if (x == P[x]) {
return x;
}
P[x] = parent(P[x]);
return P[x];
}
}