普通并查集模板

朴素的并查集:

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;

 

posted @ 2018-06-05 12:07  ZinYY  阅读(85)  评论(0编辑  收藏  举报