题目描述:

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.

Outout

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<cstdio>
using namespace std;
int a[500000];inline int search_root(int x)
{return x==a[x]?x:(search_root(a[x]));//递归查找到根
}
int main(void)
{int m,n;int student1,student2;int cas=0;int religions;while(cin>>m>>n){if(!m&!n)break;religions=m;   //一开始假设所有的人的宗教信仰都不相同 for(int i=1;i<=m;i++)  a[i]=i;//初始化,假设每个人宗教编号都不一样 for(int i=0;i<n;i++){cin>>student1>>student2;int root_point1=search_root(a[student1]);int root_point2=search_root(a[student2]);if(root_point1!=root_point2){a[root_point2]=root_point1;religions--;//每一次并集都会减少一个宗教数 }}cout<<"Case "<<++cas<<": "<<religions<<endl; }
}

Ubiquitous Religions POJ - 2524相关推荐

  1. Ubiquitous Religions (并查集)

    Ubiquitous Religions POJ - 2524 当今世界有许多不同的宗教,很难跟踪它们.你有兴趣找出你大学里有多少不同的宗教学生相信. 你知道你的大学里有 n 学生 (0 < n ...

  2. 【并查集】POJ 2524 Ubiquitous Religions

    POJ 2524 Ubiquitous Religions 就是看集合的数量是多少,将所有节点的祖先结点放进set,输出size就行了 #include <iostream> #inclu ...

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

    题目链接:http://poj.org/problem?id=2524 There are so many different religions in the world today that it ...

  4. poj 2524 Ubiquitous Religions (并查集)

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

  5. SSL-ZYC POJ 2524 Ubiquitous Religions

    题目大意: 你知道你的大学里有N个学生.每个人都信仰宗教,你向每个学生请教他们的宗教信仰是不可行的.避免这些问题的一种方法是问M对学生,问他们是否相信同一宗教.从这些数据中,你可能不知道每个人都相信什 ...

  6. POJ2524——宗教(Ubiquitous Religions)【图论,并查集】

    正题 题目链接: http://poj.org/problem?id=2524 大意 有n个学生,告诉你哪两个学生的宗教相等,求校园里有多少个宗教. 解题思路 并查集链接就好了 代码 #include ...

  7. POJ-2524 Ubiquitous Religions(无处不在的宗教)解题报告(并查集)

    目录 题目描述 思路分析 今天没有考试,那就再刷点题吧(难道就不怕c语言挂科吗?).因为昨天写了道并查集,所以今天再来一道,还是有所收获的. 题目描述 题目:https://vjudge.net/pr ...

  8. POJ 2524 宗教信仰 并查集 基础模板

    世界上有许多不同的宗教,现在有一个你感兴趣的问题:找出多少不同的宗教,在你的大学中的大学生信仰了多少种不同的宗教.你知道在你的大学有n个学生(0<n<= 50000).若直接问每一个学生的 ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  10. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

最新文章

  1. Excel 技术篇-跨页签统计某一区域下符合条件值的数量方法,COUNTIF函数、数量统计公式的用法实例演示
  2. SAP 主生产计划(MPS)
  3. Spring事务配置的五种方式 说明
  4. Atom飞行手册翻译: 4.3 作用域设置、作用域和作用域描述符
  5. 如何将composer设置为全局变量?
  6. Verilog 教程
  7. Fedora14 root用户登陆
  8. 龙芯 Linux usb,使用debirf制作龙芯2F的LiveUSB
  9. Oracle数据库之导入导出
  10. 谈谈OSI模型和它的数据传输
  11. python截屏保存本地_Python截图并保存的具体实例
  12. 从有道云笔记迁移到另一个有道云笔记账号
  13. 响应式Web设计(四):响应式Web设计的优化
  14. shell编程(二)
  15. 【1】GAN在医学图像上的生成,今如何?
  16. 【AI论文精粹】图形化解释工具;神经网络数据质量;LiDAR 语义分割;机器学习的应用
  17. bugku 告诉你个秘密(ISCCCTF) (636A56355279427363446C4A49454A7154534230526D6843 56445A31614342354E326C4B494)
  18. vue-cli3 接口api代理 308 Permanent Redirect
  19. 二进制安装Kubernetes 单集群
  20. 【web前端特效源码】使用 HTMLCSSJavaScript实现各种跳跃浮动慢跑翻转旋转坠落的魔幻文字动画效果~太上头了~/动画效果|前端开发|IT软件开发基础入门教程|网页制作|网站开发定制

热门文章

  1. 格式化移动硬盘或者U盘,并设置为NTFS格式
  2. apache 服务器状态查看
  3. 计算机左侧没有桌面菜单栏,教您电脑菜单栏不见了
  4. 洛杉矶儿童医院被列为美国西部第一,在全美儿童医院中的总体排名升至第五
  5. 变异系数(Coefficient of Variation,COV)和协方差(Covariance, Cov)
  6. 转载:王垠·程序员的心理疾病
  7. Unity 在安卓手机上实时调试
  8. ubuntu下matplotlib的字体设置(新罗马字体)
  9. 在java程序中实现发送邮件的功能
  10. 解决外接显示屏后CPU占用率过高问题