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

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
题目大意:输入n,m代表有n个帮派,下面输入m行x,y,代表学生x,y同一个帮派,问总共有多少个帮派。
解题方法:并查集,每次输入x,y时如果发现他们不同帮派,则n减一,然后合并。
#include <stdio.h>

typedef struct
{
int parent;
int rank;
}UFSTree; UFSTree Student[]; void MakeSet(int n)
{
for (int i = ; i <= n; i++)
{
Student[i].parent = i;
Student[i].rank = ;
}
} int FindSet(int x)
{
if (x != Student[x].parent)
{
return FindSet(Student[x].parent);
}
else
{
return x;
}
} void UnionSet(int x, int y)
{
x = FindSet(x);
y = FindSet(y);
if (Student[x].rank > Student[y].rank)
{
Student[y].parent = x;
}
else
{
Student[x].parent = y;
if (Student[x].rank == Student[y].rank)
{
Student[y].rank++;
}
}
} int main()
{
int m, n, x, y, nCase = ;
while(scanf("%d%d", &n, &m) != EOF && n != && m != )
{
MakeSet(n);
for (int i = ; i < m; i++)
{
scanf("%d%d", &x, &y);
if (FindSet(x) != FindSet(y))
{
n--;
UnionSet(x, y);
}
}
printf("Case %d: %d\n", ++nCase, n);
}
return ;
}

POJ 2524 Ubiquitous Religions的更多相关文章

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

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

  2. poj 2524 Ubiquitous Religions(宗教信仰)

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

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

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   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(并查集)

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

  8. POJ 2524 Ubiquitous Religions (幷查集)

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

  9. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

随机推荐

  1. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  2. Net中JSON序列化和反序列化处理(日期时间特殊处理)

    0  缘由 笔者最近在web api端使用Json.Net进行序列化处理,而在调用端使用DataContractSerializer进行反序列化,遇到日期时间处理反序列化不成功[备注:笔者使用Net ...

  3. C# 通过SerialPort简单调用串口

    问题 最近比较经常使用串口进行发送以及传输数据,但是笔者在刚开始接触SerialPort类时,对于Write之后去Read数据的时候,由于设备上面还没有返回数据,读取到的只能是空值.然而,再进行下一次 ...

  4. iOS YSKit系列

    YSKit是基于object-c的一套常用功能点的总结,都是我们在平时开发的过程中遇到的.这个框架将会慢慢地完善,由于本人精力有限,有时可能更新得相对慢一点.如果发现代码里有错误,也欢迎提出. 1.常 ...

  5. mysql 5.6.17 x64 安装

    下载地址 百度网盘地址:http://pan.baidu.com/share/link?shareid=1895747668&uk=3257050114&fid=234538452 官 ...

  6. 菜鸟学习WCF笔记-Address

    终结点是WCF最为核心的对象,因为它承载了所有通信功能.服务通过相应的终结点发布出来,客户端通过与之匹配的终结点对服务进行调用.终结点有地址.绑定和契约ABC三要素构成. Address  作为终结点 ...

  7. 如何在servlet取得spring beans (autowired)(转)

    在应用中一般普通的JavaPojo都是由Spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是 Filter,一个是Servlet,这两样东西都是由S ...

  8. linux平台下防火墙iptables原理(转)

    原文地址:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646466.html iptables简介 netfilter/iptables( ...

  9. reader

    http://git.oschina.net/jayqqaa12/abase-reader https://github.com/JustWayward/BookReader https://gith ...

  10. django关闭debug后,静态文件的处理

    Django框架仅在开发模式下提供静态文件服务.当我开启DEBUG模式时,Django内置的服务器是提供静态文件的服务的,所以css等文件访问都没有问题,但是关闭DEBUG模式后,Django便不提供 ...