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

class Solution {
public:
    int st,k=1;
    bool detect(int node,int parent,vector<vector<int>>&graph,vector<int>&vis, vector<vector<int>>&cycle)
    {
        vis[node]=1;
        for(auto it:graph[node])
        {
            if(!vis[it])
            {
                if(detect(it,node,graph,vis,cycle))
                {
                    if(k)cycle.push_back({it,node});
                    if(node==st)k=0;
                    return true;
                }
            }
            else if(parent!=it)
            {
                st=it;
                cycle.push_back({it,node});
                return true;
            }
        }
        return false;
    }
    vector<int> findRedundantConnection(vector<vector<int>>& edges) 
    {
        int n=edges.size();
        vector<vector<int>>graph(n+1);
        map<pair<int,int>,int>mpp;
        int k=1;
        for(auto it:edges)
        {
            graph[it[0]].push_back(it[1]);
            graph[it[1]].push_back(it[0]); 
            mpp[{it[0],it[1]}]=k; 
            mpp[{it[1],it[0]}]=k;
            k+=1;
        }
        vector<int>vis(n+1,0);
        vector<int>answer;
        vector<vector<int>>cycle;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                if(detect(i,-1,graph,vis,cycle) && cycle.size())
                {
                    answer=cycle[0];
                    for(int l=1;l<cycle.size()-1;l++)
                    {
                        pair<int,int>a={cycle[l][0],cycle[l][1]};
                        pair<int,int>b={answer[0],answer[1]};
                        if(mpp[a]>mpp[b]) 
                        {
                            answer=cycle[l];
                        }  
                    }
                    if(answer[0]>answer[1])return {answer[1],answer[0]};
                    return answer;
                }
            }
        }
        return answer;
    }
};
16188