TOJ3136
3136: Ubiquitous Religions
总提交: 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
提示
#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的更多相关文章
- TOJ2647
2647: How Many Tables 时间限制(普通/Java):1000MS/ ...
随机推荐
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- 在QtCreator 2.1.0 下使用opencv231库加载图片并显示
在.pro中库连接如上图,具体规则正在学习,注意debug下连接*d.lib.release下链接.lib.没有d的. 如果出现imread不可以加载图片,cvloadImage却可以,则是上面说的连 ...
- 前端开发week1
1.前端开发前期相关工具 photoshop:主要学习运用与前端相关工具,了解UI与前端的关联. axure:原型设计,通过设计原型了解网页基本结构,结合ps对UI有更好的理解. webs ...
- 将halcon导出的c++程序打包成dll库
1.从“文件”菜单中,选择“新建”,然后选择“项目…”. 2.从“项目类型”窗格中选择“Visual C++”下的“Win32”. 3.从“模板”窗格中,选择“Win32 控制台应用程序”. 4.为 ...
- Java对象的XML序列化(转)
转自:http://westlifesz.javaeye.com/blog/48618 java.io.Serializable引发的问题——什么是序列化?在什么情况下将类序列化? 序列化就是一种用 ...
- stella mccartney falabella foldover tote a few eye observed
Lately, the particular Heyuan City Courtroom retrial, in order to commit the criminal offense of cou ...
- c++中变量声明和变量定义的区别。2016年12月6日
整个流程: 1.程序告诉cpu,程序将要使用一个变量.(暂时不一定用到,先说一下.) 2.程序告诉CPU,程序现在就要使用一个变量.(现在就用) 3.cpu按照这个变量的类型,把内存划分出几个单位(b ...
- c语言实现牛顿迭代法
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<float.h> #inc ...
- NC台网震相走时获取及 HYPOINVERSE 格式读取
HYPOINVERSE格式介绍:http://www.ncedc.org/ftp/pub/doc/man5/ncsn.phase.5 获取网站: http://www.ncedc.org/ncedc/ ...
- C#和Java在重写上的区别
C# class A { public string Get1() { return "A1"; } public virtual string Get2() { return & ...