-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuickFind.java
More file actions
33 lines (30 loc) · 796 Bytes
/
QuickFind.java
File metadata and controls
33 lines (30 loc) · 796 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
public class QuickFind {
private int[] id;
public QuickFind(int N)
{
id = new int[N];
for (int i = 0; i < N; i++)
id[i] = i;
}
public boolean connected(int id1, int id2) {
return id[id1] == id[id2];
}
public void union(int id1, int id2) {
int id_1 = id[id1];
int id_2 = id[id2];
for (int i = 0; i < id.length; i++) {
if (id[i] == id_1) id[i] = id_2;
}
}
public static void main(String[] args) {
QuickFind q = new QuickFind(10);
q.union(3, 8);
q.union(5, 2);
q.union(2, 3);
q.union(9, 1);
q.union(7, 4);
q.union(3, 9);
for (int i = 0; i < 10; i++)
System.out.print(q.id[i] + " ");
}
}