Formatted question description: https://leetcode.ca/all/685.html

685. Redundant Connection II

Level

Hard

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:

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.

Solution

If a connection is redundant, there may be two cases. The first case is that the connection causes a conflict, which means a node has more than one parent. The second case is that the connection introduces a cycle.

Maintain a global variable parents which is an array to store each node’s parent node. The number of nodes equals edges.length. Initialize parents[i] = i for each node i.

Loop over edges. For each edge in edges, if parent[edge[1]] != edge[1], then edge[1] is already a child of another node, so edge introduces a conflict, and store the current edge’s index as the conflict index. Otherwise, if neither a conflict connection nor a cycle connection is found, find the ancestors of edge[0] and edge[1]. If the two nodes’ ancestors are the same, then edge introduces a cycle, and store the current edge’s index as the cycle index.

If no conflict is found, then return the connection at the cycle index.

If there is a conflict found, then check whether the connection at the conflict index causes a cycle. If so, return the connection at the conflict index. Otherwise, return the other connection that has the same child as the connection at the conflict index.

class Solution {
    int[] parents;

    public int[] findRedundantDirectedConnection(int[][] edges) {
        int nodesCount = edges.length;
        parents = new int[nodesCount + 1];
        for (int i = 1; i <= nodesCount; i++)
            parents[i] = i;
        int conflict = -1;
        int cycle = -1;
        for (int i = 0; i < nodesCount; i++) {
            int[] edge = edges[i];
            int node1 = edge[0], node2 = edge[1];
            if (parents[node2] != node2)
                conflict = i;
            else {
                if (conflict < 0 && cycle < 0) {
                    int ancestor1 = findAncestor(node1);
                    int ancestor2 = findAncestor(node2);
                    if (ancestor1 == ancestor2)
                        cycle = i;
                }
                parents[node2] = node1;
            }
        }
        if (conflict < 0) {
            int[] redundant = {edges[cycle][0], edges[cycle][1]};
            return redundant;
        }
        int[] conflictEdge = edges[conflict];
        if (containsCycle(conflictEdge[1])) {
            int[] redundant = new int[2];
            redundant[1] = conflictEdge[1];
            redundant[0] = parents[conflictEdge[1]];
            return redundant;
        } else {
            int[] redundant = {conflictEdge[0], conflictEdge[1]};
            return redundant;
        }
    }

    public boolean containsCycle(int node) {
        int tempNode = node;
        while (parents[node] != node) {
            if (parents[node] == tempNode)
                return true;
            node = parents[node];
        }
        return false;
    }

    public int findAncestor(int node) {
        while (parents[node] != node)
            node = parents[node];
        return node;
    }
}

All Problems

All Solutions