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

#include <ranges>

class Solution {
public:
    using _map_ = std::unordered_map<int, std::vector<int>>;
    using _set_ = std::unordered_set<int>;
    using _queue_ = std::deque<int>;

    void dfs(vector<vector<int>>& edges, _map_ const& adjList,int node, int parent, _set_&  visited, _queue_ resStk, std::vector<int>& result)
    {
        std::cout<<"DFS:"<<node<<",";
        if(result.size()) return;
        if(std::ranges::find(resStk, node) != resStk.end())
        {
            while(resStk.front() != node){
                resStk.pop_front();
            }

            std::cout<<"Cycle at:"<<node<<std::endl;
            for(auto x : resStk) 
            std::cout<<x<<","<<std::endl;
            for(auto edge : edges | std::views::reverse){
                if(std::ranges::find(resStk, edge[0]) != resStk.end() && std::ranges::find(resStk, edge[1]) != resStk.end())
                {
                    result.push_back(edge[0]);
                    result.push_back(edge[1]);
                    std::cout<<edge[0]<<"->"<<edge[1]<<std::endl;
                    return;
                }
            }
        }

        if(visited.contains(node)) return;
        
        visited.insert(node);
        resStk.push_back(node);

        if(adjList.contains(node)){
            for(auto next : adjList.at(node)){
                if(next == parent) continue;
                dfs(edges, adjList, next, node, visited, resStk, result);
            }
        }
        visited.erase(node);
    }

    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        
        // create adjlist
        _map_ adjList;
        for(auto edge : edges){
            adjList[edge[0]].push_back(edge[1]);
            adjList[edge[1]].push_back(edge[0]);
        }

        _set_ visited;
        std::vector<int> result;
        dfs(edges, adjList, adjList.begin()->first, adjList.begin()->first, visited, {}, result);
        return result;
    }
};
20423