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.

public class Solution { public int[] FindRedundantDirectedConnection(int[][] edges) { /* Step 1. Build In and Out Degree map, detect if any any vertex with Multiple parents. Step 2. If we don't find vertex 'A' with multiple parents 2.1) Start at 'root' and detect any cycle with DFS, return the edge causing the cycle Step 3. If we find vertex 'A' with multiple parents 3.1) if out degree at vertex 'A' is > 0, i.e there is a edge going out of the 'A' vertex 3.1.1) Start at 'A' and detect any cycle with DFS, return the edge causing the cycle 3.2) if there is NO outgoing edge from vertex 'A' 3.2.1) Return the LAST edge that is ending at 'A' (i.e 'A' last parent) */ var vertexInDegree = new List<int>[edges.Length + 1]; var vertexOutDegree = new List<int>[edges.Length + 1]; for (int i = 0; i < edges.Length + 1; i++) vertexInDegree[i] = new List<int>(); for (int i = 0; i < edges.Length + 1; i++) vertexOutDegree[i] = new List<int>(); bool[] visited = new bool[edges.Length + 1]; // as vertex # starts with 1 int vertexWithMultipleParents = -1; foreach (var arr in edges) { vertexInDegree[arr[1]].Add(arr[0]); vertexOutDegree[arr[0]].Add(arr[1]); if (vertexInDegree[arr[1]].Count > 1) vertexWithMultipleParents = arr[1]; } if (vertexWithMultipleParents != -1) { //detect if there is out edges from Vertex with multiple parents //if yes, defect this edge cause a cycle, if yes, remove the first edge //else remove the end edge //start from vertex with multiple parents *********** int root = vertexWithMultipleParents; if (vertexOutDegree[root].Count >0) //if there is a out edge { Stack<KeyValuePair<int, int>> stack = new Stack<KeyValuePair<int, int>>(); stack.Push(new KeyValuePair<int, int>(root, -1)); while (stack.Any()) { var node = stack.Pop(); if (!visited[node.Key]) { visited[node.Key] = true; foreach (int c in vertexOutDegree[node.Key]) stack.Push(new KeyValuePair<int, int>(c, node.Key)); } else { //cycle detected return new int[] { node.Value, node.Key }; } } return new int[] { vertexInDegree[root][1], root }; } else //doesnt have any out degree, return the last one { return new int[] { vertexInDegree[root][1], root }; } } else { //if multple parent vertex NOT found, //then search for cycle, if found, then remove the edge causing the cycle //first edge's as start int root = edges[0][0]; Stack<KeyValuePair<int, int>> stack = new Stack<KeyValuePair<int, int>>(); stack.Push(new KeyValuePair<int, int>(root, -1)); while (stack.Any()) { var node = stack.Pop(); if (!visited[node.Key]) { visited[node.Key] = true; foreach (int c in vertexOutDegree[node.Key]) stack.Push(new KeyValuePair<int, int>(c, node.Key)); } else { //cycle detected return new int[] { node.Value, node.Key}; } } return null; } } }