Ubiquitous Religions

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
题目大意:有n个学生,每个都有信仰的宗教。。。 有m组数据(i,j)表示i与j同学是同一个宗教的。 输出有多少个宗教。
解题思路:简单的并查集。
Code:
 #include<stdio.h>
#include<math.h>
int fat[];
int find(int x)
{
if (x!=fat[x])
fat[x]=find(fat[x]);
return fat[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx!=fy)
fat[fx]=fy;
}
int main()
{
int n,m,times=,i,t1,t2,cnt;
while (scanf("%d %d",&n,&m)!=EOF)
{
times++;
cnt=;
if (n==&&m==) break;
for (i=; i<=n; i++)
fat[i]=i;
for (i=; i<=m; i++)
{
scanf("%d %d",&t1,&t2);
join(t1,t2);
}
for (i=; i<=n; i++)
if (fat[i]==i) cnt++;
printf("Case %d: %d\n",times,cnt);
}
return ;
}

最新文章

  1. CSS3的calc()使用
  2. 发现的eval的一个小问题
  3. echo使用说明,参数详解
  4. bash read命令用法
  5. IoGetDeviceObjectPointer .
  6. MVC开发Markdown编辑器(1)
  7. Java script 看看黑客怎么写的
  8. 目前项目wordpress插件记录
  9. arTemplate解析语法
  10. Android热修复技术原理详解(最新最全版本)
  11. 洛谷P1209-最大公约数与最小公倍数问题
  12. Vue项目中jsonp抓取数据实现方式
  13. 【Codeforces 1137A】Skyscrapers
  14. 对oracle中SQL优化的理解
  15. 设置Eclipse具有字母自动联想
  16. MySQL Binlog和Relaylog生成和清理
  17. Swift图书展示项目笔记
  18. c++ A类包含B类指针,B类包含A类指针的情况
  19. &lt;转&gt;Win8.1+CentOS7 双系统 U盘安装
  20. keras—多层感知器MLP—MNIST手写数字识别

热门文章

  1. 重回cnblogs
  2. 基于FPGA的按键扫描程序
  3. IOS中一个简单的粒子效果实现
  4. System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
  5. C++ 对数组sizeof 和对数组元素sizeof
  6. Sublime Text 3 使用备注
  7. 链接中 href=&#39;#&#39; 和 href=&#39;###&#39; 的区别以及优缺点
  8. CentOS平台下为Python添加MongoDB支持PyMongo
  9. git命令行
  10. (转)《深入理解java虚拟机》学习笔记5——Java Class类文件结构