并查集

int find(int x) {
       if(fa[x] == x)  return x;
       fa[x] = find(fa[x]);
       return fa[x];
}
void unio(int x, int y) {
       int  fx = find(x), int fy = find(y);
       if(fx != fy) fa[x] = y;
}
void init() {
for(int i = 0; i < n; i++) fa[i] = I;
}

//带权重的写法
int find(int x) {
       if(fa[x] == x)  return x;
       fa[x] = find(fa[x]);
       return fa[x];
}
void unio(int x, int y) {
       int  fx = find(x), int fy = find(y);
       if(fx != fy) fa[x] = y, cnt[y] += cnt[x];
}
void init() {
for(int i = 0; i < n; i++) fa[i] = I, cnt[i] = 1;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-05-03 13:32  xryz  阅读(107)  评论(0编辑  收藏  举报