POJ-2524 Ubiquitous Religions【并查集】
来源:互联网 发布:php架构师书籍 编辑:程序博客网 时间:2024/05/18 22:17
1.思路分析
根据题意,统计所有人的宗教信仰,为集合操作,应使用并查集。
2.方法设计及性能衡量
实现并查集即可,计算合并后的集合数量。
3.实现部分
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>int n,m,group=0,par[50010];void init(){ int i; for(i=1;i<=n;i++){ par[i]=i; }}int find(int x){ if(par[x]==x)return x; else return par[x]=find(par[x]);}int unio(int x,int y){ x=find(x); y=find(y); if(x==y)return 0; else { par[x]=y; return 1; }}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ if(n==0&&m==0)break; group++; int i,a,b,count=n; init(); for(i=0;i<m;i++){ scanf("%d%d",&a,&b); if(unio(a,b)!=0)count--; } printf("Case %d: %d\n",group,count); } 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 并查集
- 串口通信帧的同步方法(识别一帧数据的起始结束)
- B/S与C/S究竟是何物
- 百度地图二次开发
- 城市天气和现在时间
- MySQL命令行中文编码完美解决方案
- POJ-2524 Ubiquitous Religions【并查集】
- git 分之管理---创建与合并分支
- 服务计算——配置go语言环境
- Oracle 数据库基础练习
- 几秒出现数据(图片)和时间
- struts2总结
- 自动轮播适配器
- RTU安卓开发
- 线段树+差分——【模板】树状数组2