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/ ...
随机推荐
- 为bootstrap添加更多自定义图标
From: http://blog.csdn.net/mengxiangfeiyang/article/details/45224731 Twitter Bootstrap 真是前端开发的瑞士军刀,作 ...
- [导读]Learning from Imbalanced Classes
原文:Learning from Imbalanced Classes 数据不平衡是一个非常经典的问题,数据挖掘.计算广告.NLP等工作经常遇到.该文总结了可能有效的方法,值得参考: Do nothi ...
- vps推荐之DigitalOcean
作为一个爱折腾的网站”程序猿“,我用过多家vps,由于一般支持paypal 月付, 所以基本上都会用两三个月,不行就换另一家. 1.Yard VPS 台湾人开的,有中文支持,貌似也支持支付宝付款,偶尔 ...
- linux 两个文件合并
可以使用cat命令,有两种实现的方式,一种将两个文件合并的到一个新的文件,另一种将一个文件追加到另一个文件的末尾. 方法一:使用cat命令从文件中读入两个文件,然后将重定向到一个新的文件.这种方法可以 ...
- Sql 常用时间转换
CONVERT(varchar(100), GETDATE(), 0); -- 08 31 2015 04:57PM CONVERT(varchar(100), GETDATE(), 20); --2 ...
- asp.net文件下载文件另存为
这是一个困惑已久的问题…… 首先,用<a>标签的href打开浏览器能解读的文件(如txt,jpg,pdf等),会自动打开,无法做到弹出另存为的效果. 其次,网上搜索了各种JS解决办法,包括 ...
- HDOJ 题目2474 String painter(区间DP)
String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【转】输入/输出流 - 全面掌握IO
File类: 程序中操作文件和目录都可以使用File类来完成即不管是文件还是目录都是使用File类来操作的,File能新建,删除,重命名文件和目录,但File不能访问文件内容本身,如果需要访问文件本身 ...
- 使用java发送邮件
首先要加入mail.jar包 import java.io.UnsupportedEncodingException; import java.util.Properties; import java ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...