题意简单。

询问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. php 中cookie和session的用法比较

    1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,考虑到安全应当使用session. ...

  2. asp.net页面与页面之间传参数值

    一.利用POST传值 传值asp文件send.aspx  代码如下 复制代码 <form id="form1" runat="server" action ...

  3. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  4. dorado问题查询&快捷键重命名

    重命名还有一个快捷键  F2 有关dorado的问题可以进 www.bsdn.org提问,而且更好

  5. 使用MySQL数据库将汉字转换成拼音的一个C语言小程序

    环境: mysql:mysql-5.1.65 centos:centos 6.5 编译命令: gcc -o chinesetopinyin chinesetopinyin.c -L/usr/lib/m ...

  6. UserControl eventhander 注册问题

    1. 如果主页面调用UserControl来画界面 2. UserControl局部变化需要通过事件通知主页面,通过UserControl定义EventHandler,主界面注册UserControl ...

  7. 利用nginx做负载均衡

    round-robin:轮询.以轮询方式将请求分配到不同服务器上,默认 least-connected:最少连接数.将下一个请求分配到连接数最少的那台服务器上 ip-hash :基于客户端的IP地址. ...

  8. discuz x3插件开发傻瓜图文教程,用demo说话

    此demo功能是在模板footer部位插入一段javascript代码,这段代码可以是alert提示,也可以是加载广告等等. 第一步: 在config\config_global.php 文件里设置$ ...

  9. MFC VC6++学习笔记

    一.mfc中基于对话框程序添加菜单栏 1打开对话框资源,然后右键->属性->常规 里面有个"菜单" 下拉框,然后选择IDM_USER! 2打开对话框,右键属性,选择刚才 ...

  10. iOS 深复制&浅复制

        1.无论是深复制还是浅复制,被复制的对象类型是不变的.此对象类型具有什么功能就具有什么功能,不会因为自行修改了返回对象的指针类型而改变.   比如: 这里的str和str1的值和指针地址完全一 ...