forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Operations to Make Network Connected.java
49 lines (48 loc) · 1.23 KB
/
Number of Operations to Make Network Connected.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
class Solution {
public int makeConnected(int n, int[][] connections) {
int m = connections.length;
if(m < n - 1) return -1;
UnionFind uf = new UnionFind(n);
for(int[] connection: connections){
uf.union(connection[0], connection[1]);
}
return uf.getIsolated();
}
}
class UnionFind{
int[] root;
int[] rank;
int isolated;
public UnionFind(int n){
root = new int[n];
rank = new int[n];
for(int i = 0; i < n; i++){
root[i] = i;
rank[i] = 1;
}
isolated = 0;
}
public int find(int x){
if(root[x] != x) root[x] = find(root[x]);
return root[x];
}
public void union(int x, int y){
int rootx = find(x);
int rooty = find(y);
if(rootx == rooty) return;
if(rank[rootx] > rank[rooty]){
root[rooty] = rootx;
}else if(rank[rootx] < rank[rooty]){
root[rootx] = rooty;
}else{
root[rootx] = rooty;
rank[rooty] += 1;
}
}
public int getIsolated(){
for(int i = 0; i < root.length; i ++){
if(root[i] == i) isolated++;
}
return isolated - 1;
}
}