poj 2524 Ubiquitous Religions
来源:互联网 发布:淘宝开店怎么找不到了 编辑:程序博客网 时间:2024/06/06 04:48
题目链接:http://poj.org/problem?id=2524
并查集的模版题:
#include <cstdio>#include <cstring>struct node{ int pre; int rankk;};node stu[50010];int n,m;int find(int x){ int r=x; while(stu[r].pre!=-1) r=stu[r].pre; while(r!=x) { int p=stu[x].pre; stu[x].pre=r; x=p; } return r;}void unionone(int x,int y){ if(stu[x].rankk>stu[y].rankk) { stu[y].pre=x; stu[x].rankk+=stu[y].rankk; } else { stu[x].pre=y; stu[y].rankk+=stu[x].rankk; }}int main(){ int i,x,y,ans,cnt=0; while(~scanf("%d%d",&n,&m)&&(n||m)) { ans=0; for(i=1;i<=n;++i) { stu[i].pre=-1; stu[i].rankk=1; } for(i=1;i<=m;++i) { scanf("%d%d",&x,&y); int xx=find(x); int yy=find(y); if(xx!=yy) unionone(xx,yy); } for(i=1;i<=n;++i) { if(stu[i].pre==-1) ++ans; } cnt++; printf("Case %d: %d\n",cnt,ans); } return 0;}
0 0
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524 Ubiquitous Religions
- Poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 - Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524-Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- 文件读写
- UITextView:定制选中文字的菜单和用代码选择文字
- 计算机操作系统——存储器管理(6)
- modem建链过程详述
- 即使没有读者,你应该写博客(You should blog even if you have no readers)
- poj 2524 Ubiquitous Religions
- Fedora 20下Eclipse Ingido中配置hadoop插件,并运行Wordcount例程
- SQL join
- .net序列化及反序列化
- Eclipse快捷键大全
- 九度:题目1008:最短路径问题
- zsh 使用 安装
- 两个数组比较,去掉重复的数据后生成第三个数组,这个怎么实现?
- Pajek的数据录入与参数计算