题意简单。

询问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. 获取bundle目录下的所有图片文件名

    今天在写代码时候,偶然发现自己忘记了一些oc的基础知识(这里指的是获取bundle目录下的所有图片),感到很不爽.在百度了几次,发现自己的领悟能力实在不行,感觉萌萌的::>_<:: 好了, ...

  2. java新手笔记28 文件

    1.目录.文件 package com.yfs.javase; import java.io.File; public class FileDemo1 { /** * File 目录 文件 */ pu ...

  3. xml的生成与解析_老师笔记

    使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...

  4. Java+FlexPaper+swfTools 文档在线预览demo

    1.概述 主要原理 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 2.通过swfTools将pdf文件转换成swf格式的文件 3.通过FlexP ...

  5. 考虑virtual函数以外的其它选择

    详情见<Effective C++>item35 1.使用non-virtual interface(NVI)手法,这是Template Method设计模式的一种特殊形式. 它以publ ...

  6. lex&yacc8--wehter use in C++

    bintree.h:12:1: error: unknown type name ‘using’ using namespace std; ============== bintree.h:28:1: ...

  7. Linux 下cronolog分割catalina.out文件

    开发项目的时候查看日志,发现catalina.out已经有1个多G,日积月累的慢慢变大,幸亏及时发现还没有导致错误, tomcat默认日志之一输出在catalina.out文件中的,不会分割,不便于使 ...

  8. encodeURIComponent=>Uri.EscapeDataString

    javascript: encodeURIComponent=> C#: Uri.EscapeDataString http://stackoverflow.com/questions/5754 ...

  9. Hibernate 老外的完整教程

    http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/

  10. Java执行命令行脚本

    百度到的 Process p=Runtime.getRuntime().exec("C:\\test.cmd"); ProcessBuilder processBuilder=ne ...