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 ;
}

POJ2524——Ubiquitous Religions的更多相关文章

  1. poj2524 Ubiquitous Religions(并查集)

    题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...

  2. poj-2524 ubiquitous religions(并查集)

    Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...

  3. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  4. POJ2524 Ubiquitous Religions(并查集)

    题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...

  5. poj 2524:Ubiquitous Religions(并查集,入门题)

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

  6. POJ 2524 Ubiquitous Religions

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

  7. Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏

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

  8. poj 2524 Ubiquitous Religions(宗教信仰)

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

  9. POJ 2524 :Ubiquitous Religions

    id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...

随机推荐

  1. 怎样把SEL放进NSArray里

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3805270.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  2. Linux 文件系统 相关

    鸟个讲得很详细啦:http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem_1.php 重要知识点:磁盘,文件系统,超级区块 (superblock ...

  3. Linux C 程序 预处理,结构体(13)

    C语言预处理,结构体 C语言预处理命令1.宏定义 1.无参数宏 #define 标识符 字符串 #代表本行是编译预处理命名 习惯上,宏定义大写 代替一个字符串,介绍重复书写某个字符串的工作量 有意义的 ...

  4. qml去标题栏

    只要加入"flags: Qt.Window | Qt.FramelessWindowHint "属性就可实现去标题栏. 注意:在使用这个属性的时候要先导入QtQuick.Windo ...

  5. 鼠标滑过提示title

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1. ...

  6. WinForm多线程及委托防止界面假死

    当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决. using System; using System.Collections.Generi ...

  7. c#winform,制作可编辑html编辑器

    大神勿喷,新手记笔记 材料 网上下载kindeditor,动手在写个htmldome,图中的e.html.然后全部扔到了bin/debug下面,(x86是要扔到bin/x86/debug) 中间bod ...

  8. document.compatMode的CSS1compat

    document.compatMode BackCompat:标准兼容模式关闭.浏览器宽度:document.body.clientWidth: CSS1Compat:标准兼容模式开启. 浏览器宽度: ...

  9. Nat网络地址转换

    Nat中的术语 -------------------------------------------------------------------------------------------- ...

  10. 2014年辛星完全解读Javascript第八节 json

    json是JavaScript Object Notation的简写,它是一种轻量级的数据交换格式,而且表达上很容易靠字面去理解.json是用于存储和传输数据的格式,通常用于向服务器端传递数据. ** ...