并查集及其简单应用:优化kruskal算法
2014-08-05 16:03
260 查看
并查集是一种可以在较短的时间内进行集合的查找与合并的树形数据结构
每次合并只需将两棵树的根合并即可
通过路径压缩减小每颗树的深度可以使查找祖先的速度加快不少
代码如下:
并查集优化克鲁斯卡尔算法:
每次合并只需将两棵树的根合并即可
通过路径压缩减小每颗树的深度可以使查找祖先的速度加快不少
代码如下:
int getfather(int x) //查找祖先 { if(father[x]!=x) father[x]=getfather(father[x]); //路径压缩,把每个节点的父亲都变得与他的祖先相同 else return x; return father[x]; } int issame(int x,int y) //是否在同一个集合 { return getfather(x)==getfather(y); } void set_union(int x,int y) //合并 { int fx,fy; fx=getfather(x); fy=getfather(y); father[fx]=fy; }
并查集优化克鲁斯卡尔算法:
for(i=1;i<=n;i++) father[i]=i; qsort(e,k,sizeof(ed),cmp); ans=0; for(i=0;i<k;i++) { if(issame(e[i].l,e[i].r)) //跳过已在生成树中的边 continue; ans+=e[i].weight; set_union(e[i].l,e[i].r); }
相关文章推荐
- 并查集及其简单应用
- 最小生成树Kruskal算法与并查集及其优化
- 并查集 (Union-Find Sets)及其应用
- 并查集及其应用
- Delphi 画布对象及其简单应用
- Delphi 画布对象及其简单应用
- 基于MINA构建简单高性能的NIO应用-优化指南
- 多线程的基本概念及其简单应用
- 【jquery模仿net控件】初步GridView模型实现,及其简单应用
- 【我看Hibernate】Hibernate 介绍及其简单应用
- SeqList.h (顺序表及其简单应用)
- 凸优化收缩算法的简单统一与应用
- 并查集 (Union-Find Sets)及其应用
- 并查集(Union-Find Sets)及其应用
- 【我看Hibernate】Hibernate 介绍及其简单应用
- kmp算法的思想及其简单应用(java版)
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集及其应用
- 隐马尔可夫模型及其在分词中的简单应用