Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K

Total Submissions: 30666

  Accepted: 14860

Description

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.

Input

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.

Output

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.
 

Sample Input

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

Sample Output

Case 1: 1
Case 2: 7

Hint

Huge input, scanf is recommended.
 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
int f[]; int find(int x) {
if (x != f[x])
f[x] = find(f[x]);
return f[x];
} void Union(int a, int b) {
int f1 = find(a);
int f2 = find(b);
if (f1 != f2)
f[f2] = f1;
} int main() {
//ifstream cin("aaa.txt");
int n, m, test = , sum; while(scanf("%d%d", &n, &m)){
if (n == && m == )
break;
memset(f, , sizeof(f)); for(int i = ; i <= n; i++) {
f[i] = i;
} sum = n; for(int i = ; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
if(find(a) != find(b)){
Union(a, b);
sum--;
}
} printf("Case %d: %d\n", test++, sum); }
//system("pause");
return ;
}
 #include <stdio.h>
#include <iostream>
using namespace std; const int MAXN = ; /*结点数目上线*/
int pa[MAXN]; /*p[x]表示x的父节点*/
int rank1[MAXN]; /*rank[x]是x的高度的一个上界*/
int n, ans; void make_set(int x)
{/*创建一个单元集*/
pa[x] = x;
rank1[x] = ;
} int find_set(int x)
{/*带路径压缩的查找*/
if(x != pa[x])
pa[x] = find_set(pa[x]);
return pa[x];
} /*按秩合并x,y所在的集合*/
void union_set(int x, int y)
{
x = find_set(x);
y = find_set(y);
if(x == y)return ;
ans--; //统计
if(rank1[x] > rank1[y])/*让rank比较高的作为父结点*/
{
pa[y] = x;
}
else
{
pa[x] = y;
if(rank1[x] == rank1[y])
rank1[y]++;
}
}
//answer to 2524
int main()
{
int m, i, j = , x, y;
while(scanf("%d%d", &n, &m))
{
if(n == m && m == ) break;
for(i = ; i <= n; i++)
make_set(i);
ans = n;
for(i = ; i < m; i++)
{
scanf("%d%d", &x, &y);
union_set(x, y);
}
printf("Case %d: %d\n", j, ans);
j++;
}
return ;
}

poj 2524 Ubiquitous Religions(宗教信仰)的更多相关文章

  1. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  2. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  3. POJ 2524 Ubiquitous Religions

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 20668   Accepted:  ...

  4. POJ 2524 Ubiquitous Religions 解题报告

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34122   Accepted:  ...

  5. [ACM] POJ 2524 Ubiquitous Religions (并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted:  ...

  6. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  7. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

  8. poj 2524 Ubiquitous Religions(并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted:  ...

  9. POJ 2524 Ubiquitous Religions (幷查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted:  ...

随机推荐

  1. Serializable 序列化为文件

    package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  2. Bootstrap迁移系列 - Navbar

    在V2.3.2版本中一个标准的导航栏模版如下: <div class="navbar"> <div class="navbar-inner"& ...

  3. EasyUI 下拉列表联动

    //绑定部门.人员下拉菜单项 function BindDdl() { var $ddlbm = $("#ddlBm");//部门下拉列表 var $ddlry = $(" ...

  4. 神经网络环境搭建,windows上安装theano和keras的流程

    今天碰到有朋友问道怎么在windows下安装keras,正好我刚完成搭建,总结下过程,也算是一个教程吧,给有需要的朋友. 步骤一:安装python. 这一步没啥好说的,下载相应的python安装即可, ...

  5. CentOS6.4 64位系统安装jdk

    1. CentOS操作安装好了以后,系统自带了openJDK,先查看相关的安装信息: $rpm -qa | grep java tzdata-java-2013b-1.el6.noarchjava-1 ...

  6. php and web service with wsdl

    Following are the links: Developing Web Services Using PHP PHP Web Services with WSDL Creating Web S ...

  7. usr/bin/ld: cannot find 错误解决方法

    参考:http://blog.siyebocai.cn/20100324_5p424qs7.html 通常在软件编译时出现的usr/bin/ld: cannot find -lxxx的错误,主要的原因 ...

  8. Unix: How to Install BerkeleyDB From Source

    http://www.masaokitamura.com/2010/07/23/unix-how-to-install-berkeleydb-from-source/ This documentati ...

  9. POI Excel导出样式设置

    HSSFSheet sheet = workbook.createSheet("sheetName");    //创建sheet sheet.setVerticallyCente ...

  10. JTextField限制 输入数字

    貌似有很多方法,先记了再说... 1.限制输入数字 用法 textfield.setDocument(new IntegerDocument()); class IntegerDocument ext ...