<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 detect(int node,vector<bool>&vis,vector<vector<int>>&adj){
//         vis[node]=1;
//         queue<pair<int,int>>q;
//         q.push({node,-1});
//         while(q.size()!=0){
//             int a=q.front().first;
//             int p=q.front().second;
//             q.pop();
//             for(int i=0;i<adj[a].size();i++){
//                 if(!vis[adj[a][i]]){
//                     vis[adj[a][i]]=1;
//                     q.push({adj[a][i],a});
//                 }
//                 else if(p!=adj[a][i]){
//                     return true;
//                 }
//             }
//         }
//         return false;
//     }

//     vector<int> findRedundantConnection(vector<vector<int>>& edges) {
//         int n=edges.size();
//         vector<bool> vis(n + 1, false);  
//         vector<vector<int>> adj(n + 1);
//         for(int i=0;i<edges.size();i++){
//             adj[edges[i][0]].push_back(edges[i][1]);
//             adj[edges[i][1]].push_back(edges[i][0]);
//         }
//        for (int i = 0; i < edges.size(); i++) {
//             int u = edges[i][0];
//             int v = edges[i][1];
//             adj[u].push_back(v);
//             adj[v].push_back(u);

//             fill(vis.begin(), vis.end(), false);

//             if (detect(u, vis, adj)) {
//                 return {u, v};
//             }
//         }
//         return {};
//     }
// };


class Solution {
public:
    // Helper function to detect a cycle in the graph
    bool detect(int node, vector<bool>& vis, vector<vector<int>>& adj) {
        vis[node] = true;
        queue<pair<int, int>> q;
        q.push({node, -1});  // {current node, parent node}

        while (!q.empty()) {
            int a = q.front().first;
            int parent = q.front().second;
            q.pop();

            // Traverse all neighbors
            for (int i = 0; i < adj[a].size(); i++) {
                int neighbor = adj[a][i];
                
                // If the neighbor is not visited, push to the queue
                if (!vis[neighbor]) {
                    vis[neighbor] = true;
                    q.push({neighbor, a});
                } 
                // If the neighbor is visited and it's not the parent, a cycle is detected
                else if (neighbor != parent) {
                    return true;
                }
            }
        }
        return false;
    }

    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        int n = edges.size();
        vector<bool> vis(n + 1, false);  // To track visited nodes
        vector<vector<int>> adj(n + 1);  // Adjacency list

        // Process each edge
        for (int i = 0; i < edges.size(); i++) {
            int u = edges[i][0];
            int v = edges[i][1];

            // Add the edge to the graph (before checking for cycles)
            adj[u].push_back(v);
            adj[v].push_back(u);

            // Clear the visited array for the fresh cycle check
            fill(vis.begin(), vis.end(), false);

            // Check if this edge creates a cycle
            if (detect(u, vis, adj)) {
                // If a cycle is detected, return the current edge causing it
                return {u, v};
            }
        }

        return {};
    }
};

18608