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

Hint

Huge input, scanf is recommended.
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=+;
int f[maxn];
int find(int x) {
return f[x]==x?x:f[x]=find(f[x]);
}
void init(int x) {
for(int i=;i<=x;i++) f[i]=i;
}
void merge(int x,int y) {
int a=find(x);
int b=find(y);
if(a!=b) f[a]=b;
}
int main() {
// freopen("input.txt","r",stdin);
int n,m;
int cas=;
while(scanf("%d%d",&n,&m)!=EOF) {
init(n);
if(n==&&m==) break;
for(int i=;i<=m;i++) {
int a,b;
scanf("%d%d",&a,&b);
merge(a,b);
}
int ans=;
for(int i=;i<=n;i++) {
if(f[i]==i) ans++;
}
printf("Case %d: %d\n",cas++,ans);
}
return ;
}

zoj 2524 并查集裸的更多相关文章

  1. POJ 2524 并查集

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

  2. Connections in Galaxy War ZOJ - 3261 (并查集)

    点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...

  3. poj 2524 并查集 Ubiquitous Religions

    //#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...

  4. HDU 1213 How Many Tables(并查集裸题)

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  5. kuangbin带我飞QAQ 并查集

    1. POJ 2236 给出N个点,一开始图是空白的,两个操作,一个是增加一个点(给出坐标),一个是查询两个点间是否相通,当两点间的距离小于D或者两点通过其他点间接相连时说这两个点相通.并查集维护,每 ...

  6. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  7. [NOI2002] 银河英雄传说 (带权并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  8. HDU - 1232 畅通工程-并查集模板

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  9. BZOJ 4551 HEOI 2016 树 (并查集)

    思路: 考虑时光倒流 这不就是并查集裸题了-----. //By SiriusRen #include <cstdio> #include <cstring> #include ...

随机推荐

  1. Java之for循环嵌套练习

    1.打印4层*** ***** ***** ***** ***** class forfor{ public static void main(String[] args){ for(int y=0; ...

  2. 关于Boost在工程下的配置

    http://blog.csdn.net/u012333003/article/details/38078655# 视图->属性页(VS2013在这才能找到相关的属性配置栏)->C/C++ ...

  3. Shadow Properties之美(二)【Microsoft Entity Framework Core随笔】

    接着上一篇Shadow Properties之美(一),我们来继续举一个有点啰嗦的栗子. 先看简单需求:某HR系统,需要记录员工资料.需要记录的资料有: 员工号(规则:分公司所在城市拼音首字母,加上三 ...

  4. VM虚拟机拍摄快照时出错或者克隆失败解决办法

    在换了固态硬盘后,下载好VM,装虚拟机,结果克隆虚拟机和拍摄快照时出问题了. 拍摄快照时出错或者隆失败出现参数错误如图: 所提供的参数中有一个无效参数 解决办法 出现这个问题一般是在机械硬盘的电脑上面 ...

  5. OTP&ETS

    最近觉得实在是该梳理梳理erlang的框架内容了,所以整理了下. OTP(开放电信平台):并发系统平台, 特点:容错(erlang:get_stacktrace(),try-catch,trap_ex ...

  6. dict的几个要点

    1. 采用key,value键-值对进行存储 2. key必须是不可变对象 3. key值不能重复 添加元素: aDict = {'1':'aaa','b':'bbb','3':'ccc'} aDic ...

  7. 第九周学习笔记-ADO.Net中DataSet的应用

    一.知识点描述 1.含义:DataSet是ADO.Net的断开式结构的核心组件,它可以用于多种不同的数据源,用于XML数据,或用于管理应用程序本地的数据.DataSet包含一个或多个DataTable ...

  8. centos6 升级安装openssh7

    1.准备工作: 查看下当前SSH版本: [root@bogon ~]# ssh -V OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013 或者 [root@b ...

  9. ATL编程初级教程(图文事例)(VC6)

    http://blog.csdn.net/titilima/archive/2004/07/18/44273.aspx 介绍 本教程的目的是告诉你如何使用ATL创建一个COM服务器,并使用Visual ...

  10. Vasya and Binary String(来自codeforces

    题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...