taptree

Union-Find Set

短小精悍的代码+巧夺天工的思路, 完美地无可救药。

int find(int x) {                   //寻找根节点(加路径压缩)

       if (x!=fa[x])             fa[x]=find(fa[x]);

       return fa[x];
}

 

void merge(int x, int y)          //合并操作(按秩合并)

{

        int fx=find(x),fy=find(y);

        if (fa==fb)   return;

        if ( rank[fx]<rank[fy] )       fa[fx]=fy;

        else    fa[fy]=fx;

        if ( rank[fx]==rank[fy] )         rank[fx]++;
}

评论
I'm a tree.Standing alone in the field of code.