Problem description:

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 1, 2, …, 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 [u, v] that represents a directed edge connecting nodes u and v, where u is a parent of child v.

Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.

Example 1:

1
2
3
4
5
6
7
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given directed graph will be like this:
1
/ \
v v
2-->3

Example 2:

1
2
3
4
5
6
7
Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
Output: [4,1]
Explanation: The given directed graph will be like this:
5 <- 1 -> 2
^ |
| v
4 <- 3

Note:
The size of the input 2D-array will be between 3 and 1000.
Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

Solution:

  1. no cycle, but there’s a node have two parents

    1
    2
    3
    4
    5
    6
    [[1,2], [1,3], [2,3]]

    1
    / \
    v v
    2-->3
  2. have cycle, every node only have one parent

    1
    2
    3
    4
    5
    6
    [[1,2], [2,3], [3,4], [4,1], [1,5]]

    5 <- 1 -> 2
    ^ |
    | v
    4 <- 3
  3. have cycle, there’s one node have two parents

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [[1,2],[2,3],[3,1],[1,4]]

    4
    /
    v
    1
    / ^
    v \
    2 -->3

1) Check whether there is a node having two parents.
If so, store them as candidates A and B, and set the second edge invalid.
2) Perform normal union find.
If the tree is now valid
simply return candidate B
else if candidates not existing
we find a circle, return current edge;
else
remove candidate A instead of B.

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
class Solution {
public:
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> parent(n+1, 0), candA, candB;
// step 1, check whether there is a node with two parents
for (auto &edge:edges) {
if (parent[edge[1]] == 0)
parent[edge[1]] = edge[0];
else {
candA = {parent[edge[1]], edge[1]};
candB = edge;
edge[1] = 0;
}
}
// step 2, union find
for (int i = 1; i <= n; i++) parent[i] = i;
for (auto &edge:edges) {
if (edge[1] == 0) continue;
int u = edge[0], v = edge[1], pu = root(parent, u);
// Now every node only has 1 parent, so root of v is implicitly v
if (pu == v) {
if (candA.empty()) return edge;
return candA;
}
parent[v] = pu;
}
return candB;
}
private:
int root(vector<int>& parent, int k) {
if (parent[k] != k)
parent[k] = root(parent, parent[k]);
return parent[k];
}
};

time complexity: $O()$
space complexity: $O()$
reference: