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




https://www.youtube.com/watch?v=lnmJT5b4NlM

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



Parent node definition : https://www.google.com/search?q=tree+structure+with+two+parents&num=100&rlz=1C5CHFA_enUS766US767&source=lnms&tbm=isch&sa=X&ved=0ahUKEwj-kcDe9ILeAhUJyFkKHYHFBXYQ_AUIDigB&biw=1125&bih=696#imgrc=UOcuQ69eUN2AJM:



// other’s code, I don’t understand 
// need to understand the logic and follow thru a case to understand the code and the logic better 
class Solution {
    public int[] findRedundantDirectedConnection(int[][] edges) {
        int[] can1 = {-1, -1};
        int[] can2 = {-1, -1};
        int[] parent = new int[edges.length + 1];
        for (int i = 0; i < edges.length; i++) {
            if (parent[edges[i][1]] == 0) {
                parent[edges[i][1]] = edges[i][0];
            } else {
                can2 = new int[] {edges[i][0], edges[i][1]};
                can1 = new int[] {parent[edges[i][1]], edges[i][1]};
                edges[i][1] = 0;
            }
        }
   //      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.
        
        // set parent itself as its parent (initialization)
        for (int i = 0; i < edges.length; i++) {
            parent[i] = i;
        }
        // if invalid edge, do nothing. continue
        for (int i = 0; i < edges.length; i++) {
            if (edges[i][1] == 0) {
                continue;
            }
            int child = edges[i][1];
            int father = edges[i][0];
            if (root(parent, father) == child) {
                if (can1[0] == -1) {
                    return edges[i];
                }
                return can1;
            }
            parent[child] = father;
        }
        return can2;
    }
    
    private int root(int[] parent, int i) {
        while (i != parent[i]) {
            parent[i] = parent[parent[i]];
            i = parent[i];
        }   
        return i;
    }
}

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9933028.html