最小生成数(并查集)Kruskal算法

并查集:
使用并查集可以把每个连通分量看作一个集合,该集合包含连通分量的所有点。这两两连通而具体的连通方式无关紧要,
就好比集合中的元素没有先后顺序之分,只有属于和不属于的区别。
#define N 100 int father[N]; void init() { for(int i=0;i<n;i++) father[i]=1; } void union(int x,int y) //合并两元素所在集合 { x=getfather(x); y=getfather(y); if(x!=y) father[x]=y; } /*bool same(int x,int y) //判断两元素在不在同一集合 {return getfather(x)==getfather(y);} */ int getfather(int x) //获得该元素的父亲节点 { while(x!=father[x]) {x=father[x];} return x; }
原文地址:https://www.cnblogs.com/jin-nuo/p/5347531.html