题目链接

分析:

给定 n 个点和 m 条无项边,求连通分量的数量。用并查集很简单。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <map>
  7. #include <queue>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. const int maxn = + ;
  13.  
  14. int p[maxn];
  15.  
  16. int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
  17.  
  18. int main(){
  19. int n, m, a, b, kase = ;
  20.  
  21. while(scanf("%d%d", &n, &m) == ) {
  22. if(n == && m == ) break;
  23. for(int i=; i<=n; i++) p[i] = i;
  24.  
  25. int cnt = ;
  26.  
  27. for(int i=; i<m; i++) {
  28. scanf("%d %d", &a, &b);
  29.  
  30. int x = find(a), y = find(b);
  31. if(x != y) {
  32. p[x] = y;
  33. }
  34. }
  35.  
  36. for(int i=; i<=n; i++) {
  37. if(p[i] == i) cnt++;
  38. }
  39.  
  40. printf("Case %d: ", ++kase);
  41. printf("%d\n", cnt);
  42. }
  43.  
  44. return ;
  45. }

POJ2524 Ubiquitous Religions(并查集)的更多相关文章

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

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

  2. [ACM] POJ 2524 Ubiquitous Religions (并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted:  ...

  3. POJ 2524 Ubiquitous Religions (幷查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted:  ...

  4. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  5. poj2524 Ubiquitous Religions(并查集)

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

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

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

  7. POJ2524——Ubiquitous Religions

    Ubiquitous Religions Description There are so many different religions in the world today that it is ...

  8. poj2524(简单并查集)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. 设计模式(二)The Observer Pattern 观察者模式

    问题引入 生成一个公告板显示当时的天气状况,当天气状况发生改变的时候公告板能够实时的更新. 模式定义 定义对象之间的一对多的依赖.当一个对象改变状态时,它的全部依赖者都会自己主动收到通知并自己主动更新 ...

  2. ZOJ3329之经典概率DP

    One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very ...

  3. Flashback Query、Flashback Table(快速闪回查询、快速闪回表)

    Flashback Query闪回查询 flashback query是基于undo表空间的闪回,与之相关的参数如下: SQL> show parameter undo NAME         ...

  4. MYSQL 体系结构图-log (踏雪无痕) (UC技术博客)

    http://www.cnblogs.com/chenpingzhao/category/690116.html http://www.cnblogs.com/chenpingzhao/p/51074 ...

  5. How To Cluster Rabbit-MQ--reference

    Foreword This explanation of clustering Rabbit-MQ assumes that you’ve had some experience with Rabbi ...

  6. window7 远程连接 拒绝访问

    windows7 远程连接 拒绝访问 ----------------------------- 找了很多网络文章,都没有解决问题. 然后突然: 用Administrator超级管理员修改了一下 想要 ...

  7. 学习《Spring 3.x 企业应用开发实战》Day-1

    Day-1 记录自己学习spring的笔记 提要:根据<Spring 3.x 企业应用开发实战>开头一个用户登录的例子,按照上面敲的. 1.项目分层

  8. warning:1071 (42000) Specified key was too long;max key length is 1000 bytes

    原因是mysql字段长度设置的太长了, 从而导致mysql在建立索引时,索引长度超过了mysql默认许可的长度 默认 Innodb 允许长度为 767 MyISAM 允许长度为 1000 官方说明 如 ...

  9. Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.

    查看了进程, nginx, php-fpm都在运行, 排除程序错误, 那么就是配置的问题了. 一个可能的错误, 是由于配置中的 fastcgi_pass 配置错了 错误的配置如下 server { l ...

  10. Css四种样式

    1. 2 3 4 5 6.