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/ ...
随机推荐
- Android的权限检查
Application的权限: 可以在AndroidManifest.xml中用<permission>定义运行Application需要的权限. 用<uses-permission ...
- TRACERT命令
这半个月 服务器从联通线路换到移动线路 导致基层用联通和电信线路的用户 上不去收费软件 tracert 120.194.42.142:8090 发现路由器 解析地址绕过很多条街后 出现丢包现象 联系 ...
- MEAN.JS入门
MEAN stands for: 下载:MongoDB下载文件 百度盘共享 运行命令:mongod 提示你dbpath(/data/db/) does not exist, terminating 创 ...
- JS 跑马灯
利用jquery 来实现图片切换.文字转换移动的工具. MSClass 连接 http://www.popub.net/script/MSClass.html Mark 用
- 开启GZIP(转)
因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理.我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下 ...
- VMware-workstation-full-12.0.0-2985596&VMware-player-12.0.0-2985596
VMware-workstation-full-12.0.0-2985596 永久KEY:YV3D2-A0D50-481XP-KPNGE-ZV2ZF VMware-player-12.0.0-2985 ...
- Dapper学习笔记(3)-增、删、改、查
一.建表 在数据库中建立如下三张表: CREATE TABLE [dbo].[T_User] ( , ) PRIMARY KEY NOT NULL, ) NOT NULL, ) NULL, ) NUL ...
- 关于<form>标签
<form>用于为用户输入创建HTML表单,表单用于向服务器传输数据 form是块级元素,其前后会产生折行 <form>包含: 1.input元素:(根据不同的type属性,输 ...
- ios上架报错90080,90087,90209,90125 解决办法
ERROR ITMS-90087: "Unsupported Architectures. The executable for yht.temp_caseinsensitive_renam ...
- ThinkPHP学习遇到的点问题(学习中,持续更新)
1.__ROOT__变量没有值的问题 今天遇到这个比较变态的问题,可能是我技术比较菜吧,搞了一个多小时,最后发现问题可能出现在 入口文件中定义的APP_NAME的名字和项目的跟目录名称相同了,比如我网 ...