poj 2504 Ubiquitous Religions
2010-10-17 18:21
260 查看
题意:宗教信仰的引题,首先告诉我们相互认识的两个人有相同的信仰。
解题思路:比较简单的并查集入门。
代码:
解题思路:比较简单的并查集入门。
代码:
#include <iostream> using namespace std; const int MAX = 50005; int father[MAX],num[MAX]; int n,output; //father[i] 保存的是i的父亲节点。num【i】中保存的是 这个集合里面从i开始下面有多少个数+自己 void init() { for(int i = 0 ; i <=n ; i++) { father[i] = i; num[i] = 1; } } int find(int x) { if(x!=father[x]) father[x] = find(father[x]); return father[x]; } void unions(int a,int b) { int x = find(a); int y = find(b); if(x==y) return ; else if(num[x] <= num[y]) { father[x] = y; num[y] += num[x]; output--; } else { father[y] = x; num[x] += num[y]; output--; } } int main() { int x,y,m; int cas=1; while(scanf("%d%d",&n,&m) , n||m) { output = n; init(); while(m--) { scanf("%d%d",&x,&y); unions(x,y); } printf("Case %d: %d/n",cas,output); cas++; } return 0; }
相关文章推荐
- poj 2504 Bounding box
- zoj 1892 || poj 2504 || UVA 10577 Bounding box
- POJ 1159 Palindrome
- poj 3292 Semi-prime H-numbers
- POJ 2553 The Bottom of a Graph (Trajan 强连通分量 缩点)
- POJ 2109 算数积累
- 原根 poj 1284
- poj2559 Largest Rectangle in a Histogram hdu1505 City Game
- poj 3017
- poj 1155 TELE (树形背包dp)
- poj1995 Raising Modulo Numbers 整数型快速幂 算法的应用
- poj 1019 Number Sequence 对数的分解应用
- poj 2948 Martian Mining
- poj_3071概率dp
- poj 2104 K-th Number(线段树)
- POJ 3716 Panda's Birthday Present 条件概率
- POJ1703 Find them, Catch them 并查集 好题 有坑点
- POJ 3259 Wormholes 虫洞(负权最短路,负环)
- POJ2388map的应用统计树的百分比,并按照字母顺序输出
- poj2001Shortest Prefixes【字典树找最短唯一前缀】