博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ubiquitous Religions-并查集(5)
阅读量:6703 次
发布时间:2019-06-25

本文共 2291 字,大约阅读时间需要 7 分钟。

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 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0

Sample Output

Case 1: 1Case 2: 7

Hint

Huge input, scanf is recommended.       
#include 
int x,y,pa[1000000];int cha(int k){ if(pa[k]!=k) { pa[k]=cha(pa[k]); } return pa[k];}bool bing(int x,int y){ int x2=cha(x); int y2=cha(y); if(x2==y2) return false; pa[y2]=x2; return true;}void init(int n){ for(int i=0; i<=n; i++) { pa[i]=i; }}int main(){ int n,m,k,a,b,cas=1,i; while(scanf("%d%d",&n,&m)&&(m+n)) {
int count=0; init(n); for(i=1; i<=m; i++) { scanf("%d%d",&a,&b); bing(a,b); } for(i=1;i<=n;i++) { if(pa[i]==i)//直接求根节点的个数就可以得知 count++; } printf("Case %d: %d\n",cas++,count); } return 0;}

 

转载于:https://www.cnblogs.com/tianmin123/p/4620829.html

你可能感兴趣的文章
android中RecycleView分页原生代码封装,无任何第三方代
查看>>
Emgu-WPF 激光雷达研究-移动物体跟踪
查看>>
如何删除一个标签,但是保留里面的内容?
查看>>
[Angular] 'providedIn' for service
查看>>
PHP函数register_shutdown_function的使用示例
查看>>
设计模式-行为型模式,python访问者模式
查看>>
GIT 常用命令
查看>>
开源网站流量统计系统Piwik源码分析——后台处理(二)
查看>>
new FormData() - FormData对象的作用及用法
查看>>
Out of memory: Kill process 内存不足
查看>>
linux 基础(一)
查看>>
ejb-jar.xml
查看>>
最新杭州地铁开通时间表
查看>>
关闭shift中英文切换 英文代码/中文注释随意切换着写。
查看>>
距离北京奥运还有359天,发布WPF版本的北京2008标志(下)
查看>>
命令模式
查看>>
如何自定义长连接策略
查看>>
平衡二叉树与自平衡二叉树(红黑树)的区别
查看>>
读取JPG图片的Exif属性(一) - Exif信息简介
查看>>
【译文】追求生产极简主义
查看>>