class Solution {
public:
int n = 0;
int find(int u , vector<int> &father)
{
if(u == father[u]) return father[u];
father[u] = find(father[u] , father);
return father[u];
}
void join(int u , int v , vector<int> &father)
{
u = find(u , father);
v = find(v , father);
if(u == v) return;
father[v] = u;
}
bool same(int u , int v , vector<int> &father)
{
u = find(u , father);
v = find(v , father);
return u == v;
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
n = edges.size() + 1;
vector<int> father(n,0);
for(int i=0 ; i<n ; i++)
father[i] = i;
for(int i=0 ; i<edges.size() ; i++)
{
if(same(edges[i][0] , edges[i][1] , father)) return edges[i];
else join(edges[i][0] , edges[i][1] , father);
}
return {};
}
};