题目链接

分析:

给定 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. POJ - 1170 Shopping Offers (五维DP)

    题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...

  2. Java并发学习之二——获取和设置线程信息

    本文是学习网络上的文章时的总结,感谢大家无私的分享. Thread类的对象中保存了一些属性信息可以帮助我们辨别每个线程.知道它的一些信息 ID:每一个线程的独特标示: Name:线程的名称: Prio ...

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

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

  4. 从零开始学习jQuery-------jQuery元素选择器(三)

    下面我们来介绍一下jQuery元素选择器,在Web开发中我们最常用的操作是获取元素,然后对获取的元素进行一系列的操作,jQuery根据获取页面元素的不同,可以将jQuery选择器分为四大类:基本选择器 ...

  5. java的通信机制

    通信机制无非就那几种:http访问.socket访问: http又分为:jsp.servlet.html,用的就是get和post方法 socket则可分为:tcp或者udp方式 从以上内容又衍生出其 ...

  6. Android运行时注解

    Android的注解有编译时注解和运行时注解,本文就介绍下运行时注解. 其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用fin ...

  7. POJ 3162 Walking Race(树的直径+单调队列)

    题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...

  8. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5456   Acc ...

  9. 使用out来返回多个值

    一般的方法中,一次只能有一个返回值.但是当我们需要一个方法给我们返回多个值得时候应该怎么做呢?这时候可以使用out来修饰参数. out介绍: static void Main(string[] arg ...

  10. Oracle10g、 Oracle11g完美共存

    Oracle10g. Oracle11g完美共存           环境描述 客户服务器上已经安装Oracle9i软件并部署多套数据库,现在客户要求安装Oracle11g软件,并且创建11g数据库, ...