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. 最少clock

    var elClock = document.getElementById("clock");var getTime = function(){ var _ = ['00','01 ...

  2. Mallet 使用说明

    Mallet:自然语言处理工具包 发表于128 天前 ⁄ 技术, 科研 ⁄ 评论数 6 ⁄ 被围观 1006 Views+ MALLET是基于java的自然语言处理工具箱,包括分档得分类.句类.主题模 ...

  3. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  4. JS实现登陆验证的主要代码及思路

    window.onload = function(){ // 获取input标签 var alInput = document.getElementsByTagName("input&quo ...

  5. js实现当前导航菜单高亮显示

    为了增加用户体验度,增加网页的易用性和美观度,往往需要把当前导航菜单以特殊方式显示,通常是高亮显示或有不同于其它菜单的背景,有两种方法可以实现,第一种是用纯css来实现,二是用js辅助css来实现,两 ...

  6. git基本技巧及进阶

    基本技巧 1. 安装后的第一步 在安装好git后,你第一件该做的事是设置你的名字和电子邮箱,因为每次提交都要用到这些信息: $ git config --global user.name " ...

  7. 配置指定使用tcc编译器编译nim程序

    1.前言 nim是什么? nim是一门静态编译型语言,语法类似python,nim的代码被翻译成C代码再被C编译器编译成可执行文件.因此nim的可执行文件比较小,性能应该也不错. 最简单的nim程序就 ...

  8. iOS $299申请时碰到的狗血问题

    最近项目需要申请in-house证书,结果在提交审核前一步,遇到问题:Enter your organization information.please enter the Romanized ve ...

  9. css 笔记

    外边距合并 当一个元素出现在另一个元素的上面时,第一个元素的下外边距和第二个元素的上外边距会产生合并,两个盒子之间的上下间距为大的数值. 当一个子元素包含在另外一个父元素(假设没有内边距 没有边框), ...

  10. centos7下安装mantis

    1.环境配置 Web Server:Apache,The web server must support PHP. 数据库:MySQL (or one of its forks, e.g. Maria ...