-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.js
44 lines (44 loc) · 1.08 KB
/
Solution.js
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
/**
* @param {number[][]} edges
* @return {number[]}
*/
var findRedundantDirectedConnection = function (edges) {
const n = edges.length;
const ind = Array(n).fill(0);
for (const [_, v] of edges) {
++ind[v - 1];
}
const dup = [];
for (let i = 0; i < n; ++i) {
if (ind[edges[i][1] - 1] === 2) {
dup.push(i);
}
}
const p = Array.from({ length: n }, (_, i) => i);
const find = x => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
if (dup.length) {
for (let i = 0; i < n; ++i) {
if (i === dup[1]) {
continue;
}
const [pu, pv] = [find(edges[i][0] - 1), find(edges[i][1] - 1)];
if (pu === pv) {
return edges[dup[0]];
}
p[pu] = pv;
}
return edges[dup[1]];
}
for (let i = 0; ; ++i) {
const [pu, pv] = [find(edges[i][0] - 1), find(edges[i][1] - 1)];
if (pu === pv) {
return edges[i];
}
p[pu] = pv;
}
};