-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
48 lines (44 loc) · 1.08 KB
/
Solution.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
/**
* Created by Inno Fang on 2018/3/28.
*/
/**
* 39 / 39 test cases passed.
* Status: Accepted
* Runtime: 5 ms
*/
class Solution {
private int[] store;
private int[] ranks;
private int find(int p) {
while (p != store[p]) {
p = store[p] = store[store[p]];
}
return p;
}
private boolean union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return false;
if (ranks[rootP] < ranks[rootQ]) store[rootP] = rootQ;
else {
store[rootQ] = rootP;
if (ranks[rootQ] == ranks[rootP]) ranks[rootP]++;
}
return true;
}
public int[] findRedundantConnection(int[][] edges) {
int num = edges.length * 2;
store = new int[num];
ranks = new int[num];
for (int i = 0; i < num; i++) {
store[i] = i;
ranks[i] = 1;
}
for (int[] edge : edges) {
if (!union(edge[0], edge[1])) {
return edge;
}
}
return new int[2];
}
}