并查集压缩路径非递归写法

int find(int x){
    int temp=x;
    while(temp!=d[temp])
        temp=d[temp];
    while(x!=d[x]){
        x=d[x];
        d[x]=temp;
    }
    return temp;
}

猜你喜欢

转载自www.cnblogs.com/war1111/p/12526682.html