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

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

 
  并查集,入门题
  题意
  一个校园内的所有学生都有宗教信仰,现在要做一个调查,你不能直接问每一个学生,但是可以通过询问他的朋友是否和他的宗教信仰一样,最后需要统计出这个校园内有多少不同的宗教信仰。
  输入有多组测试数据,每组开头是两个数,n和m,分别表示学校有n名学生和m组询问。后面是m行询问。最后当n和m都为0的时候停止。
  思路
  很明显的并查集问题。将一样的宗教信仰的集合合并,最后写一个for循环统计还有多少不同的信仰即可。
  注意
  每一个人都有宗教信仰,也就是说某一个编号没有在m组询问中出现,不要忽略它,他也是一个信仰。
  代码
 #include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 50010 int par[MAXN]; //par[x]表示x的父节点
int n; void Init() //初始化
{
int i;
for(i=;i<=n;i++)
par[i] = i;
} int Find(int x) //查询x的根节点并路径压缩
{
if(par[x]!=x)
par[x] = Find(par[x]);
return par[x];
} void Union(int x,int y) //合并x和y所在集合
{
par[Find(x)] = Find(y);
} int main()
{
int m,x,y,i,Case=;
while(scanf("%d%d",&n,&m)!=EOF){
if(n== && m==)
break;
//初始化
Init();
//m次询问
while(m--){
scanf("%d%d",&x,&y);
Union(x,y);
}
int ans = n;
for(i=;i<=n;i++) //统计
if(par[i]!=i)
--ans;
printf("Case %d: %d\n",Case++,ans);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 2524:Ubiquitous Religions(并查集,入门题)的更多相关文章

  1. [ACM] POJ 2524 Ubiquitous Religions (并查集)

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

  2. POJ 2524 Ubiquitous Religions (幷查集)

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

  3. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  4. poj 2524 Ubiquitous Religions(并查集)

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

  5. poj 2524 Ubiquitous Religions 一简单并查集

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

  6. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

  7. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

  8. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  9. POJ 2524 Ubiquitous Religions

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

随机推荐

  1. HEOI 2016 游记

    闲来无事,把这玩意儿补上. OI生涯中第一次正经的考试.挂的很惨. Day -1 不小心把机油(雾)sm惹毛了. 好像没啥别的事儿. Day 0 说好了上午直接去机房,然而临时说让我们上完前两节课再去 ...

  2. 3.6---双栈排序(CC150)

    答,课本上的方法比较好. public static Stack<Integer> sort(Stack<Integer> s) { Stack<Integer> ...

  3. django ATOMIC_REQUESTS

    在数据库配置中,如果配置了此属性为True,如下: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.xxx', 'NAME': '', ...

  4. MFCC可视化

    大多数文章和博客介绍都是MFCC的算法流程,物理意义,这里仅仅从数据分布可视化的角度,清晰 观察MFCC特征在空间中的分布情况,加深理解. MFCC处理流程: MFCC参数的提取包括以下几个步骤: 1 ...

  5. [20160707]Java中如何关闭Frame窗口

    用户类继承自WindowAdapter,而窗口对象Frame作为用户类的数据成员. 1 import java.awt.*; import java.awt.event.*; public class ...

  6. js弹出提示信息,然后跳转到另一页面

    <script language="javascript">  alert("您的用户名与密码已成功修改!");  document.locatio ...

  7. Java实验四和实验五

    实验四 类的继承性和多态性 [开发语言及实现平台或实验环境] Windows2000 或XP,JDK1.6与Jcreator4.0 [实验目的] 1.  掌握OOP方式进行程序设计的方法, 2.  了 ...

  8. ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)

    题意:大致就是要求画出这个有规律的Fractal图形了= = 例如 1 对应 X 2 对应 X  X   X    X  X 这个题是个理解分治法很典型的例子(详情请参见Code) 分治法:不断缩小规 ...

  9. HDU 4865 Peter's Hobby(概率、dp、log)

    给出2个影响矩阵,一个是当天天气对湿度的影响,一个是前一天天气对当天天气的影响. 即在晴天(阴天.雨天)发生Dry(Dryish.Damp.Soggy)的概率,以及前一天晴天(阴天.雨天)而今天发生晴 ...

  10. 修改EsayUi 中 tree 的原有样式,变为according 之类的样式 ,且子菜单显示在右侧

    easyUi 中 tree 框架的属性有: 修改原有展开样式代码如下: onExpand:function(node,param){ $(this).children("li"). ...