Problem:
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.
Idea:
Union Find
Time complexity: O(nlog*n) ~ O(n)
Space complexity: O(n)
Solution:
C++
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
// Author: Huahua // Runtime: 6 ms class Solution { public: vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) { vector<int> parents(edges.size() + 1, 0); vector<int> roots(edges.size() + 1, 0); vector<int> sizes(edges.size() + 1, 1); vector<int> ans1; vector<int> ans2; for(auto& edge: edges) { int u = edge[0]; int v = edge[1]; // A node has two parents if (parents[v] > 0) { ans1 = {parents[v], v}; ans2 = edge; // Delete the later edge edge[0] = edge[1] = -1; } parents[v] = u; } for(const auto& edge: edges) { int u = edge[0]; int v = edge[1]; // Invalid edge (we deleted in step 1) if (u < 0 || v < 0) continue; if (!roots[u]) roots[u] = u; if (!roots[v]) roots[v] = v; int pu = find(u, roots); int pv = find(v, roots); // Both u and v are already in the tree if (pu == pv) return ans1.empty() ? edge : ans1; // Unoin, always merge smaller set (pv) to larger set (pu) if (sizes[pv] > sizes[pu]) swap(pu, pv); roots[pv] = pu; sizes[pu] += sizes[pv]; } return ans2; } private: int find(int node, vector<int>& roots) { while (roots[node] != node) { roots[node] = roots[roots[node]]; node = roots[node]; } return node; } }; |
C++ / without using Union find
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 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Author: Huahua // Runtime: 6 ms class Solution { public: vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) { vector<int> parents(edges.size() + 1, 0); vector<int> ans1; vector<int> ans2; bool dup_parents = false; for(auto& edge: edges) { int u = edge[0]; int v = edge[1]; // A node has two parents if (parents[v] > 0) { ans1 = {parents[v], v}; ans2 = edge; dup_parents = true; // Delete the later edge edge[0] = edge[1] = -1; } else { parents[v] = u; } } // Reset parents parents = vector<int>(edges.size() + 1, 0); for(const auto& edge: edges) { int u = edge[0]; int v = edge[1]; // Invalid edge (we deleted in step 1) if (u < 0 || v < 0) continue; parents[v] = u; if (cycle(v, parents)) return dup_parents ? ans1 : edge; } return ans2; } private: bool cycle(int v, const vector<int>& parents) { int u = parents[v]; while (u) { if (u == v) return true; u = parents[u]; } return false; } }; |
Python
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 |
""" Author: Huahua Runtime: 62 ms """ class Solution: def findRedundantDirectedConnection(self, edges): def cycle(v, p): u = p[v] while u != 0: if u == v: return True u = p[u] return False n = len(edges) p = [0] * (n + 1) ans1 = [] ans2 = [] dup_p = False for e in edges: u, v = e if p[v] > 0: ans1 = [p[v], v] ans2 = [u, v] dup_p = True e[0] = e[1] = -1 else: p[v] = u p = [0] * (n + 1) for u, v in edges: if u < 0: continue p[v] = u if cycle(v, p): return ans1 if dup_p else [u, v] return ans2 |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment