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

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;
}

 

posted @ 2020-03-19 19:39  WeiAR  阅读(255)  评论(0编辑  收藏  举报