并查集


一般用于联通图,路径,朋友,有分组的

int father[10000000];
int fatherList(int x){
    int a=x;
    while(x!=father[x]){//查找父节点
        x=father[x];
    }

    //并查集的压缩版,使所以的子节点的指向同一个父亲节点
    
    while(a!=father[a]){
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}
void union1(int a,int b){//并
    int fa=fatherList(a);
    int fb=fatherList(b);
    if(fa!=fb)//使两个节点联合
        father[fa]=fb;


}