并查集
来源:互联网 发布:冰血暴 结局 知乎 编辑:程序博客网 时间:2024/06/08 02:46
并查集常用来解决不相交集合的合并问题,在加入路径压缩和合并后,具有非常好的时间和空间复杂度。
#include<iostream>using namespace std;int father[100];int find(int &x){int r=x;int i,j;while(father[r]!=r) r=father[r]; i=x; while(i!=r) //路径压缩 { j=father[i]; father[i]=r; i=j; } return r;}void join(int &x,int &y) //合并 { if(find(x)==find(y)) return; father[y]=find(x);}int main(){int n,m;int a,b;int i=0;cin>>n>>m;for(;i<n;++i){father[i]=i; //初始化 }for(i=0;i<m;++i){cin>>a>>b; join(a,b); //合并 }cout<<"Input"<<endl;i=2;while(i--){ cin>>a>>b; a=find(a); b=find(b); if(a!=b) cout<<"No"<<endl; else{ cout<<"Yes"<<endl; }} return 0;}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- C和指针读书笔记-第9章(函数)
- Broadcom WICED Wi-Fi 研究BCM943362WCD4之STM32F205+43362通信
- mysql jdbc入门学习
- 使用 Eclipse 远程调试 Java 应用程序
- 斐波拉契数列的java实现和性能优化
- 并查集
- platform_set_drvdata
- IBM-Lotus文档库链接
- 实现一个包含Min,Max函数的栈
- Python小技巧 – True or False
- Unity3D之Get与Post
- NumPy教程
- IM设计思考:试问
- POJ 2407 Relatives(欧拉函数)