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/ ...
随机推荐
- swift 自定义图片轮播视图
Swift封装图片轮播视图: import UIKit class XHAdLoopView: UIView { private var pageControl : UIPageControl? pr ...
- 【转】查看java类是从哪个包加载
Java的类装载器使用的是优先策略,加载类的时候先找到哪个就加载哪个.有时候我们做一个系统,当类库非常庞大的时候,类可能会出现冲突,也就是类路径中存在不同版本的两个相同的类,这往往给调试带来非常大的麻 ...
- Java_DOM创建XML
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...
- 涵涵和爸爸习惯养成进度表(三)(June 25 - )
规则说明 23天内,没有哭脸,不超过三个无表情脸,可以给一个奖励(动画书等) 涵涵违反规则,在爸爸和妈妈都同意的情况下,可以给无表情脸 爸爸违反规则,在妈妈和涵涵都同意的情况下,可以给无表情脸 获奖记 ...
- java中开源日志记录工具log4j
日志:除了能记录异常信息,还可以记录程序正常运行时的关键信息. 使用log4j来进行日志文件记录经典步骤: 001.在项目中创建一个lib文件夹,然后将下载好的jar包copy到该文件夹下 002.对 ...
- 关于解析P D X P 协议的心得
1一个线程进队Quee 一个线程出队 也应该lock,不然会出错. 2 委托的效率较低 能不用委托的地方,尽量不要用委托. 在一个线程中需要调用控件时采用委托. 3 for循环中异步发送数据不能保证发 ...
- MFC中使用FLASH
一.准备工作 第一步:下载并安装Adobe Flash Player. 从官方网站(http://get.adobe.com/cn/flashplayer/)上下载最新的Flash Player(大约 ...
- Linux下使用NMON监控、分析系统性能
一.下载nmon. 根据CPU的类型选择下载相应的版本:http://nmon.sourceforge.net/pmwiki.php?n=Site.Downloadwget http://source ...
- Unity3D中可中途释放的单例
Unity3D中可中途释放的单例 使用静态类,静态变量的坏处是从程序加载后就一直占用内存,想要释放比较麻烦,可是之前使用的单例,没有提供释放的方法,那是不是也同静态的一样直到程序结束菜释放?那单例的好 ...
- css position static | absolute | fixed | relative
<div id="bigbox1"> <div id="box1" class="box">box1</ ...