In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
The given input is a directed graph that started as a rooted tree with n
nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.
The resulting graph is given as a 2D-array of edges. Each element of edges
is a pair [ui, vi] that represents a directed edge connecting nodes ui
and vi, where ui is a parent of child vi.
Return an edge that can be removed so that the resulting graph is a rooted tree ofnnodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
There are two possible issues: a node with two parents, or a cycle. If a node has two parents, removing either incoming edge may fix the tree. If not, the problem is a cycle. We use union-find to detect cycles and track parents to find nodes with two parents.
classSolution {
public: vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> parent(n+1), candA, candB;
for (int i =1; i <= n; ++i) parent[i] = i;
vector<int> par(n+1, 0);
for (auto& e : edges) {
int u = e[0], v = e[1];
if (par[v] ==0) par[v] = u;
else { candA = {par[v], v}; candB = e; e[1] =0; }
}
for (int i =1; i <= n; ++i) parent[i] = i;
for (auto& e : edges) {
if (e[1] ==0) continue;
int u = e[0], v = e[1];
int pu = find(parent, u);
if (pu == v) return candA.empty() ? e : candA;
parent[v] = pu;
}
return candB;
}
intfind(vector<int>& p, int x) { return p[x] == x ? x : p[x] = find(p, p[x]); }
};
classSolution {
publicint[]findRedundantDirectedConnection(int[][] edges) {
int n = edges.length;
int[] parent =newint[n+1];
int[] par =newint[n+1];
int[] candA =null, candB =null;
for (int i = 1; i <= n; ++i) parent[i]= i;
for (int[] e : edges) {
int u = e[0], v = e[1];
if (par[v]== 0) par[v]= u;
else { candA =newint[]{par[v], v}; candB = e; e[1]= 0; }
}
for (int i = 1; i <= n; ++i) parent[i]= i;
for (int[] e : edges) {
if (e[1]== 0) continue;
int u = e[0], v = e[1];
int pu = find(parent, u);
if (pu == v) return candA ==null? e : candA;
parent[v]= pu;
}
return candB;
}
intfind(int[] p, int x) { return p[x]== x ? x : (p[x]= find(p, p[x])); }
}
classSolution {
funfindRedundantDirectedConnection(edges: Array<IntArray>): IntArray {
val n = edges.size
val parent = IntArray(n+1) { it }
val par = IntArray(n+1)
var candA: IntArray? = nullvar candB: IntArray? = nullfor (e in edges) {
val u = e[0]; val v = e[1]
if (par[v] ==0) par[v] = u
else { candA = intArrayOf(par[v], v); candB = e; e[1] = 0 }
}
for (i in1..n) parent[i] = i
for (e in edges) {
if (e[1] ==0) continueval u = e[0]; val v = e[1]
val pu = find(parent, u)
if (pu == v) return candA ?: e
parent[v] = pu
}
return candB!! }
funfind(p: IntArray, x: Int): Int = if (p[x] == x) x else { p[x] = find(p, p[x]); p[x] }
}
classSolution:
deffindRedundantDirectedConnection(self, edges: list[list[int]]) -> list[int]:
n = len(edges)
parent = list(range(n+1))
par = [0] * (n+1)
candA = candB =Nonefor u, v in edges:
if par[v] ==0:
par[v] = u
else:
candA = [par[v], v]
candB = [u, v]
# Mark this edge as removed v =0deffind(p, x):
if p[x] != x:
p[x] = find(p, p[x])
return p[x]
for i in range(1, n+1):
parent[i] = i
for e in edges:
if e[1] ==0:
continue u, v = e
pu = find(parent, u)
if pu == v:
return candA if candA else e
parent[v] = pu
return candB
impl Solution {
pubfnfind_redundant_directed_connection(edges: Vec<Vec<i32>>) -> Vec<i32> {
let n = edges.len();
letmut parent: Vec<i32>= (0..=n asi32).collect();
letmut par =vec![0; n+1];
letmut cand_a = None;
letmut cand_b = None;
letmut edges = edges.clone();
for e in edges.iter_mut() {
let u = e[0] asusize;
let v = e[1] asusize;
if par[v] ==0 {
par[v] = u asi32;
} else {
cand_a = Some(vec![par[v], v asi32]);
cand_b = Some(vec![u asi32, v asi32]);
e[1] =0;
}
}
for i in1..=n { parent[i] = i asi32; }
for e in edges.iter() {
if e[1] ==0 { continue; }
let u = e[0] asusize;
let v = e[1] asusize;
let pu = find(&mut parent, u asi32);
if pu == v asi32 {
return cand_a.clone().unwrap_or_else(||vec![e[0], e[1]]);
}
parent[v] = pu;
}
cand_b.unwrap()
}
}
fnfind(p: &mut Vec<i32>, x: i32) -> i32 {
if p[x asusize] != x {
p[x asusize] = find(p, p[x asusize]);
}
p[x asusize]
}