题目链接

分析:

给定 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. [Angular 2] Custom Validtors

    Create a custom validtor which only accepts the string start with '123'; function skuValidator(contr ...

  2. Android 开源项目 eoe 社区 Android 客户端(转)

    本文内容 环境 开源项目 eoe 社区 Android 客户端 本文介绍 eoe 社区 Android 客户端.它是一个开源项目,功能相对简单,采用侧边菜单栏.可以学习一下.点击此处查看 GitHub ...

  3. 解决Ubuntu 14.04 下SMPlayer的字幕乱码问题

    1.SMPlayer播放器 对于使用ubuntu系统的同学,虽然系统初始就带有vedio播放器,但是这里强烈推荐SMPlayer,直接在Ubuntu Software Center中就可以免费下载安装 ...

  4. Traceview 性能分析工具

    简介 TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method.详细内容参考:http://deve ...

  5. HTML (1)href与Action,get post

    1.   href与Action的区别 href只能get参数,action能get参数又能post参数 href一般用于单个连接,可以带参数(URL重写),是采用get方式请求的,在地址栏中可以看到 ...

  6. java org.apache.struts.taglib.html.BEAN 没有找到

    index.jsp <body> <a href="login2.do">登陆(struts标签)</a><br> </bod ...

  7. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  8. 局部线性嵌入(LLE)原理总结

    局部线性嵌入(Locally Linear Embedding,以下简称LLE)也是非常重要的降维方法.和传统的PCA,LDA等关注样本方差的降维方法相比,LLE关注于降维时保持样本局部的线性特征,由 ...

  9. JavaScript--对象-检查一个对象是否是数组

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. android 06

    1.android原理 菜单-->MainActivity-->onCreate-->setContentView(R.layout.item)-->layout(item.x ...