<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 {
public:
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& a){
        int n = a.size();
        const int N = 1e5 + 5;
        vector<int> adj[N];
        int col[N], par[N], in[N];
        memset(in, 0, sizeof in);
        memset(col, 0, sizeof col);
        memset(par, -1, sizeof par);
        
        for(int i = 0; i < n; i++){
            adj[a[i][0]].push_back(a[i][1]);
            in[a[i][1]]++;
        }
        
        int node = -1;
        function<void(int, int)> dfs = [&](int u, int p){
            col[u] = 1; 
            par[u] = p;
            for(auto v : adj[u]){
                par[v] = u;
                if(col[v] == 1){
                    node = u; // u -> v
                }else{
                    dfs(v, u);
                }
            }
            col[u] = 2; 
        };
        
        for(int i = 1; i <= n; i++) {
            if(!col[i]) {
                dfs(i, -1);
            }
        }
        
        if(node == -1){ // when there is no cycle
            for(int i = n - 1; i >= 0; i--){
                if(in[a[i][1]] == 2) return a[i];
            }   
            return {};
        }


        // when there is cycle
        multiset<pair<int, int>> ms; // (u, v), u -> v
        ms.insert({par[node], node});
        int u = par[node];
        while(u != node && u != -1){
            ms.insert({par[u], u});
            u = par[u];
        }
        // one node has indegree 2, try to remove this one first
        for(int i = n - 1; i >= 0; i--){
            if(ms.find({a[i][0], a[i][1]}) != ms.end()){
                if(in[a[i][1]] == 2){
                    return a[i];
                }
            }
        }
        // all nodes has indegree 1
        for(int i = n - 1; i >= 0; i--){
            if(ms.find({a[i][0], a[i][1]}) != ms.end()){
                return a[i];
            }
        }
        return {}; // here we will never reach
    }
};

// class Solution {
// public:
//     vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
//         int n = edges.size();
//         vector<int> parent(n+1, 0), candA, candB;
//         // step 1, check whether there is a node with two parents
//         for (auto &edge:edges) {
//             if (parent[edge[1]] == 0)
//                 parent[edge[1]] = edge[0]; 
//             else {
//                 candA = {parent[edge[1]], edge[1]};
//                 candB = edge;
//                 edge[1] = 0;
//             }
//         } 
//         // step 2, union find
//         for (int i = 1; i <= n; i++) parent[i] = i;
//         for (auto &edge:edges) {
//             if (edge[1] == 0) continue;
//             int u = edge[0], v = edge[1], pu = root(parent, u);
//             // Now every node only has 1 parent, so root of v is implicitly v
//             if (pu == v) {
//                 if (candA.empty()) return edge;
//                 return candA;
//             }
//             parent[v] = pu;
//         }
//         return candB;
//     }
// private:
//     int root(vector<int>& parent, int k) {
//         if (parent[k] != k) 
//             parent[k] = root(parent, parent[k]);
//         return parent[k];
//     }
// };
18178