题意简单。

询问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+并查集的更多相关文章

  1. POJ1611 && POJ2524 并查集入门

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28293   Accepted: 13787 De ...

  2. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  3. POJ2524并查集水题

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  4. poj2524 Ubiquitous Religions(并查集)

    题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...

  5. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  6. POJ2524 Ubiquitous Religions(并查集)

    题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...

  7. 并查集——poj2524(入门)

    传送门:Ubiquitous Religions 许多次WA,贴上错的代码随时警示 简单没多加修饰的并查集 [WA1] #include <iostream> #include <c ...

  8. 【POJ-2524】Ubiquitous Religions(并查集)

    并查集. #include<cstdio> #include<cstring> using namespace std; const int maxn = 55555; int ...

  9. poj-2524 ubiquitous religions(并查集)

    Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...

随机推荐

  1. 关于JAVA System常见类的一些总结

    一.JAVA System类概述 1.概述: System 类是一个抽象类,所有的字段和方法都是静态的,即不能被实例化.其中包含一些有用的类字段和方法,它不能被实例化.在 System 类提供的设施中 ...

  2. Cocos2d-x开发实例介绍特效演示

    下面我们通过一个实例介绍几个特效的使用,这个实例下图所示,下图是一个操作菜单场景,选择菜单可以进入到下图动作场景,在下图动作场景中点击Go按钮可以执行我们选择的特性动作,点击Back按钮可以返回到菜单 ...

  3. ios开发----视图的生命周期

    熟悉web开发的朋友可能对页面page的生命周期有一定的了解和认识,正如web开发中的页面生命周期一样,移动客户端开发也有它自己的生命周期.下文将说明ios开发中视图的生命周期既运行顺序. 在ios视 ...

  4. (转)Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

    本文的英文版本链接是 http://www.mrxuri.com/index.php/2013/11/20/install-mysql-cluster-on-ubuntu-12-04-lts.html ...

  5. 简介spring中MethodReplacer的用法

    欢迎转载交流:个人博客地址http://www.cnblogs.com/shizhongtao/p/3468713.html org.springframework.beans.factory.sup ...

  6. mysql 主从同步 Last_SQL_Error

    参考文章: http://kerry.blog.51cto.com/172631/277414/ http://hancang2010.blog.163.com/blog/static/1824602 ...

  7. java concurrent包的学习(转)

    java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...

  8. 【winform】如何在DateTimePicker中显示时分秒

    1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss

  9. 解决DB2事物日志满、扩充表字段长度和表空间的命令

    解决DB2事物日志满.扩充表字段长度和表空间的命令 转:http://blog.sina.com.cn/s/blog_4c0137d10100bb5r.html 一.通常我们在使用db2导入数据或进行 ...

  10. RequireJS的简单应用

    一.RequireJS的主要作用与优点 主要作用:js模块化.编写复用js代码 优点: 1.防止命名冲突 2.声明不同js文件之间的依赖 3.代码模块化 (1)一个文件一个模块:每个js文件应该只定义 ...