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
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; int pre[];
bool t[]; int _find(int x)
{
int r=pre[x];
while(r!=pre[r])
r=pre[r];
int i,j;
i=x;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
} void mix(int x,int y)
{
int fx=_find(x),fy=_find(y);
if(fx!=fy)
pre[fy]=fx;
} int main()
{
int n,m,a,b,ans,CASE=;
while(cin>>n>>m)
{
if(n==&&m==) break;
ans=;
CASE++;
for(int i=;i<=n;i++)
pre[i]=i;
for(int i=;i<=m;i++)
{
cin>>a>>b;
mix(a,b);
}
memset(t,false,sizeof(t));
for(int i=;i<=n;i++)
t[_find(i)]=true;
for(int i=;i<=n;i++)
if(t[i]==true)
ans++;
printf("Case %d: %d\n",CASE,ans); }
return ;
}

POJ2524:Ubiquitous Religions (并查集模板)的更多相关文章

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

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

  2. POJ2524 Ubiquitous Religions(并查集)

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

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

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

  4. POJ 2524 Ubiquitous Religions (幷查集)

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

  5. poj 2524 Ubiquitous Religions (并查集)

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

  6. POJ-图论-并查集模板

    POJ-图论-并查集模板 1.init:把每一个元素初始化为一个集合,初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变). void init() { for ...

  7. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  8. 【2018寒假集训Day 8】【并查集】并查集模板

    Luogu并查集模板题 #include<cstdio> using namespace std; int z,x,y,n,m,father[10001]; int getfather(i ...

  9. 【并查集模板】 【洛谷P2978】 【USACO10JAN】下午茶时间

    P2978 [USACO10JAN]下午茶时间Tea Time 题目描述 N (1 <= N <= 1000) cows, conveniently numbered 1..N all a ...

  10. 【并查集模板】并查集模板 luogu-3367

    题目描述 简单的并查集模板 输入描述 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1时,将Xi与Yi所在的集合合并 当Zi=2时,输出 ...

随机推荐

  1. haoce修改mysql

    修改时长余额 select * from sys_user_product up where up.user_id in(select u.id from sys_user u where login ...

  2. Android系统签名文件

    系统签名所需要的工具 signapk.jar -------------------->out/host/linux-x86/framework/signapk.jar (build/tools ...

  3. Oracle第二天

    Oracle第二天 整体安排(3天) 第一天:Oracle的安装配置(服务端和客户端),SQL增强(单表查询). 第二天:SQL增强(多表查询.子查询.伪列-分页),数据库对象(表.约束.序列),Or ...

  4. Java web项目综合练习(Estore)

    Java web项目综合练习(Estore) 复习day18: ajax代码的书写步骤 2)json格式文本,转js对象的方法是那个 项目开发流程介绍 这里学习的JavaWEB项目实战,主要是把前面学 ...

  5. 网络最大流最短增广路Dinic算法模板

    #include<cstdio> #include<cstring> #include<string> #include<cmath> #include ...

  6. UVa 679 Dropping Balls (例题 6-6)

    传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D ...

  7. CSS3秘笈复习:第六章

    第六章 1.文本大写化: text-transform:uppercase; 另外三种选项是:lowercase或capitalize以及none. 2.文本修饰: text-decoration:u ...

  8. osg + cuda

    #include <osg/Notify> #include <osgViewer/Viewer> #include <osgCompute/Memory> #in ...

  9. JS学习之路,之弹性运动框架

    弹性运动:顾名思义,就如同物理中的加速减速运动,当开始时速度过大,到达终点时,速度不会立刻停下,而是再前进一段距离,而后再向相反方向运动,如此往复. var timer=null; var speed ...

  10. TortoiseGit - pull request

    有一个仓库,叫Repo A.你如果要往里贡献代码,首先要Fork这个Repo,于是在你的Github账号下有了一个Repo A2,.然后你在这个A2下工作,Commit,push等.然后你希望原始仓库 ...