并查集路径压缩
2015-10-15 09:01
260 查看
//非递归压缩路径 int findx(int x){ int root = x; // 先找到根节点. while(root != parent[root]) root = parent[root]; while(x != root){ int p = parent[x]; parent[x] = root; // 将x指向根节点. x = p; // 移到x原来的父节点继续进行压缩. } return root; } //回溯路径时压缩, 可能会造成堆栈溢出, 建议用上一种. int findx(int x){ if(x!=parent[x]){ parent[x] = findx(parent[x]); } return parent[x]; }
相关文章推荐
- 数据库异常:“Column 'CODE' in where clause is ambiguous”
- Entity Framework 数据生成选项DatabaseGenerated
- String.split()函数使用注意事项
- js操作cookie
- QT 多线程程序设计
- MySql:备份与恢复数据库
- 自己做的漫画阅读APP.
- 在IntelliJ IDEA14中安装go语言插件
- 最优分解方案
- GSON
- 16种发动机动态工作原理图,神奇的帅呆了!
- 马云云栖大会魔性演讲:DT时代、云计算和投资创业详解!
- SQL中Left Join 与Right Join 与 Inner Join 与 Full Join的区别
- Tomcat的URL中文乱码解决以及传输优化
- python 如何引入自己的模块
- echarts动态加载数据,显示柱状图,饼图图表
- 数据持久化———CoreData
- ORACLE之ASM概念
- 在jsp页面中运行vbs
- 如何实现移动端浏览器不显示 pc 端的广告