forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion-find.java
41 lines (39 loc) · 785 Bytes
/
union-find.java
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
class UnionFind {
public static void main(String[] args) {
int N = 10;
int arr = new int[N];
int size = new int[N];
init(arr, size, N);
}
public void init(int[] arr, int[] size, int N) {
count = N;
for (int i = 0; i < N; i++) {
arr[i] = i;
size[i] = 1;
}
}
public boolean find(int u, int v) {
if (root(u) == root(v)) {
return true;
} else {
return false;
}
}
public int root (int[] arr, int i) {
while (arr[i] != i) {
i = arr[i];
}
return i;
}
public void union(int[] arr, int[] size, int u, int v) {
int rootU = root(arr, u);
int rootV = root(arr, v);
if (size[rootU] < size[rootV]) {
arr[rootU] = arr[rootV];
size[rootV] += size[rootU];
} else {
arr[rootV] = arr[rootU];
size[rootU] += size[rootV];
}
}
}