poj 2524 Ubiquitous Religions(数据结构:并查集)
2014-07-22 16:50
513 查看
题意是告诉你有n个学生,其中m对学生信仰相同
让你输出n个学生一共有多少个信仰
简单并查集的使用,学生信仰相同则并入相同集合
最后输出有多少个集合即可
注意数组大小,因为这个RE了一次
代码如下:
让你输出n个学生一共有多少个信仰
简单并查集的使用,学生信仰相同则并入相同集合
最后输出有多少个集合即可
注意数组大小,因为这个RE了一次
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> #define MAXN 50010 #define LL long long using namespace std; int p[MAXN], n, m; int find(int x) { return p[x]==x ? x : p[x] = find(p[x]); } int cnt() { int i, ans = 0; for(i=0; i<n; ++i) { if(p[i] == i) ans++; } return ans; } int main(void) { int a, b, i, res = 1; while(scanf("%d%d", &n, &m) && (n+m)) { for(i=1; i<=n; ++i) p[i] = i; while(m--) { scanf("%d%d", &a, &b); a = find(a); b = find(b); if(a != b) p[a] = b; } printf("Case %d: %d\n", res++, cnt()); } return 0; }
相关文章推荐
- poj 2524 Ubiquitous Religions(最简单的并查集)
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
- poj 食物链 数据结构 并查集
- poj 2524 Ubiquitous Religions(并查集)
- POJ 2524 Ubiquitous Religions 并查集
- POJ 2524-Ubiquitous Religions 并查集
- poj 2524-小白算法学习 并查集 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions【并查集入门】
- (使用树结构来支持并查集操作8.4.4)POJ 2524 Ubiquitous Religions(并查集的基本操作: 求有多少个相互独立的集合)
- Ubiquitous Religions POJ - 2524(并查集)
- poj2524_并查集基础
- POJ 2524 Ubiquitous Religions(简单并查集)
- 【POJ】2524 - Ubiquitous Religions(并查集,水)
- POJ2524 Ubiquitous Religions(并查集)
- poj并查集问题-2524
- POJ 2524 并查集
- POJ 2524 Ubiquitous Religions【并查集入门】
- POJ2524——Ubiquitous Religions(并查集)
- poj 2524 普通并查集
- POJ 2524 Ubiquitous Religions【并查集模板】