Time limit5000 ms

Memory limit65536 kB

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0. 

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint

Huge input, scanf is recommended.
 
题意:几个数连起来,问你有几个块,就是问并查集里面有几个集合
 
题解:并查集的模版题,注意Hint中的用scanf就好了
 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define PI 3.14159265358979323846264338327950 int father[],n,m; void init(int n)
{
for(int i=;i<n;i++)
{
father[i]=i;
}
} int find(int x)
{
if(father[x]==x)
return x;
else
return father[x]=find(father[x]);
} void combine(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
father[x]=y;
} int main()
{
int sum=;
while(scanf("%d %d",&m,&n) && (m||n))
{
init(m);
int a,b,total=;
sum++;
for(int i=;i<n;i++)
{
scanf("%d %d",&a,&b);
combine(a,b);
}
for(int i=;i<m;i++)
if(father[i]==i)
total++;
printf("Case %d: %d\n",sum,total); }
}

poj-2524 ubiquitous religions(并查集)的更多相关文章

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

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

  2. POJ 2524 Ubiquitous Religions (幷查集)

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

  3. poj 2524 Ubiquitous Religions (并查集)

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

  4. poj 2524:Ubiquitous Religions(并查集,入门题)

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

  5. poj 2524 Ubiquitous Religions 一简单并查集

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

  6. poj 2524 Ubiquitous Religions(并查集)

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

  7. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

  8. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

  9. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  10. POJ 2524 Ubiquitous Religions

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

随机推荐

  1. ASM 磁盘组的的scrip

    之前经常用如下方式进行查询:步骤 1 以oracle用户登录系统.步骤 2 执行如下命令改变ORACLE_SID环境变量.$ export ORACLE_SID=+ASM1[1或者2]需要通过ps - ...

  2. 算法导论课后习题解答 第一部分 练习1.1-1->1.1-5

    很高兴能和大家一起共同学习算法导论这本书.笔者将在业余时间把算法导论后面的题解以博文的形式展现出来希望能得到大家的支持谢谢.如果有可能我会做一些教学视频免费的供大家观看. 练习题选自算法导论中文第三版 ...

  3. Smarty中的请求变量和保留变量的使用范例

    PHP中提供的超全局数组 Smarty中对应的请求变量 $_GET               <{$smarty.get}> $_POST                         ...

  4. React日常注意点

    1.在定义组件的时候,名字首字母一定要大写 最近刚学,我会持续把自己学习过程中的的注意点在这里逐个列出,敬请等待!

  5. Objective-C Inheritance

    One of the most important concepts in object-oriented programming is that of inheritance. Inheritanc ...

  6. C# 初始学习心情

    当听说需要转做.net的时候.内心是忐忑不安的.因为突然从前端转向后端,几乎完全颠倒了...一个注重界面实现功能.一个注重逻辑的开发,然并卵,服从需求吧. 虽说公司需要你转.但是时间是不允许的,所以只 ...

  7. 小目标 | DAX高级实践-Power BI与Excel联合应用

    · 适用人群:数据分析专业人士,在数据分析方向需求发展人士 · 应用场景:数据汇报.数据可视化展现.数据建模分析 · 掌握难度:★★★★☆ 本期讲师 DAX高级实践-Power BI与Excel联合应 ...

  8. python3发送邮件01(简单例子,不带附件)

    # -*- coding:utf-8 -*-import smtplibfrom email.header import Headerfrom email.mime.text import MIMET ...

  9. LR脚本录制方式说明

    1.LR脚本录制方式说明1)HTML-based script基于HTML的脚本从内存中读取并下载资源,较少的关联处理,可以加入图片检查,回放时需要解析返回的信息a-基于用户行为的方式 web_lin ...

  10. [windows]窗口文件夹中使用常见任务

    文件夹中使用常见任务,如截图所示增加红色框部分. 设置步骤: 我的电脑--〉右键--〉属性--〉高级选项--〉性能设置--〉自定义:勾选在文件夹中使用常见任务.