poj-2524(裸并查集)
来源:互联网 发布:java 读取文件流 编辑:程序博客网 时间:2024/06/05 19:49
题目链接:
http://poj.org/problem?id=2524
题意: n个人,m对人宗教相同,输出一共有多少个不同的宗教。
代码如下:
#include<iostream>#include<cstdio>using namespace std;#define M 500100int par[M];int h[M];int n, m;void init(int a){ for(int i = 1; i <= a; i++) { par[i] = i; h[i] = 0; }}int Find(int a){ if(par[a] != a) { return par[a] = Find(par[a]); } else return a;}void Union(int a,int b){ a = Find(a); b = Find(b); if(a == b) return; else { if(h[a] > h[b]) par[b] = a; else { if(h[a] == h[b]) h[b]++; par[a] = b; } }}int main(){ int cases = 1; while(scanf("%d%d",&n,&m) == 2) { if(n == 0) break; init(n); int Count = 0; for(int i = 0; i < m; i++) { int a,b; scanf("%d%d",&a,&b); Union(a,b); } for(int i = 1; i <= n; i++) { if(par[i] == i) Count++; //自己是根节点的是一类宗教 } cout<<"Case "<<cases++<<": "<<Count<<endl; } return 0;}
0 0
- poj-2524(裸并查集)
- poj 2524 并查集
- 并查集 POJ 2524
- poj-2524 并查集
- poj 2524(并查集)
- POJ-2524 并查集
- poj 2524(并查集)
- POJ 2524 并查集
- POJ 2524 查并集
- POJ 2524 并查集
- POJ 2524 并查集
- poj 2524 并查集
- POJ 2524 并查集
- poj 2524[并查集]
- POJ 2524 (并查集)
- POJ 2524 并查集
- poj 2524 并查集
- 并查集 POj 2524
- [Phonegap+Sencha Touch] 移动开发1、准备工作
- fprintf与vfprintf的区别于用法
- 一些我看过的Object tracking算法的理解
- makefile @echo 命令回显
- 将EXCEL文件转换成utf8编码的csv文件
- poj-2524(裸并查集)
- 表的级联更新,删除以及交叉连接
- 指针初步知识
- [Phonegap+Sencha Touch] 移动开发2、PhoneGap/Cordova初步使用
- JSTL自定义标签一简单的例子
- [Phonegap+Sencha Touch] 移动开发3、使用Weinre调试PhoneGap/Cordova应用
- 使用Latex制作简历
- 【静态链接】第2章---------------------静态链接过程(符号解析与重定位)
- c++基础