forked from cheungYX/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectingGraph3.java
45 lines (41 loc) · 945 Bytes
/
ConnectingGraph3.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
public class ConnectingGraph3 {
private int[] father = null;
private int count;
private int find(int x) {
if (father[x] == x) {
return x;
}
return father[x] = find(father[x]);
}
/*
* @param n: An integer
*/public ConnectingGraph3(int n) {
// do intialization if necessary
father = new int[n + 1];
count = n;
for (int i = 1; i <= n; ++i) {
father[i] = i;
}
}
/*
* @param a: An integer
* @param b: An integer
* @return: nothing
*/
public void connect(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father[root_a] = root_b;
count --;
}
}
/*
* @return: An integer
*/
public int query() {
// write your code here
return count;
}
}