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. 你早该这么玩Excel 读书笔记

    1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...

  2. JS 正则 获取URL参数

    function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  3. java应用uploadify 3.2丢失session

    java应用uploadify 3.2丢失session http://c-bai.iteye.com/blog/1829269 uploadify上传用的是一个flash插件. flash中有个bu ...

  4. DTcms 扩展字段标签调用

    前台模版: 文章列表:{dr[author]} 文章内容{model.fields[author]} 点击数 后台CS文件:model.fields["author"].ToStr ...

  5. CentOs7&zookeeper

    下载安装包 wget http://www.apache.dataguru.cn/zookeeper/stable/zookeeper-3.4.6.tar.gz 解压 tar xzvf zookeep ...

  6. laravel--上传

    1.视图里面 2.控制器里面 //收集表单提交数据 $input = $request->all(); //查看是否有图片提交上来 if ($request->hasFile('title ...

  7. 【转】【C#】无边框窗体移动的三种方法

    1. 重写WndProc protected override void WndProc(ref Message m) { const int WM_NCHITTEST = 0x84; const i ...

  8. PHP 归并排序

    在我们日常的程序开发时候,有时候需要对一个已知的集合按照一定的规则进行排序,其实当数据的规模不太大时或者数据的有序特征比较明显,其实我们可以采用其它的排序算法例如:Bubble Sort, Inser ...

  9. SDP协议分析

    一.SDP协议介绍 SDP 完全是一种会话描述格式 ― 它不属于传输协议 ― 它只使用不同的适当的传输协议,包括会话通知协议(SAP).会话初始协议(SIP).实时流协议(RTSP).MIME 扩展协 ...

  10. 文件大小的友好输出及其 Python 实现

    在数据库中存储时,使用 Bytes 更精确,可扩展性和灵活性都很高. 输出时,需要做一些适配. 1. 注意事项与测试代码 需要考虑 sizeInBytes 为 None 的场景. 除以 1024.0 ...