普通并查集模板

朴素的并查集:

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;

猜你喜欢

转载自www.cnblogs.com/zinyy/p/9139083.html
今日推荐