<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:
    bool isCycle(vector<vector<int>>& adjl, vector<int> &path, vector<bool> &visited, int x, int p){
        visited[x] = true;
        path.push_back(x);

        for (int n: adjl[x]){
            if (n==p)
                continue;
            if (!visited[n]){
                if (isCycle(adjl, path, visited, n, x)){
                    return true;
                }
            } else {
                path.push_back(n);
                return true;
            }
        }
        path.pop_back();
        return false;
    }

    vector<int> getCycle(vector<vector<int>>& edges){
        int n = edges.size();
        vector<int> c;

        vector<vector<int>> adjl(n);

        for (auto &e: edges){
            adjl[e[0]].push_back(e[1]);
            adjl[e[1]].push_back(e[0]);
        }

        vector<bool> visited(n, false);

        vector<int> path;
        isCycle(adjl, path, visited, 0, -1);

        for (int i=0; i<path.size(); i++){
            if (path[i] == path.back()){
                c = vector<int> (path.begin()+1+i, path.end());
                break;
            }
        }

        return c;
    }

    vector<int> findRedundantConnection(vector<vector<int>> &edges) {
        int n = edges.size();

        map<pair<int, int>, int> pos;
        set<pair<int, int>> present;

        for (int i=0; i<n; i++){
            auto &e = edges[i];
            e[0]--;
            e[1]--;
            pos[{e[0], e[1]}] = i;
            pos[{e[1], e[0]}] = i;
            present.insert({e[0], e[1]});
        } 

        auto c = getCycle(edges);

        // for (int i: c){
        //     cout << i << " ";
        // }
        // cout << "\n";

        int maxpos = pos[{c[0], c.back()}];
        vector<int> edge = {c[0], c.back()};

        for (int i=0; i<c.size()-1; i++){
            if (maxpos < pos[{c[i], c[i+1]}]){
                maxpos = pos[{c[i], c[i+1]}];
                if (present.find({c[i], c[i+1]}) == present.end()){
                    edge = {c[i+1]+1, c[i]+1};
                } else {
                    edge = {c[i]+1, c[i+1]+1};
                }
            }
        }

        return edge;
    }
};
14978