POJ2524+并查集
题意简单。
询问n个人的宗教关系。
#include<stdio.h> const int maxn = ; int fa[ maxn ];
int vis[ maxn ]; void init( int n ){
for( int i=;i<=n;i++ )
{fa[i] = i;vis[i] = ;}
}
int find( int x ){
if( x==fa[x] )
return x;
return fa[x] = find( fa[x] );
}
void union_ab( int a,int b ){
int fa_a = find(a);
int fa_b = find(b);
if( fa_a == fa_b ) return ;
if( fa_a<fa_b ) fa[ fa_b ] = fa_a;
else fa[ fa_a ] = fa_b;
return ;
} int main(){
int n,m;
int Case = ;
//freopen("in.txt","r",stdin);
while( scanf("%d%d",&n,&m)== ){
if( n+m == ) break;
init(n);
int x,y;
while( m-- ){
scanf("%d%d",&x,&y);
if( x==y ) continue;
union_ab( x,y );
}
int ans = ;
for( int i=;i<=n;i++ ){
find(i);
}
/*
这里需要重新更新每个节点的父子关系。
比如:
5 4
1 2
5 3
4 5
1 4
*/
for( int i=;i<=n;i++ ){
vis[ fa[i] ] = ;
}
for( int i=;i<=n;i++ ){
ans += vis[ i ];
//printf("fa[%d] = %d\n",i,fa[i]);
}
printf("Case %d: %d\n",Case++,ans);
}
return ;
}
POJ2524+并查集的更多相关文章
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...
- poj2524(并查集水题)
题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- 并查集——poj2524(入门)
传送门:Ubiquitous Religions 许多次WA,贴上错的代码随时警示 简单没多加修饰的并查集 [WA1] #include <iostream> #include <c ...
- 【POJ-2524】Ubiquitous Religions(并查集)
并查集. #include<cstdio> #include<cstring> using namespace std; const int maxn = 55555; int ...
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
随机推荐
- 第一个Apple Watch小例子
原文在这, 不过他说的add target按照他的说法还真没找到(估计是我的眼瞎了或者是版本不一样),还有就是好记性不如烂博客,先自己能看懂就行了. 请用Single View Application ...
- Php 魔术常量
魔术常量 由于其值在程序运行过程中不允许更改,所以是常量:其值,又是运行环境不同而不现,所以叫魔术. __DIR__ 当前脚本文件的路径 示例: __LINE__ ...
- 公交CPU卡原理
现在的公交卡已经开始逐步的采用IC卡(CPU卡?什么东东?),而且在国家交通部的推动下,开始了全国范围内的互联互通.以后,手里只用拿着一张卡,就可以走遍全国,而且如果支持在线充值的话,基本上就不用在车 ...
- 关键字 extern
定义:extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中.编译器会到其他模块中寻找其定义. extern int f(); extern int i; extern关键字 作为 ...
- 重回cnblogs
毕业一年,关于工作的想法和思路渐渐充实,是时候回到cnblogs,开始写技术日志了.
- Poj 3117 World Cup
1.Link: http://poj.org/problem?id=3117 2.Content: World Cup Time Limit: 1000MS Memory Limit: 65536 ...
- linux关闭服务的方法
本文介绍下,在linux下关闭服务的方法,主要学习chkconfig的用法,有需要的朋友参考下. 先来看一个在linux关闭服务的例子,例如,要关闭sendmail服务,则可以按如下操作. 例1, 复 ...
- php删除html标签的三种解决方法
分享下PHP删除HTMl标签的三种方法. 方法1:直接取出想要取出的标记 <?php //取出br标记 function strip($str) { $str=str_replace(" ...
- 关于B/S系统中文件上传的大小限制怎么做
1.前端:采用flash控件或者Html5的特性(有浏览器版本要求)来判断文件大小.纯html或js是没法判断用户上传文件大小的. 2.nginx:服务器端的第一道防线,一般会有对上传文件做大小限制. ...
- centos6.5 最小化安装无法上网
在VMware里装了个centos 6.5. 最小化安装后无法上网.在 google里找到答案 第一步:执行命令启动网卡 (最小化安装不是自动启动的) [root@localhost]# ifcon ...