码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-04-02 14:54:21      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:

并查集:
使用并查集可以把每个连通分量看作一个集合,该集合包含连通分量的所有点。这两两连通而具体的连通方式无关紧要,
就好比集合中的元素没有先后顺序之分,只有属于和不属于的区别。
#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; }

 

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

标签:

原文地址:http://www.cnblogs.com/jin-nuo/p/5347531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!