<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>

from collections import defaultdict

class Solution:
    def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
        graph = defaultdict(lambda: set())
        num_incoming_edges = defaultdict(lambda: 0)

        for edge in edges:
            a, b = edge[0], edge[1]
            graph[a].add(b)
            num_incoming_edges[b] += 1

        def findCyclesDfs(root, curr, history, visited):
            if curr == root:
                return history
            if curr in visited:
                return
            visited.add(curr)
            for node in graph[curr]:
                if (curr, node) in non_cycle_edges:
                    return
                history.add((curr, node))
                result = findCyclesDfs(root, node, history, visited)
                if result is not None:
                    return result
                non_cycle_edges.add((curr, node))
                history.remove((curr, node))

        redundant_edge = None
        known_cycle_edges = None
        non_cycle_edges = set()

        for edge in edges:
            a, b = edge[0], edge[1]
            known_cycle_edges = findCyclesDfs(a, b, {(a, b)}, set()) if known_cycle_edges is None else known_cycle_edges

            if num_incoming_edges[b] > 1:
                redundant_edge = edge
            if known_cycle_edges and (a, b) in known_cycle_edges:
                if edge == redundant_edge:
                    return edge
                else:
                    redundant_edge = edge
        
        return redundant_edge

18500