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. 基于spring-boot和docker-java实现对docker容器的动态管理和监控[附完整源码下载]

    ​ (我是个封面) docker简介 Docker 是一个开源的应用容器引擎,和传统的虚拟机技术相比,Docker 容器性能开销极低,因此也广受开发者喜爱.随着基于docker的开发者越来越多,doc ...

  2. 在IDEA中编辑struts国际化properties文件

    在IDEA中编辑struts国际化properties文件 如果手工创建的web工程,struts的i18n属性文件,可以使用native2ascii工具转换(记得命令行的第二个文件名是要保存的文件名 ...

  3. ios 开发最新屏幕适配

  4. Beginning Python Chapter 2 Notes

    Python基本数据类型用Python官方说法应该叫Python内建数据类型,英文叫built-in type.下面稍微总结了一下我看到过的Python内建数据类型. Python基本数据类型 数据类 ...

  5. Windows平台下如何在C#中调用Python

    最近迷上了Python,发现它能够做很多C#无法完成的事情,比如,调用CMD或者在CMD中执行一个exe文件命令行并获得输出的结果.过程简单,处理起来也非常方便,但如果要用C#调用Python文件呢, ...

  6. jsp四大作用域之Application

    <%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding=& ...

  7. BZOJ 4563: [Haoi2016]放棋子

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 389  Solved: 248[Submit][Status][Discuss] Descriptio ...

  8. Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)

    因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...

  9. nginx 的反向代理及缓存功能

    上游服务器的设置 server { #监听的IP及端口 listen 127.0.0.1:8080; #虚拟主机对硬解析的主机名 #server_name localhost; #charset ko ...

  10. tmpfs与内存盘

    如何快速的吃掉一段内存:通过tmpfs来划一片领地,再用dd去粗暴占用mount -t tmpfs -o size=512M tmpfs /mnt/demodd if=/dev/zero of=/mn ...