<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 {
private:
    // head -> (tail, priority)
    std::unordered_map<int, std::set<std::vector<int>>> adj_list {};
    std::set<std::vector<int>> setified_edges {};
    std::unordered_set<int> visited {};
    std::deque<std::vector<int>> result {};
    int cycle_head {};
    bool flag {true}; // enforce "write once" - locking

public:
    void dfs(int source, std::unordered_set<int>& seen, int parent, std::deque<std::vector<int>>& seen_edges) {
        visited.insert(source);
        seen.insert(source);
        // std::cout << source << '\n';

        for (auto vec : adj_list[source]) {
            int outNeighbor = vec[0];
            // std::cout << "Outneighbor: " << outNeighbor << '\n';
            int priority = vec[1];

            if (outNeighbor == parent) {
                continue;
            }

            if (seen.contains(outNeighbor)) { // don't include trivial cycle...
                if (flag) {
                    seen_edges.push_back({source, outNeighbor, priority});
                    result = seen_edges;
                    cycle_head = outNeighbor;
                    // Account for the "reverse"
                    flag = false;
                }
                return;
            }

            if (!visited.contains(outNeighbor)) {
                // std::cout << "Here" << '\n';
                seen_edges.push_back({source, outNeighbor, priority});

                dfs(outNeighbor, seen, source, seen_edges);

                seen_edges.pop_back();
            }
        }

        seen.erase(source);
    }

    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        // There is a cycle. Find the "rightmost" edge that participates in it...

        // 1. Build adj list
        for (auto edge : edges) {
            setified_edges.insert(edge);
        }

        for (int priority = 0; priority < edges.size(); priority++) {
            if (adj_list.contains(edges[priority][0])) {
                adj_list[edges[priority][0]].insert({edges[priority][1], priority});
            } else {
                adj_list[edges[priority][0]] = std::set<std::vector<int>> {{edges[priority][1], priority}};
            }

            if (adj_list.contains(edges[priority][1])) {
                adj_list[edges[priority][1]].insert({edges[priority][0], priority});
            } else {
                adj_list[edges[priority][1]] = std::set<std::vector<int>> {{edges[priority][0], priority}};
            }
        }

        // for (auto pair : adj_list) {
        //     std::cout << pair.first << '\n';
        //     for (auto vec : pair.second) {
        //         std::cout << vec[0] << ' ';
        //     }
        //     std::cout << "\n\n";
        // }

        // 2. DFS
        std::unordered_set<int> seen {};
        std::deque<std::vector<int>> seen_edges {};
        // stupid, the nodes are labeled 1..n
        dfs(1, seen, 0, seen_edges);

        std::vector<int> ans {};
        int highestPriority = -1;

        // Replay and prune
        int i {};
        for (i = 0; i < result.size(); i++) {
            if (result[i][0] == cycle_head) {
                break;
            }
        }

        for (int k = 0; k < i; k++) {
            result.pop_front();
        }

        for (auto vec : result) {
            if (vec[2] > highestPriority) {
                ans = std::vector<int> {vec[0], vec[1]};
                highestPriority = vec[2];
            }
        }

        if (!setified_edges.contains(ans)) {
            std::reverse(ans.begin(), ans.end());
        }

        return ans;
    }
};
20726