题目链接

http://poj.org/problem?id=2524

题意

有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a、b,表示同学a和同学b有相同的信仰,求在n名学生中最多存在多少种不同的宗教信仰。

思路

使用并查集解决。

代码

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = + ;
int p[N]; void make_set(int n)
{
for (int i = ;i <= n;i++)
p[i] = -;
} int find_root(int i)
{
if (p[i] == -)
return i;
else
{
int temp = find_root(p[i]); //路径压缩
p[i] = temp;
return temp;
}
} void union_set(int a, int b)
{
int ra = find_root(a);
int rb = find_root(b);
if (ra != rb)
p[ra] = rb;
} int main()
{
//freopen("poj2524.txt", "r", stdin);
int n, m;
int cnt = ;
while (scanf("%d%d", &n, &m) == && n)
{
make_set(n);
int a, b;
for (int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
union_set(a, b);
}
int ans = ;
for (int i = ;i <= n;i++)
if (p[i] == -) ans++;
printf("Case %d: %d\n", ++cnt, ans);
}
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. POJ2524 Ubiquitous Religions(并查集)

    题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...

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

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

  4. POJ 2524 Ubiquitous Religions (幷查集)

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

  5. poj 2524 Ubiquitous Religions (并查集)

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

  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. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  2. 微信网页动画---swiper.animate.css

    项目需要,自己写了个demo <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  3. 判断html是否含有图片

    核心代码: $url="http://XXXXX/article/012.html"; $content=file_get_contents($url); //读取文章页面源代码 ...

  4. AAC编码

    1. 前言 如果说目前H.264是视频CODEC的实际霸主,那么AAC就是音频CODEC的女王.主流的音视频格式都是H.264搭配AAC,无论是非实时的媒体文件还是实时的媒体流. 2. AAC历史 A ...

  5. Mysql查看建表语句以及修改引擎

    更多内容推荐微信公众号,欢迎关注: 1 查看系统支持的存储引擎 show engines; 2 查看表使用的存储引擎 两种方法: a.show table status from db_name wh ...

  6. 系统学习(javascript)_基础(数据类型一)

    五种基本数据类型:Number,String,Boolean,Null,Undefind: 三种引用数据类型:Object,Array,Symbol: Symbol为ECMAScript6新增的数据类 ...

  7. tensorflow环境安装

    tensorflow环境安装1.安装虚拟机Virtrualbox下载地址:https://www.virtualbox.org/wiki/Downloads 2.下载安装Ubuntu镜像下载地址:ht ...

  8. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

  9. scp加端口号

    scp -P 21110 root@192.168.0.1:/home/abc.txt root@192.168.0.2:/root 注意: 参数-P 的位置一定要紧跟在scp命令后面 参数-P 指的 ...

  10. linux通配符,grep和 egrep区别

    其实主要是正则表达式中的一些特殊语法.在网上找的几篇文章,截取相关部分贴在了下面,方便以后翻阅. 参考:http://hi.baidu.com/sei_zhouyu/item/c18e1a950d2e ...