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 ofedges. Each element ofedgesis a pair[u, v]that represents adirectededge connecting nodesuandv, whereuis a parent of childv.
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.
Analysis
Redundant Connection 那道题给的是无向图,只需要删掉组成环的最后一条边即可,归根到底就是检测环就行了。而这道题给我们的是有向图,那么整个就复杂多了,因为有多种情况存在 via @Grandyang
classSolution {classUnionFind {int[] parent;int[] rank;int size;publicUnionFind(int size) {this.size= size; parent =newint[size]; rank =newint[size];for (int i =0; i < size; i++) { parent[i] = i; } }publicintfind(int x) {if (parent[x] != x) { parent[x] =find(parent[x]); }return parent[x]; }// return true if x, y has same parentpublicbooleanunion(int x,int y) {int xp =find(x);int yp =find(y);if (xp == yp) {returnfalse; }if (rank[xp] > rank[yp]) { parent[yp] = xp; } elseif (rank[xp] < rank[yp]) { parent[xp] = yp; } else { parent[yp] = xp; rank[xp]++; }returntrue; } }publicint[] findRedundantDirectedConnection(int[][] edges) {HashSet<Integer> points =newHashSet<>();HashMap<Integer,Integer> parent =newHashMap<>();List<int[]> candidates =newArrayList<>();for (int[] edge: edges) {int src = edge[0];int dst = edge[1];points.add(src);points.add(dst);if (!parent.containsKey(dst)) {parent.put(dst, src);continue; }// if a node has two parents, add to candidates listcandidates.add(newint[] {parent.get(dst), dst});candidates.add(newint[] {src, dst});// invalidate the second edge edge[1] =-1; }UnionFind uf =newUnionFind(points.size());for (int[] edge: edges) {// skip invalidated edgeif (edge[1] ==-1) {continue; }int src = edge[0] -1;int dst = edge[1] -1;if (!uf.union(src, dst)) {// if we have invalidated the second edge// yet still have a cycle// we either just formed a cycle with this edge// or there still exists a node with two parents// which is the first edge stored in candidates.get(0)if (candidates.isEmpty()) {return edge; }returncandidates.get(0); } }// no cycle found, meaning the redundant edge // is the second edge in the candidates returncandidates.get(1); }}