题目链接

分析:

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath> using namespace std; const int maxn = + ; int p[maxn]; int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } int main(){
int n, m, a, b, kase = ; while(scanf("%d%d", &n, &m) == ) {
if(n == && m == ) break;
for(int i=; i<=n; i++) p[i] = i; int cnt = ; for(int i=; i<m; i++) {
scanf("%d %d", &a, &b); int x = find(a), y = find(b);
if(x != y) {
p[x] = y;
}
} for(int i=; i<=n; i++) {
if(p[i] == i) cnt++;
} printf("Case %d: ", ++kase);
printf("%d\n", cnt);
} return ;
}

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. Structual设计--Flyweight模式

    1.意图 运用共享技术有效地支持大量细粒度的对象. 2.别名 无 3.动机 有些应用程序得意于在其整个设计过程中採用对象技术,但简单化的实现代价极大.如我们在使用word的时候.假设设置正文字体为:t ...

  2. 算法9-5:最大流算法的Java代码

    残留网络 在介绍最大流算法之前先介绍一下什么是残留网络.残余网络的概念有点类似于集合中的补集概念. 下图是残余网络的样例. 上面的网络是原始网络.以下的网络是计算出的残留网络.残留网络的作用就是用来描 ...

  3. [PWA] 16. Clean IDB

    When we save items to IndexDB, we need to think about clean the items or keep those in a short list. ...

  4. S2SH框架集成详解(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)

    近期集成了一次较新版本的s2sh,出现了不少问题,网上资料也是良莠不齐,有的甚至就是扯淡,简单的把jar包扔进去就以为是集成成功了,在这里整理一下详细的步骤,若哪位有什么不同看法,可以留言,欢迎批评改 ...

  5. BeanFactory学习

    关于BeanFactory,一个相对易懂的博客,关于深入的理解,后续继续学习 http://www.cnblogs.com/liuling/archive/2013/04/14/BeanFactory ...

  6. css.day05

    1. 外边距合并  不是bug  而是一个特性  (以最大的那个边距为准) 两个盒子是并列关系 两个盒子 父子级关系 1. border 2.overflow:hidden; 3. padding  ...

  7. Linq101-Ordering

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Orderin ...

  8. java中判断两个字符串是否相等的问题

    我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...

  9. 樱花雨 www.yinghy.com

    自己刚申请的域名,和主机,以后就用这个来试着做点东西

  10. Struts1 中实现Action跳转地址栏变化的方法

    Action进行跳转,有时候地址栏不变化,如果重复刷新就会重复提交, 这里一般需要进行重定向: 1.在xml里面进行配置 <action path="/checkCdconfirmEn ...