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 ...
随机推荐
- iOS - 字典(NSDictionary)
1. 字典类型的常用处理 //---------------不可变字典 //1.字典的创建 NSArray *array1 = [NSArray arrayWithObjects:@"zha ...
- ios开发:GCD多线程
ios有三种多线程编程技术,分别是NSThread,Cocoa NSOperation和GCD,GCD全称Grand Central Dispatch 是Apple开发的一个多核编程的解决方法,在iO ...
- Objective-C 【构造方法(重写、场景、自定义)、super】
------------------------------------------- super关键字的使用 #import <Foundation/Foundation.h> @int ...
- 20141016--for 兔子
Console.Write("请输入月数:"); int m =int.Parse(Console.ReadLine()); ;//成兔对数ct ;//小兔对数xt ;//幼兔对数 ...
- (转)DES、RSA、MD5、SHA、随机生成加密与解密
一.数据加密/编码算法列表 常见用于保证安全的加密或编码算法如下: 1.常用密钥算法 密钥算法用来对敏感数据.摘要.签名等信息进行加密,常用的密钥算法包括: DES(Data Encr ...
- Poj OpenJudge 百练 1860 Currency Exchang
1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...
- xml学习总结(一)
xml DTD 定义元素<!ELEMENT 元素名 元素类型描述 > (1)元素类型描述:任意类型,字符串型,空元素,包含子元素,混合类型 任意类型: <?xml version=& ...
- Beaglebone Back学习三(开发环境搭建)
开发环境搭建 1 Ubuntu环境搭建 2 Window环境搭建 3 开发板环境搭建 1 Ubuntu环境搭建 (1)安装必要的网络工具 samba nfs tftp vmware-tools sam ...
- javascript event兼容IE和FF
事件对象在IE和FF下的兼容写法 function abc(event){ var e=event||window.event; //键盘码的捕获 var key=e.which||e.keyCode ...