poj2524
说来惭愧啊。。现在才会并查集。我竟然给我妈妈讲明白并查集怎么回事了- -
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #define maxx 50010 int set[maxx]; int find(int x)
{
return x == set[x] ? x : (set[x] = find(set[x]));
} int main()
{
int n, m;
int kase = 0;
while (cin >> n >> m && (n || m))
{
for (int i = 1; i <= n; i++)
{
set[i] = i;
}
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b); int c = find(a), d = find(b);
if (c != d){ set[c] = d; } }
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (set[i] == i) cnt++;
}
cout << "Case " << ++kase << ": " << cnt << endl;
}
}
poj2524的更多相关文章
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...
- [原]poj-2524(裸并查集)
题目链接: http://poj.org/problem?id=2524 题意: n个人,m对人宗教相同,输出一共有多少个不同的宗教. 代码如下: #include<iostream> # ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524+并查集
题意简单. 询问n个人的宗教关系. #include<stdio.h> ; int fa[ maxn ]; int vis[ maxn ]; void init( int n ){ ;i& ...
- POJ2524——Ubiquitous Religions
Ubiquitous Religions Description There are so many different religions in the world today that it is ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- POJ2524:Ubiquitous Religions (并查集模板)
Description There are so many different religions in the world today that it is difficult to keep tr ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
随机推荐
- 下拉刷新,上拉装载许多其他ListView
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanVuaHVhaG91c2U=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 收藏的Android很好用的组件或者框架。
收藏的Android很好用的组件或者框架. android框架 先说两个站点: http://www.androidviews.net/ 非常好的国外开源码站,就是訪问速度有点慢啊 http://w ...
- Android 上实现非root的 Traceroute -- 非Root权限下移植可运行二进制文件 脚本文件
作者 : 万境绝尘 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/36438365 演示样例代码下载 : -- CSDN : h ...
- Python学习入门基础教程(learning Python)--3.2 if-else分支语句
if-else分支语句结构的特点是当conditon条件满足时,执行if下的语句块,当condition条件不满足时执行else下的语句块,也就是说根据条件来控制让某些语句执行,某些语句不被执行. i ...
- The JSR-133 Cookbook for Compiler Writers(an unofficial guide to implementing the new JMM)
The JSR-133 Cookbook for Compiler Writers by Doug Lea, with help from members of the JMM mailing lis ...
- 矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n ...
- ecshop2.7.3怎么自动清除缓存
1.在ecs_shop_config表中插入一条数据 进入ECSHOP后台-数据库管理-SQL查询 复制下面SQL,粘贴到里面执行.注意这时是默认表前缀ecs_,如果你的修改过要和你的统一了. INS ...
- lua简洁的功能(两)
Lua中的函数带有词法定界的第一类值. 第一类值: 在Lua中,函数和其它值(数值,字符串)一样,函数能够被存放在变量中,也存放在表中, 能够作为函数的參数,还能够作为函数的返回值. 词法定界:被嵌套 ...
- CentOS tripwire-文件指纹
Tripwire是目前最为著名的unix下文件系统完整性检查的软件工具,这一软件采用的技术核心就是对每个要监控的文件产生一个数字签名,保留下来.当文件现在的数字签名与保留的数字签名不一致时,那么现在这 ...
- OpenStack_Swift源代码分析——Ring基本原理及一致性Hash算法
1.Ring的基本概念 Ring是swfit中最重要的组件.用于记录存储对象与物理位置之间的映射关系,当用户须要对Account.Container.Object操作时,就须要查询相应的Ring文件( ...