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

    vector<int> order;
    int cycleStart,cycleEnd;
    bool ans=false;
    void detectCycle(map<int,vector<int>> &mp, vector<int> &visited, int i, int parent) {
        visited[i]=1;

        for(int j: mp[i]) {
            if(j==parent) {
                continue;
            }
            else if(visited[j]==1) {
                ans=true;
                cycleStart=i;
                cycleEnd=j; 
                return;
            }
            else {
                if(!ans) {
                    order.push_back(j);
                    detectCycle(mp,visited,j,i);
                }
                if(!ans)
                    order.pop_back();
            }
        }
    }

    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
       int i;
       map<int,vector<int>> mp;
       int vertices=0;

       for(i=0;i<edges.size();i++) {
        mp[edges[i][0]-1].push_back(edges[i][1]-1);
        mp[edges[i][1]-1].push_back(edges[i][0]-1);
        vertices=max(vertices,max(edges[i][0],edges[i][1]));
       } 
       vector<int> visited(vertices,0);

        order.push_back(0);
        detectCycle(mp,visited,0,-1);
        // cout<<cycleStart<<" "<<cycleEnd;
        // for(int x : order) {
        //     cout<<" x="<<x<<" ";
        // }
        set<int> st;
        int flag=0;
        for(i=0;i<order.size();i++) {
            if(order[i]==cycleStart) {
                st.insert(order[i]);
                flag=1;
            }
            else if(flag==1) {
                st.insert(order[i]);
                if(order[i]==cycleEnd)
                    break;
            }
        }
        for(i=edges.size()-1;i>=0;i--) {
            int uu=edges[i][0]-1;
            int vv=edges[i][1]-1;

            if(st.find(uu)!=st.end() && st.find(vv)!=st.end())
                return edges[i];
        }
        vector<int> tmp;
        return tmp;
    }   
};
14071