POJ2524
来源:互联网 发布:住宅成交数据 编辑:程序博客网 时间:2024/05/17 08:18
并查集模版,水题
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 50005;int f[N];int n, m;void init() { for (int i = 1; i <= n; i++) f[i] = i; }int find(int x) { return x == f[x] ? x : f[x] = find(f[x]);}void Union(int a, int b) { int pa = find(a); int pb = find(b); if (pa != pb) { f[pa] = pb; }}int main() { int t = 1; while (scanf("%d %d", &n, &m) && n && m) { init(); int a, b; for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); Union(a, b); } int cnt = 0; for (int i = 1; i <= n; i++) if (f[i] == i) cnt++; printf("Case %d: %d\n", t++, cnt); } return 0;}
0 0
- poj2524
- poj2524
- POJ2524
- poj2524
- POJ2524
- poj2524
- poj2524
- [POJ2524]宗教
- poj2524 Ubiquitous Religions
- Ubiquitous Religions POJ2524
- poj2524解题报告
- POJ2524 Ubiquitous Religions
- POJ2524:Ubiquitous Religions
- poj2524 Ubiquitous Religions
- POJ2524(并查集)
- poj2524(并查集)
- POJ2524-并查集
- poj2524 Ubiquitous Religions
- 用MySQLDump来打包(压缩)备份文件及如何还原到其他端口上的数据库
- 王金平:有义务尽快使“立法院”恢复运作
- leetcode-two sum(2014.4.3)
- Perl 中的正则表达式
- HTML笔记——列表
- POJ2524
- PHP生成和获取XML格式数据
- 操作系统之PV操作
- unable to load virtualbox engine解决方案
- leetcode-gas station(2014.2.21)
- C++创建类必备的三个函数以及使用new所需要做的事
- MachineLearning(Hsuan-Tien Lin)第十一讲
- JAVA命令大全
- Winform中的Load事件处理