hdu1231 并查集模板题
来源:互联网 发布:预算软件破解版 编辑:程序博客网 时间:2024/05/16 10:43
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
模板题,没什么好说的。.
这里整合了并查集的模板,也有一些并查集的说明:http://blog.csdn.net/coder_panyy/article/details/52814987
0ms代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int pre[1005],n,m;bool vis[1005];void init(){for(int i=1;i<=n;i++){pre[i] = i;}memset(vis,false,sizeof(vis));}int find(int p){int root = p, t;while(pre[root]!=root)root = pre[root];while(pre[p]!=p){t = pre[p];pre[p] = root;p = t;}return root;}void connect(int a, int b){int x = find(a), y = find(b);if(x!=y){pre[x]=y;}}//统计连通分量 void solve(){int res = 0;for(int i=1;i<=n;i++){if(pre[i]==i){res++;}}printf("%d\n",res);}int main(){//freopen("input.txt","r",stdin);int T, a, b;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);init();while(m--){scanf("%d%d",&a,&b);connect(a,b);}solve();} return 0;}
0 0
- hdu1231 并查集模板题
- hdu1231 并查集模板题
- 并查集模板
- 并查集模板
- 并查集模板
- 并查集模板
- 【模板】并查集
- 并查集模板
- 并查集模板
- 并查集模板
- 并查集模板
- 并查集【模板】
- 并查集 模板
- 并查集模板
- 并查集模板
- 并查集模板
- 并查集模板
- 并查集模板
- Longest substring without repeating characters
- SharedPreferences保存List
- JAVA的构造器和构造方法
- uvalive3882(约瑟夫环) B
- 自学React Native(一)
- hdu1231 并查集模板题
- please shutdown interbase guardian before proceeding
- 在Octave中移动数据
- 数据结构(六)线性表(一)
- hdu5916长春区题目-瞎搞构造
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 7
- Android App 常用的几种系统交互程序
- hdu1272 并查集
- android开发常用的数据库