-
Notifications
You must be signed in to change notification settings - Fork 0
/
WQUFWPC.java
75 lines (70 loc) · 1.81 KB
/
WQUFWPC.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
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
import java.util.Scanner;
/**
* Created by Paul on 5/3/2015.
*/
public class WQUFWPC {
private int count;
private int sz[];
private int id[];
public WQUFWPC(int N){
count = N;
sz = new int[N];
for(int i = 0; i < N; ++i) sz[i] = 1;
id = new int[N];
for(int i = 0; i < N; ++i) id[i] = i;
}
public int find(int p){ // 2lgN
int root = p;
while(root != id[root]) root = id[root]; // lgN
while(p != root) { // lgN
id[p] = root;
p = id[p];
}
return root;
}
public void union(int p, int q){
int i = find(p);
int j = find(q);
if(i == j) return;
if(sz[i] < sz[j]) {
id[i] = j;
sz[j] += sz[i];
}
else{
id[j] = i;
sz[i] += sz[j];
}
count--;
}
public int count(){
return count;
}
boolean connected(int p, int q){
return find(p) == find(q);
}
public static void main(){
try(Scanner sc = new Scanner(System.in)){
int N = sc.nextInt();
WQUFWPC uf = new WQUFWPC(N);
while(sc.hasNextInt()){
int p = sc.nextInt();
int q = sc.nextInt();
if(uf.connected(p, q)) continue;
uf.union(p, q);
System.out.println(p + " " + q);
}
System.out.println(uf.count() + " components.");
for(int i = 0; i < N; ++i){
System.out.print(i + " ");
}
System.out.println();
for(int i = 0; i < N; ++i){
System.out.print(uf.id[i] + " ");
}
System.out.println();
}
catch(Exception e){
e.printStackTrace();
}
}
}