标签:并查集 合并 开始 表示 ret 初始化 初始 一个 oid
int find(int x){ // 寻找x的根 int i=x,j; while(root[x]!=x) //如果x的根节点不为他本身则继续找 x=root[x]; while(i!=x){ // 路径压缩 j=root[i]; root[i]=x; i=j; } return x; } void Union(int x,int y){ // 合并x,y成一个集合 int a=find(x);//x的根节点为a int b=find(y);//y的根节点为b if(a!=b)//如果a,b不相等,表示不是一个集合 { root[a]=b; } } void init(int n){ // 一开始对root数组进行初始化 for(int i=1;i<=n;i++){ root[i]=i; } }
标签:并查集 合并 开始 表示 ret 初始化 初始 一个 oid
原文地址:https://www.cnblogs.com/mengyinfuIOI/p/9382654.html