一个非常朴素的并查集模板,先贴代码:
int set[10005];
int findSet(int x) {
if (set[x] == x)
return x;
return set[x] = findSet(set[x]);
}
void unionSet(int x, int y) {
int fx = findSet(x);
int fy = findSet(y);
if(fx!=fy)
set[fx] = fy;
}
当然别忘了赋初值哦
for(int i=1; i<=n; i++) set[i] =i;