<p>In this problem, a tree is an <strong>undirected graph</strong> that is connected and has no cycles.</p>

<p>You are given a graph that started as a tree with <code>n</code> nodes labeled from <code>1</code> to <code>n</code>, with one additional edge added. The added edge has two <strong>different</strong> vertices chosen from <code>1</code> to <code>n</code>, and was not an edge that already existed. The graph is represented as an array <code>edges</code> of length <code>n</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the graph.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.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/2021/05/02/reduntant1-2-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
<strong>Output:</strong> [1,4]
</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;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= edges.length</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
	<li>The given graph is connected.</li>
</ul>

// disjoint set

class Solution {
public:
    unordered_set<int> cycle; // will store all nodes of cycle
    int cycleStart = -1;      // used to mark start node of cycle
    vector<int> findRedundantConnection(vector<vector<int>>& e) {
        int n = size(e);
        vector<vector<int>> graph(n + 1);
        vector<bool> vis(n + 1);
        // constructing the graph from the edges
        for (auto& edge : e)
            graph[edge[0]].push_back(edge[1]),
                graph[edge[1]].push_back(edge[0]);
        dfs(graph, vis, 1); // dfs traveral to detect cycle and fill the those
                            // nodes in cycle set.
        for (int i = n - 1; ~i; i--)
            if (cycle.count(e[i][0]) && cycle.count(e[i][1]))
                return e[i]; // last edge of input having both nodes in cycle
        return {};           // un-reachable
    }
    void dfs(vector<vector<int>>& graph, vector<bool>& vis, int cur,
             int par = -1) {
        if (vis[cur]) {
            cycleStart = cur;
            return;
        } // reached an visited node - mark it as start of cycle and return
        vis[cur] = true; // not visited earlier - mark it as visited
        for (auto child :
             graph[cur]) { // iterate over child / adjacents of current node
            if (child == par)
                continue; // dont visit parent again - avoids back-and-forth
                          // loop
            if (cycle.empty())
                dfs(graph, vis, child, cur); // cycle not yet detected - explore
                                             // graph further with dfs
            if (cycleStart != -1)
                cycle.insert(cur); // cycle detected - keep pushing nodes till
                                   // we reach start of the cycle
            if (cur == cycleStart) {
                cycleStart = -1;
                return;
            } // all nodes of cycle taken - now just return
        }
    }
};
14071