本文共 496 字,大约阅读时间需要 1 分钟。
今天看了并查集的具体内容吧 首先先创建俩个函数 一个用来存 一个用来查 。并查集就是看用来看图中有几个小的联通区域
还有个路径缩短法 就是把具有联通在一起的节点 都设置一个 父亲节点
int pre[n]/存放父类节点
int find(int root){ int son=root; while(root!=pre[root])//查找上级 root=pre[root]; return root; //返回上级;}
int jion(int start,int finish) { int root1=find(start); int root2= find(finish); if(root1!=root2) //如果父类节点不相同(既构成不了环路) 把所产生的 父类节点存进去 pre[root1]=root2;}
while(son!=root) //路径压缩{ int cmp=pre[son]; pre[son]=root; // 把上级节点 赋值为 最上级的节点 也就是赋值根节点 son =cmp;}
转载地址:http://xxqq.baihongyu.com/