//#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#define max1 50005
using namespace std;
int pa[max1],vis[max1];
int find(int a)
{
while(a!=pa[a])
{
pa[a]=pa[pa[a]];
a=pa[a];
}
return a;
}
void build(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
if(fa<fb)
pa[fb]=fa;
else
pa[fa]=fb;
}
}
int main()
{
int n,m;
int t=;
while((scanf("%d%d",&n,&m))&&(n+m!=))
{
int a,b;
for(int i=;i<=n;i++)
{
pa[i]=i;
vis[i]=;
}
for(int i=;i<m;i++)
{
cin>>a>>b;
build(a,b);
}
int ans=;
for(int i=;i<=n;i++)
{
if(!vis[find(i)])
{
ans++;
vis[find(i)]=;
}
}
printf("Case %d: %d\n",t++,ans);
}
return ;
}
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 30197   Accepted: 14605

Description

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

poj 2524 并查集 Ubiquitous Religions的更多相关文章

  1. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  2. POJ 2524 并查集

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

  3. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  4. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  5. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  7. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  8. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  9. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

随机推荐

  1. SecureCRT光标颜色

    SecureCRT连linux光标一直没有,尤其是在vim编辑文档的时候特别麻烦,今天找出解决办法: 选项->会话选项->仿真:将ANSI颜色选中: 选项->会话选项->外观: ...

  2. 模板插件aTpl.js新增功能

    摘要: aTpl.js是一款模板插件,该插件支持ie5+,chrome等浏览器以及移动端浏览器,支持for和if语法,以及表达式.最近对aTpl.js模板插件增加了新的功能,支持字符串模板,同时增加了 ...

  3. sql数据库批量替换dedecms内容关键字

    之前写了一篇dedecms后台批量替换文章中的关键词,这边我们介绍一下用sql数据库批量替换dedecms内容关键字,当然要求你对数据库比较熟悉,修改前请自行做好备份. 1.更改文章中的内容 upda ...

  4. js实现把网页table导成Excel

    //导出excel function exportExcel(DivID,strTitle){ if(DivID==null) { return false; } var jXls, myWorkbo ...

  5. iso socket基础2

    iPhone socket 编程之BSD Socket篇 收藏在进行iPhone网络通讯程序的开发中,不可避免的要利用Socket套接字.iPhone提供了Socket网络编程的接口CFSocket, ...

  6. python如何安装pip和easy_installer工具

    1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads 2.解压安装 3.下载Windows的easy installer,然后安装 ...

  7. FastMM的安装方法

    FastMM 快速在D2006和2007中已代替了原来的内存管理器.D7也可以使用,而且很方便哦.请看步骤: 1. FastMM是开源项目,去她老家先拖个来. http://sourceforge.n ...

  8. urllib2加入header及解决中文乱码问题

    def main(): url = "http://www.douban.com" #伪装桌面浏览器 headers = {'User-Agent':'Mozilla/5.0 (W ...

  9. 解决Cannot change version of project facet Dynamic web module to 3.0

    我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...

  10. iOS 中constraint 不等于约束和低优先级约束使用的简单体会

    看了些文章发现,在使用constraint时,不等于约束往往是和低优先级约束成对使用的,这样才能实现他们的效果. 看看例子 下面是在3.5存屏幕下的效果 图1,竖屏,在满足>=50的前提下,可以 ...