Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 23390   Accepted: 11527

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.

Source

 
#include <stdio.h>
#include <string.h>
int pre[];
int find(int n)
{
int i,t;
t = n;
while(t!=pre[t])
t=pre[t];
while(t!=pre[n])
{
i = pre[n];
pre[n]=t;
n = i;
}
return t;
}
int main()
{
int n,m,k=;
while(scanf("%d%d",&n,&m),n||m)
{
int i,j,a,b,pa,pb,total=n;
//memset(s,0,sizeof(s));
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pa]=pb;
total--;
}
}
printf("Case %d: ",k++);
printf("%d\n",total);
}
return ;
}

并差集的简单应用

poj_2524_Ubiquitous Religions_201407211506的更多相关文章

随机推荐

  1. UWP Windows10开发更新磁贴和动态更新磁贴

    下面将介绍两种方式如何在windows10 uwp开发中如何更新应用磁贴: 实际上windows的磁贴就是用xml实现的,你只需要创建相应格式的xml就可以实现动态磁贴了 一,手动更新磁贴 二,轮询更 ...

  2. AJPFX关于面向对象中的对象初始化整理,综合子父类、代码块等等

    今天总结了一下子父类当中含有静态代码块.代码块.构造函数.成员变量.子类复写父类方法时子类的初始化过程,把思路理清一下 class Fu { //父类成员变量 private int num = 3; ...

  3. 开源项目:JEECG

    工程下载:https://github.com/zymqqc/jeecg-1

  4. Android学习笔记(十四) Handler理论补充

    一.如何下载Android源码 在SDK Manager中选中Sources for Android SDK. 二.ThreadLocal初步介绍 1)执行ThreadLocal对象(static f ...

  5. Java集合框架源码(二)——hashSet

    注:本人的源码基于JDK1.8.0,JDK的版本可以在命令行模式下通过java -version命令查看. 在前面的博文(Java集合框架源码(一)——hashMap)中我们详细讲了HashMap的原 ...

  6. jQuery动画处理

    $(selector).hide(speed,callback);隐藏 $(selector).show(speed,callback);显示 $(selector).toggle(speed,cal ...

  7. 【原创】DESTOON做中英双语言(多语言)切换版本具体详解

    第一次发原创好激动,该注意点什么? 在开发过程中用户有许多要求,比如这个多语言切换就是一个需求. 首先讲解一下DESTOON(DT)后台系统如何做这个中英.甚至多语言切换的这个功能. DT本身不自带多 ...

  8. Spring Cloud练习1

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  9. zabbix4.2学习笔记--监控nginx

    图解一个客户端连接开源版本的Nginx情况 Accepts(接受).Handled(已处理).Requests(请求数)是一直在增加的计数器.Active(活跃).Waiting(等待).Readin ...

  10. No-5.变量的命名

    变量的命名 目标 标识符和关键字 变量的命名规则 0.1 标识符和关键字 1.1 标识符 标示符就是程序员定义的 变量名.函数名 名字 需要有 见名知义 的效果 标示符可以由 字母.下划线 和 数字  ...