# 685     Redundant Connection II

## 685. [Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/description/)

## 1. Question

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.

## 2. Implementation

**(1) DFS**

思路: 有3种情况出现多余的边:

(1) 当没有cycle，当有duplicate parents的时候，我们根据题意返回**最后出现**的边

(2) 当没有duplicate parents, 有cycle的时候，返回形成cycle的那条边

(3) 当既有duplicate，又有cycle的时候，返回从duplicate parents到当前节点形成cycle的那条边

1. 所以我们一步先找是否存在duplicate parents,如果存在，将两个duplicate parents到当前节点边分别存在res1和res2中，其中res2始终是存形成duplicate parents的边中最后出现的那条。同时将当前形成duplicate parent的边删除，这点很关键，因为这让我们在第二步中可以正确的查找cycle是否存在
2. 第二步则是查找cycle是否存在，如果没有则返回res2, 如果存在cycle而没有发现duplicate parent的话，我们返回当前的边，如果存在cycle而同时有duplicate parents的话，我们返回res1，原因是第一步中每当我们发现有duplicate parents时，我们都把第二条形成duplicate parents的边删除，如果此时我们发现有cycle，这说明构成cycle的必然形成duplicate parents的第一条边

```java
class Solution {
    public int[] findRedundantDirectedConnection(int[][] edges) {
        int[] res1 = {-1, -1};
        int[] res2 = {-1, -1};

        int n = edges.length;
        int[] parents = new int[n + 1];
        boolean hasDuplicateParents = false;

        for (int[] edge : edges) {
            int u = edge[0];
            int v = edge[1];

            // Find duplicate parents for node v
            if (parents[v] > 0) {
                res1[0] = parents[v];
                res1[1] = v;

                // res2 stores the extra edge that occurs LAST
                res2[0] = u;
                res2[1] = v;

                // Remove the edge that cause duplicate parents
                // If later on we still find cycle in the next step, we should return res1
                edge[0] = -1;
                edge[1] = -1;

                hasDuplicateParents = true;
            }
            else {
                parents[v] = u;
            }
        }

        parents = new int[n + 1];


        // Check if there is a cycle
        for (int[] edge : edges) {
            int u = edge[0];
            int v = edge[1];

            if (u < 0 || v < 0) {
                continue;
            }

            parents[v] = u;

            if (hasCycle(v, parents)) {
                return hasDuplicateParents ? res1 : edge;
            }
        }
        return res2;
    }

    public boolean hasCycle(int node, int[] parents) {
        int parent = parents[node];

        while (parent != 0) {
            if (node == parent) {
                return true;
            }
            parent = parents[parent];
        }
        return false;
    }
}
```

**(2) Union Find**

思路: 这里唯一的区别就是用Union Find查找cycle

```java
class Solution {
    public int[] findRedundantDirectedConnection(int[][] edges) {
        int[] res1 = new int[] {-1, -1};
        int[] res2 = new int[] {-1, -1};
        int n = edges.length;
        int[] parent = new int[n + 1];

        boolean hasDuplicateParents = false;

        // Step 1: Check if there is duplicate parent
        for (int[] edge : edges) {
            // Duplicate parent is found
            if (parent[edge[1]] > 0) {

                res1 = new int[] {parent[edge[1]], edge[1]};
                res2 = new int[] {edge[0], edge[1]};

                hasDuplicateParents = true;

                // Delete the current edge
                // So that we can find cycle correctly in Step 2
                edge[0] = -1;
                edge[1] = -1;
            }
            else {
                parent[edge[1]] = edge[0];
            }
        }

        // Step 2: Use Union Find to find cycle, same as #684
        // Here we don't consider the graph is directional
        UnionFind uf = new UnionFind(n);

        for (int[] edge : edges) {
            // Skip deleted edge
            if (edge[0] < 0 || edge[1] < 0) continue;

            // If Cycle is found and no duplicate parent, return current edge
            // Otherwise, return the edge that causes duplicate parent
            if (!uf.union(edge[1], edge[0])) {
                return hasDuplicateParents ? res1 : edge;
            }
        }
        return res2;
    }

    class UnionFind {
        int[] sets;
        int[] size;

        public UnionFind(int n) {
            sets = new int[n + 1];
            size = new int[n + 1];

            for (int i = 1; i <= n; i++) {
                sets[i] = i;
                size[i] = 1;
            }
        }

        public int find(int node) {
            while (node != sets[node]) {
                node = sets[node];
            }
            return node;
        }

        public boolean union(int i, int j) {
            int node1 = find(i);
            int node2 = find(j);

            // Find Cycle
            if (node1 == node2) {
                return false;
            }

            if (size[node1] < size[node2]) {
                sets[node1] = node2;
                size[node2] += size[node1];
            }
            else {
                sets[node2] = node1;
                size[node1] += size[node2];
            }
            return true;
        }
    }
}
```

## 3. Time & Space Complexity

**DFS**: 时间复杂度O(n^2),n是node的个数，空间复杂度O(n)

**Union Find**: 时间复杂度O(nlogn), n为edge的个数，union()的时间复杂度是O(logn) 但实际上是接近常数的，所以总的时间复杂度可以看成是O(n), 空间复杂度O(n)
