<p>In this problem, a rooted tree is a <b>directed</b> 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.</p>

<p>The given input is a directed graph that started as a rooted tree with <code>n</code> nodes (with distinct values from <code>1</code> to <code>n</code>), with one additional directed edge added. The added edge has two different vertices chosen from <code>1</code> to <code>n</code>, and was not an edge that already existed.</p>

<p>The resulting graph is given as a 2D-array of <code>edges</code>. Each element of <code>edges</code> is a pair <code>[u<sub>i</sub>, v<sub>i</sub>]</code> that represents a <b>directed</b> edge connecting nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, where <code>u<sub>i</sub></code> is a parent of child <code>v<sub>i</sub></code>.</p>

<p>Return <em>an edge that can be removed so that the resulting graph is a rooted tree of</em> <code>n</code> <em>nodes</em>. If there are multiple answers, return the answer that occurs last in the given 2D-array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph1.jpg" style="width: 222px; height: 222px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[1,3],[2,3]]
<strong>Output:</strong> [2,3]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph2.jpg" style="width: 222px; height: 382px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
<strong>Output:</strong> [4,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == edges.length</code></li>
	<li><code>3 &lt;= n &lt;= 1000</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
	<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
</ul>

class Solution:
    def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
        parent = collections.defaultdict(int)
        rank = collections.defaultdict(int)

        def find(x):
            # x is not present then assign x is its own parent
            #initial rank 0
            if x not in parent:
                parent[x] = x
                rank[x] = 0
            #find parent's parents untill parent has no parent other than itelf
            if parent[x] != x:
                parent[x] = find(parent[x])
            print("parent[x]", x, parent[x])
            return parent[x]

        def union(x, y):
            #find parent of x and y
            parent_x = find(x)
            parent_y = find(y)
            print("x, parent_x", x, parent_x)
            print("y, parent_y", y, parent_y)


            #check if parents are same
            if parent_x == parent_y:
                return False
            #assign parent with higher ranking
            if rank[parent_x] < rank[parent_y]:
                parent[parent_x] = parent_y
            else:
                parent[parent_y] = parent_x
                if rank[parent_x] == rank[parent_y]:
                    rank[parent_x] += 1
            return True
        print("parent", parent)
        print("rank", rank)
        parent1 = None 
        parent2 = None
        point_to = {}
        for x, y in edges:
            if y in point_to: 
                #we found y with 2 parents
                parent1 = point_to[y]
                parent2 = [x, y]
                break
            point_to[y] = [x, y]
        print("parent1", parent1)
        print("parent2", parent2)
        print("point to", point_to)
        #print(cand1, cand2, point_to)  
        for x, y in edges:
            if [x, y] == parent2: continue
            if not union(x+4, y+4):
                if parent1: 
                    print("case 3", x, y)
                    return parent1 #case III
               # print("case 2")
                return [x, y] #case II
        #print("case 1")
        return parent2 #case I
                
            
            
17500