-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUF.java
More file actions
93 lines (69 loc) · 2.24 KB
/
UF.java
File metadata and controls
93 lines (69 loc) · 2.24 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//Weighted quick-union by rank with path compression by halving.
import java.util.Scanner;
public class UF {
private int [] id; // id[i] = parent of i
private byte[] rank; // rank[i] = rank of subtree rooted at i (cannot be more than 31)
private int count; // number of components
/**
* Initializes an empty union-find data structure with <tt>N</tt>
* isolated components <tt>0</tt> through <tt>N-1</tt>
* @throws java.lang.IllegalArgumentException if <tt>N < 0</tt>
* @param N the number of sites
*/
public UF(int N) {
if (N < 0) throw new IllegalArgumentException();
count = N;
id = new int[N];
rank = new byte[N];
for(int i = 0; i < N; i++ ){
id[i] = i;
rank[i] = 0;
}
}
// Returns the number of components.
public int count() { return count; }
//Are the two sites p and q in the same component
public boolean connected(int p, int q) {
return find(p) == find(q);
}
/**
* Returns the component identifier for the component containing site <tt>p</tt>.
* @param p the integer representing one object
* @return the component identifier for the component containing site <tt>p</tt>
* @throws java.lang.IndexOutOfBoundsException unless <tt>0 ≤ p < N</tt>
*/
public int find(int p) {
if( p < 0 || p >= id.length) throw new IndexOutOfBoundsException();
while(p != id[p]){
id[p] = id[id[p]]; // path compression by halving
p = id[p];
}
return p;
}
public void union(int p, int q) {
int i = find(p);
int j = find(q);
if (i == j) return;
// make root of smaller rank point to root of larger rank
if (rank[i] < rank[j]) id[i] = j;
else if (rank[i] > rank[j]) id[j] = i;
else {
id[j] = i;
rank[i]++;
}
count--;
}
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
UF uf = new UF(N);
while(scan.hasNext()) {
int p = scan.nextInt();
int q = scan.nextInt();
if(uf.connected(p, q)) continue;
uf.union(p, q);
System.out.println(p + " " + q);
}
System.out.println(uf.count() + " components");
}
}