# [LeetCode] 685. Redundant Connection II 冗余的连接之二

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:

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:

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.

[[1,2], [1,3], [2,3]]

  1
/ \
v   v
2-->3

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

5 <- 1 -> 2
^    |
|    v
4 <- 3

[[1,2], [2,3], [3,1], [4,1]]

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

class Solution {
public:
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> root(n + 1), first, second;
for (auto& edge : edges) {
if (root[edge[1]] == 0) {
root[edge[1]] = edge[0];
} else {
first = {root[edge[1]], edge[1]};
second = edge;
edge[1] = 0;
}
}
for (int i = 0; i <= n; ++i) root[i] = i;
for (auto& edge : edges) {
if (edge[1] == 0) continue;
int x = getRoot(root, edge[0]), y = getRoot(root, edge[1]);
if (x == y) return first.empty() ? edge : first;
root[x] = y;
}
return second;
}
int getRoot(vector<int>& root, int i) {
return i == root[i] ? i : getRoot(root, root[i]);
}
};

int getRoot(vector<int>& root, int i) {
while (i != root[i]) {
root[i] = root[root[i]];
i = root[i];
}
return i;
}

int getRoot(vector<int>& root, int i) {
while (i != root[i]) i = root[i];
return i;
}

Github 同步地址：

https://github.com/grandyang/leetcode/issues/685

Redundant Connection

Friend Circles

Accounts Merge

Number of Islands II

Graph Valid Tree

Number of Connected Components in an Undirected Graph

Similar String Groups

https://leetcode.com/problems/redundant-connection-ii/

https://leetcode.com/problems/redundant-connection-ii/discuss/108045/C++Java-Union-Find-with-explanation-O(n)

https://leetcode.com/problems/redundant-connection-ii/discuss/108058/one-pass-disjoint-set-solution-with-explain

LeetCode All in One 题目讲解汇总(持续更新中...)

 微信打赏 Venmo 打赏
posted @ 2018-02-12 22:29  Grandyang  阅读(8919)  评论(10编辑  收藏  举报