https://leetcode.com/problems/redundant-connection/description/
题目:在图中剔除一条边,使得它变成一棵树。
思路:裸并查集。。
class Solution {
public:
int *fat;
int find(int x) {
return fat[x]==x?x:fat[x]=find(fat[x]);
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int len=edges.size();
fat=new int[len+1];
vector<int>v;
for(int x=1;x<=len;x++) fat[x]=x;
for(int x=0;x<len;x++){
int a=find(edges[x][0]);
int b=find(edges[x][1]);
if(a==b) {
v.push_back(edges[x][0]);
v.push_back(edges[x][1]);
break;
}
fat[a]=b;
}
return v;
}
};