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. jquery easyui的方法参数

    1.form 表单的参数有:submit,validate,clear,load 2.submit 例如: $('#goods-add').form('submit', { url : url, on ...

  2. TQJson序列和还原clientdataset.data

    TQJson是QDAC开源项目的JSON对象,其特点是跨平台,功能强大. 序列 procedure TForm2.Button2Click(Sender: TObject);var AJson: TQ ...

  3. 在VB中利用Nuget包使用SQLite数据库和Linq to SQLite

    上午解决了在C#中利用Nuget包使用SQLite数据库和Linq to SQLite,但是最后生成的是C#的cs类文件,对于我这熟悉VB而对C#白痴的来说怎么能行呢? 于是下午接着研究,既然生成的是 ...

  4. 我所理解的设计模式(C++实现)——备忘录模式(Memento Pattern)

    概述: 我们玩单机游戏的时候总会遇到老婆大人的各位事情,一会去买瓶醋了,一会去打个酱油了,会耽误我们玩游戏的进程,但是此时我们能有“保存游戏”这个宝贝,我们的主基地不会在我们打酱油的时候被对手拆掉. ...

  5. UML图示与代码对照

    本文转载:http://www.cnblogs.com/iamlilinfeng/archive/2012/08/29/2662740.html 一.类继承 1 public class Father ...

  6. 理解Windows中的路由表和默认网关

    每一个Windows系统中都具有IP路由表,它存储了本地计算机可以到达的网络目的地址范围和如何到达的路由信息.路由表是TCP/IP通信的基础,本地计算机上的任何TCP/IP通信都受到路由表的控制. 理 ...

  7. 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧

    Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...

  8. TC SRM 664 div2 A BearCheats 暴力

     BearCheats Problem Statement    Limak is an old brown bear. Because of his bad eyesight he sometime ...

  9. Codeforces Gym 100500F Problem F. Door Lock 二分

    Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...

  10. c# windowsForm打印

    在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .net Framework的打 印功能都以组件的方式提供,为程序员提供了很大的方便,但是 ...