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. Space Shooter

    项目:https://pan.baidu.com/s/1jIDe2H4 using UnityEngine; using System.Collections; namespace VoidGame ...

  2. datables的基本操作

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. Apache Kafka开发入门指南(1)

    Apache Kafka可以帮助你解决在发布/订阅架构中遇到消费数百万消息的问题.如今,商业应用.社交应用以及其它类型的应用产生的实时信息在不断增长,这些信息需要以简单的方式快速.可靠地路由到各种类型 ...

  4. ManyToMany OrderBy

    有A.B表和A_B中间表,A_B中间表只有A.B表的id,想让A中的bs以date列排序: @Entity @Table(name="A") public class A impl ...

  5. Android启动另一个APP时,注意disable与enable的问题

    在写游戏sdk时候遇到了一个需要在sdk中通过scheme来启动支付宝的免密支付功能,所以需要在设备中通过包名检查一下支付宝是否存在. 此时遇到了一个问题,在三星设备中可以将app给处于disable ...

  6. 使用GNU/Linux播放电视节目

    目前,生活中很多事情都可以在电脑前完成,读书.写程序.听音乐.看视频等.如果也可以在电脑上收看有线电视节目的话,那就更好了.为此,我购买了圆刚视频采集卡AverMedia C725B.如下图所示. 官 ...

  7. SourceInsight - 常用设置和快捷键大全

    1. 让{ 和 } 不缩进 Options -> Document Options -> Auto Indenting -> Auto Indent Type 选 Simple 2. ...

  8. HDU 1863 Kruskal求最小生成树

    好久没写博客了写着玩的…… Kruskal这种东西离散都学过…… 一句话…… 添加当前图权值最小且构不成环的一条边 直到连接所有点…… 其他人好多Kruskal的模版 肯定有比我的好的…… 就是刷一波 ...

  9. redis的常用命令

    键值得相关命令: keys * 返回满足给定patternd的所有key exists 确认一个key是否存在 del 删除一个key expire 设置一个key的过期时间 ttl 查看过期时间还有 ...

  10. Neutron 不健全的HA ROUTER

    首先介绍下HA,所谓的HA就是高可用性,但HA有双主.主备两种工作模式,其中主备模式又包含抢占与 非抢占两种方式,而Neutron Router采用的无疑是HA中最简单的工作方式非抢占主备模式. HA ...