POJ 2524 Ubiquitous Religions 详细题解(并查集)
来源:互联网 发布:mac版qq怎么发送文件 编辑:程序博客网 时间:2024/05/17 20:25
题目链接:http://poj.org/problem?id=2524
这道题属于并查集
题目大意:
在一个学校内调查学生的信仰情况,告诉你学校有n个人,给你共m组x,y表示信教情况相同,问一个学校学生信仰的宗教的最大种数
这也是一道模板题,没啥说的,我在另一篇文章里面讲过模板题的做法,大家可以参考下。http://blog.csdn.net/xdz78/article/details/47296585
#include <stdio.h>#include <stdlib.h>#include <string.h>int pre[50010];int find(int x){ int r=x; while(pre[r]!=r){ r=pre[r]; } //路径压缩,优化 int i=x,j; while(i!=r){ j=pre[i]; pre[i]=r; i=j; } return r;//返回根节点}void join(int x,int y){//如果x,y在不同树上就把x,y的根节点改成一样的。在一棵树上就不要动了 int r_x=find(x),r_y=find(y); if(r_x!=r_y){ pre[r_x]=r_y; }}int main(){ int hash[50010]; int sum,x,y,i,j=1; int n,m; while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){ sum=0; memset(hash,0,sizeof(hash)); for(i=1;i<=n;i++){ pre[i]=i; }//前面是初始化 while(m--){ scanf("%d%d",&x,&y); join(x,y); }//把所有关系加入到树中 for(i=1;i<=n;i++){ hash[find(i)]=1;//简单哈希 } for(i=1;i<=n;i++){ sum+=hash[i]; } printf("Case %d: %d\n",j++,sum); } return 0;}
1 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 (并查集)
- UVa 658 It's not a Bug, it's a Feature! (Dijkstra+位运算技巧+好题)
- 如何坚持每周写一篇博客
- (7) linux 编译并安装mysql-5.6.15.tar.gz ---> CentOS-6.6-x86_64 + mysql-5.6.15.tar.gz
- quartz CronExpression表达式
- 第二篇T语言实例开发(版本5.3),福彩3D摇号器
- POJ 2524 Ubiquitous Religions 详细题解(并查集)
- Cocos2d-x学习笔记(一)——开发环境搭建(win7)
- 开发中遇到的一个关于 SQL 语句执行的问题
- 关于dede:list 标签 titlelen 长度显示不全解决方案
- Android新线程中更新主线程UI中的View方法汇总
- Android 按钮常用点击事件大总结
- http请求中的referer
- 用vnc实现windows远程连接linux桌面
- EMV读卡器终端数据库的设计