Ubiquitous Religions

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 23580 Accepted: 11609

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



Alberta Collegiate Programming Contest 2003.10.18

<span style="color:#6633ff;">***********************************************************************

     author    :   Grant Yuan
time : 2014.7.25
algorithm : 并查集 *********************************************************************** #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAX 50001 using namespace std;
int ans,par[MAX];
long long n,m;
int ct=1; void init()
{
for(int i=0;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
if(x==par[x]) return x;
else
return par[x]=find(par[x]);
} void unite(int x,int y)
{
int xx,yy;
xx=find(x);yy=find(y);
if(yy==xx) return;
par[yy]=xx;ans--; } int main()
{
while(1){
cin>>n>>m;int a,b;
if(n==0&&m==0) break;
init();
ans=n;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
unite(a,b);
}
printf("Case %d: %d\n",ct++,ans); }
return 0;
}
</span>

POJ 2524 并查集的更多相关文章

  1. poj 2524 并查集 Ubiquitous Religions

    //#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...

  2. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  3. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  4. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  5. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  6. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  7. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  8. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  9. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

随机推荐

  1. 线程 (detach的作用)

      线程状态在一个线程的生存期内,可以在多种状态之间转换.不同操作系统可以实现不同的线程模型,定义许多不同的线程状态,每个状 态还可以包含多个子状态.但大体说来,如下几种状态是通用的:       就 ...

  2. c++11 生产者/消费者

    下面是一个生产者消费者问题,来介绍condition_variable的用法.当线程间的共享数据发生变化的时候,可以通过condition_variable来通知其他的线程.消费者wait 直到生产者 ...

  3. openssl 非对称加密算法RSA命令详解

    1.非对称加密算法概述 非对称加密算法也称公开密钥算法,其解决了对称加密算法密钥分配的问题,非对称加密算法基本特点如下: 1.加密密钥和解密密钥不同 2.密钥对中的一个密钥可以公开 3.根据公开密钥很 ...

  4. 常见HTTP状态码的含义

    200 请求已成功,请求所希望的响应头或数据体将随此响应返回. 301 被请求的资源已永久移动到新位置. 302 请求的资源现在临时从不同的 URI 响应请求. 400 1.语义有误,当前请求无法被服 ...

  5. JS高级程序设计学习笔记之基本包装类型

    概述 基本类型:string.boolean.number 每当读取一个基本类型的值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据. 使用new操作符创建的 ...

  6. 关于asp.net的<%#%>的一些总结

    一.说明 asp特有的控件在前台绑定数据的语法,且必须要调用该控件的DataBind()方法才执行,也可以整个页面数据绑定. 二.注意 1.并不是只有服务器控件才可以使用该语法,当整个页面调用this ...

  7. 临时解决linux下time wait问题

     通过 netstat  -anp | grepTIME_WAIT | wc -l 命令查看数量,发现TIME_WAIT的连接数量超过了阈值   1.初步怀疑是程序没有关闭连接,codereview了 ...

  8. Silverlight Visifire控件 .net后台控制aspx页面控件的显示与隐藏,动态给控件赋值,选定默认值的设定

    .net后台代码: 控件的显示与隐藏: this.dateStart.Visibility = Visibility.Collapsed;//不显示控件 this.dateYear.Visibilit ...

  9. (三)backbone - API学习 - v0.9.2 与 v1.1.2区别

    Backbone.View v0.9.2 中Backbone.View 可以导出对象的options属性, v1.1.2 中去掉该属性,通过如下代码 viewOptions = ['model', ' ...

  10. window.onload,<body onload="function()">, document.onreadystatechange, httpRequest.onreadystatechang

    部分内容参考:http://www.aspbc.com/tech/showtech.asp?id=1256 在开发的过程中,经常使用window.onload和body onload两种,很少使用do ...