版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/83304123
https://leetcode.com/problems/redundant-connection/description/
class Solution {
public:
int find(int a, int p[])
{
int res = a;
while(res!=p[res]){
res = p[res];
}
return res;
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int parent[edges.size()+1];
for(int i=1; i<=edges.size(); i++){
parent[i] = i;
}
for(auto e:edges){
if(find(e[0],parent)==find(e[1], parent))//在一个集合里面了,说明有圈了
return e;
parent[find(e[1], parent)] = find(e[0], parent);
}
}
};