-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuickFind.java
45 lines (38 loc) · 991 Bytes
/
QuickFind.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
42
43
class QuickFind {
int[] id;
public QuickFind(int n) {
id = new int[n];
for (int i = 0; i < id.length; ++i) {
id[i] = i;
}
}
public boolean connected(int p, int q) {
return id[p] == id[q];
}
public int get(int p) {
return id[p];
}
public void union(int p, int q) {
int pid = id[p];
int qid = id[q];
for (int i = 0; i < id.length; ++i) {
if (id[i] == pid) {
id[i] = qid;
}
}
}
public static void main(String[] args) {
QuickFind set = new QuickFind(10);
set.union(4, 3);
set.union(3, 8);
set.union(6, 5);
set.union(9, 4);
System.out.println(set.connected(3, 9));
System.out.println(set.get(8));
set.union(2, 1);
set.union(8, 9);
System.out.println(set.connected(1, 0));
System.out.println(set.get(0));
set.union(5, 0);
}
}