图论总结(7)并查集
来源:互联网 发布:linux如何退出top 编辑:程序博客网 时间:2024/05/17 09:03
并查集虽然是数据结构,但还是把它 放在图论里总结了吧,后面用最小生成树是要涉及到。
个人理解并查集就是用来处理集合,"并"是合并两个集合,“差”是查找一个的元素在哪个集合里,“集”就是集合啦。
并查集的关键就是用一个集合中代表元素来代表该集合。
上模板:
#include<iostream>#include<cmath>using namespace std;const int maxn=20001;int father[maxn];int find(int x){if(x!=father[x])father[x]=find(father[x]);return father[x];}void unionn(int r1,int r2){father[r2]=r1;}int main(){int n,m;cin>>n>>m;for(int i=1;i<=n;i++)father[i]=i;for(int i=1;i<=m;i++){int x,y;cin>>x>>y;int r1=find(x);int r2=find(y);if(r1!=r2){unionn(r1,r2);}}}记得father数组的初始化,find函数这样写可以避免长链表。
阅读全文
0 0
- 图论总结(7)并查集
- 并查集(总结)
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集--总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集 总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 内存查看平台化
- 推荐系统入门必读的经典paper
- 送给SDUTACM暑假集训同学们的一些话
- 库与运行库
- 这六个小细节SEO网站优化需要注意
- 图论总结(7)并查集
- @EnableAutoConfiguration
- script在Html中的摆放位置
- 分组对称加密模式:ECB/CBC/CFB/OFB
- 将数据从文件中读出并排序
- Eclipse使用:从git克隆maven项目
- vue.js import/require数组后computed监听失败
- 04.assetbundle下载
- ROC曲线知识