并查集
来源:互联网 发布:麦博煲音箱软件 编辑:程序博客网 时间:2024/06/16 04:51
#include<iostream>#include<cstdio>using namespace std;int Ancestors[1500];//存储祖先节点; int ances(int i)//搜寻祖先节点; { if(Ancestors[i]!=i) Ancestors[i]=ances(Ancestors[i]); return Ancestors[i];}int main(){ int m,n,i; cin>>m>>n; for(i=1;i<=m;++i) Ancestors[i]=i; for(i=1;i<=n;++i) { int x,y; cin>>x>>y; x=ances(x); y=ances(y); if(x!=y) Ancestors[x]=y; } int q; cin>>q; while(q--) { int x,y; cin>>x>>y; if(Ancestors[x]==Ancestors[y]) cout<<"Yes!"<<endl; else cout<<"No!"<<endl; }}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- 求逆序对
- 括弧匹配检验
- static_cast、dynamic_cast、const_cast和reinterpret_cast总结
- 图的遍历[DFS][BFS]
- tarjan算法和Kosaraju算法
- 并查集
- 最小生成树Prim算法Kruskal算法
- Uva-156 Ananagrams
- 病毒&烦人的幻灯片
- 生成随机数
- 快速认识 ELK 中的 L:Logstash
- 素数
- 《将博客搬至CSDN》
- Pandoc —— 标记语言转换工具(中文乱码问题)