685. Redundant Connection II
Asked at 1Google21
Problem
A rooted tree (edges point from parent to child, every node except the root has exactly one parent) on nodes 1…n got one extra directed edge. Given the edges, return an edge whose removal leaves a rooted tree; if several qualify, return the one that appears last in the input.
Example: [[1,2],[1,3],[2,3]] → [2,3]; [[1,2],[2,3],[3,4],[4,1],[1,5]] → [4,1]
A rooted tree plus one extra directed edge creates either (a) a node with two parents, (b) a cycle, or both. If some node has two parents, the answer is one of those two edges: skip the later one and check with Union-Find whether a cycle remains — if yes, the earlier edge is the culprit. Otherwise return the edge that closes the cycle.
Complexity: O(n · α(n)).
class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
n = len(edges)
parent_of, cand1, cand2 = {}, None, None
for u, v in edges:
if v in parent_of:
cand1, cand2 = [parent_of[v], v], [u, v]
else:
parent_of[v] = u
root = list(range(n + 1))
def find(x):
while root[x] != x:
root[x] = root[root[x]]
x = root[x]
return x
for u, v in edges:
if [u, v] == cand2:
continue
ru, rv = find(u), find(v)
if ru == rv: # cycle found
return cand1 if cand1 else [u, v]
root[rv] = ru
return cand2
assert Solution().findRedundantDirectedConnection([[1, 2], [1, 3], [2, 3]]) == [2, 3]
assert Solution().findRedundantDirectedConnection([[1, 2], [2, 3], [3, 4], [4, 1], [1, 5]]) == [4, 1]
assert Solution().findRedundantDirectedConnection([[2, 1], [3, 1], [4, 2], [1, 4]]) == [2, 1]
class Solution {
private int[] root;
private int find(int x) {
while (root[x] != x) { root[x] = root[root[x]]; x = root[x]; }
return x;
}
public int[] findRedundantDirectedConnection(int[][] edges) {
int n = edges.length;
int[] parentOf = new int[n + 1];
int[] cand1 = null, cand2 = null;
for (int[] e : edges) {
if (parentOf[e[1]] != 0) { cand1 = new int[]{parentOf[e[1]], e[1]}; cand2 = e; }
else parentOf[e[1]] = e[0];
}
root = new int[n + 1];
for (int i = 0; i <= n; i++) root[i] = i;
for (int[] e : edges) {
if (e == cand2) continue;
int ru = find(e[0]), rv = find(e[1]);
if (ru == rv) return cand1 != null ? cand1 : e;
root[rv] = ru;
}
return cand2;
}
}
func findRedundantDirectedConnection(edges [][]int) []int {
n := len(edges)
parentOf := make([]int, n+1) // 0 = no parent yet (nodes are 1..n)
var cand1, cand2 []int // the two edges into a node with two parents
for _, e := range edges {
u, v := e[0], e[1]
if parentOf[v] != 0 {
cand1, cand2 = []int{parentOf[v], v}, []int{u, v}
} else {
parentOf[v] = u
}
}
root := make([]int, n+1)
for i := range root {
root[i] = i
}
find := func(x int) int {
for root[x] != x {
root[x] = root[root[x]]
x = root[x]
}
return x
}
for _, e := range edges {
u, v := e[0], e[1]
if cand2 != nil && u == cand2[0] && v == cand2[1] {
continue // try the tree without the later candidate
}
ru, rv := find(u), find(v)
if ru == rv { // a cycle even so
if cand1 != nil {
return cand1
}
return []int{u, v}
}
root[rv] = ru
}
return cand2
}