3136: Ubiquitous Religions

时间限制(普通/Java):2000MS/6000MS     内存限制:65536KByte
总提交: 274            测试通过:149

描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

Case 1: 1
Case 2: 7

提示

Huge input, scanf is recommended.
题解:
题目意思是两两结对,A认识B,B认识C,ABC就为同一群人,问最后有多少群人。
简单并查集。直接贴代码。

#include<stdio.h>
int fid[50010];
int find(int x)
{
if(x==fid[x])
{
return x;
}
else
{
return find(fid[x]);
}
}
void hebing(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
fid[a]=b;
}
}
int main()
{
int n,m,i=0,j,a,b,s;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
s=0;
i++;
for(j=0;j<n;j++)
{
fid[j]=j;
}
for(j=0;j<m;j++)
{
scanf("%d %d",&a,&b);
hebing(a,b);
}
for(j=0;j<=n;j++)
{
if(fid[j]==j)s++;
}
printf("Case %d: %d\n",i,s);
}
}

TOJ3136的更多相关文章

  1. TOJ2647

                                                             2647: How Many Tables 时间限制(普通/Java):1000MS/ ...

随机推荐

  1. 博客迁移到GitCafe

    博客以前是放在github上,但github在国内的访问速度确实有些慢,所以就想着换个git环境,本来想迁移到oschina中,后来看到以为博友介绍的迁移到gitcafe中,索性我也就照搬迁过来了. ...

  2. IntelliJ IDEA 14.x 与 Tomcat 集成,创建并运行Java Web项目

    转自:http://www.php-note.com/article/detail/854 IntelliJ IDEA 14.x 与 Tomcat 集成,创建并运行Java Web项目 作者:php- ...

  3. 观点:BPM已经过时了?

    在这个信息爆炸的世界,业务转型通常是以数字系统和流程的方式发生. 根据Forrester的报告“软件必须丰富你的品牌”,人们要依靠这些技术实现期望的商业成果,比如品牌联系.客户服务创新.更好的产品和提 ...

  4. 【kate整理】matlab求商,求余数

    a/b=q...r   a=b*q+r  r为余数 fix(a/b)    求商rem(a,b)  求余数还可以 mod(a,b) 两者的区别是余数的符号,rem与a相同,而mod与b相同 例1: & ...

  5. Win Form程序线程点点

    消息循环 Win32窗体程序基于消息驱动的,程序的模型就是一个用户触发事件消息->系统分发事件消息->程序处理事件的循环过程. .NET Win Form程序对消息循环进行了封装,可以看到 ...

  6. C# lambda表达式(简单易懂)

    前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能之一 ...

  7. linux自动更新代码,打包发布

    1.安装svn yum install subversion 2.安装 maven 下载:百度云盘地址为 http://pan.baidu.com/s/1nuKQGjv 解压 tar -zxvf ap ...

  8. GoldenGate Studio 12.2.1.1发布

    OGG studio是一款图形化OGG配置部署产品,其主要特性:1. 逻辑层面设计OGG,不需要了解OGG细节:2. 最值实践加快常用场景的配置:3. 使用拖拉映射,自动匹配源和目标对象:4. 一键部 ...

  9. C# winform 安装程序打包(自定义操作)

    (一),安装程序 以前用vs制作过安装程序,现在把步骤写出来,有帮助的大家一定要顶哦 第一步:建立工程1.打开vs,新建项目->其他项目类型->安装和部署(這個子项下面有安装项目和Web安 ...

  10. sis9280触摸ic 基于rk3288 的安卓4.4的 多点触摸

    前言:sis提供的驱动ic.基于rk3288的安卓系统.亲眼看到人家完成一次移植.很激动的记下一些东西..虽然我看不懂.其实现在的工作也不需要看懂.叫人协助就好,只需要知道有这个东西. 1linux下 ...