-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1319.java
35 lines (31 loc) · 811 Bytes
/
1319.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
class Solution {
int[] parent;
public int makeConnected(int n, int[][] connections) {
parent = new int[n];
int res = 0;
if (connections.length < n-1) return -1;
for (int i = 0; i < n; i++) {
parent[i] = i;
}
for (int[] edge: connections) {
union(edge[0], edge[1]);
}
for (int i = 1; i < n; i++) {
if (find(i) != find(0)) {
union(i, 0);
res++;
}
}
return res;
}
private int find(int x) {
if (parent[x] == x) return x;
parent[x] = find(parent[x]);
return parent[x];
}
private void union(int x, int y) {
int px = find(x);
int py = find(y);
if (px != py) parent[py] = px;
}
}