并查集
来源:互联网 发布:linux svn 创建分支 编辑:程序博客网 时间:2024/06/07 00:36
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#include <math.h>#include <queue>#define MAXN 100010#define inf 0x3f3f3f3fusing namespace std;int uset[MAXN];int deep[MAXN];//深度void init(){ for(int i = 0; i < MAXN; ++i){ uset[i] = i; } memset(deep,0,sizeof(deep));}int found(int x){ return x==uset[x]?x:uset[x] = found(uset[x]);}int main(){ int n,m; int x,y; while(~scanf("%d",&n)){ init(); scanf("%d",&m); while(m--){ scanf("%d%d",&x,&y); int xx = found(x); int yy = found(y); if(xx != yy){ uset[xx] = yy; /*加个深度优化 if(deep[xx] > deep[yy]){ uset[y] = x; } else{ uset[x] = y; if(deep[xx] == deep[yy]){ ++deep[yy]; } } */ } } for(int i = 1; i <= n; ++i){ x = found(i); cout<<x<<endl; } } return 0;}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- 【笔记】PMBOK第6章项目进度管理
- jquery中attr和prop的使用区别
- poj 1163 The Triangle
- sql_plan_baseline
- VB程序学习代码记录20160723
- 并查集
- ISCC2016宣传海报
- 用canvas画了个钟表,来玩玩吧
- TypedArray和obtainStyledAttributes使用
- 应用添加分享至微信、QQ和微博
- Service服务
- 专业单词
- Python遍历文件夹下所有文件中数据并写入Excel
- HDU(1010)bfs+剪枝 Tempter of the Bone