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. [OpenCV] 2、边缘检测 canny

    >_<" 边缘检测代码:

  2. [WinAPI] API 11 [创建目录]

    编程实现创建目录是非常简单的,只要使用API函数CreateDirectory即可. (1) Createdirectory ◇参数lpPathName:输入参数,所要创建的目录名或路径.lpSecu ...

  3. [游戏模版7] Win32 最简单贴图

    >_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...

  4. [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.

    I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...

  5. 更新日志 - 关于 iOS9 设备的安装及其他优化

    新版 fir.im 上线整 3 周了,感谢你们对 fir.im 的关注和支持!无以言表,唯有做更好用的产品给大家.本周我们对新版做了以下的功能更新和 bug 修复: 功能更新 在使用过程中,请注意: ...

  6. iOS开发——高级技术&调用地图功能的实现

    调用地图功能的实现 一:苹果自带地图 学习如逆水行舟,不进则退.古人告诉我们要不断的反思和总结,日思则日精,月思则月精,年思则年精.只有不断的尝试和总结,才能让我们的工作和生活更加 轻松愉快和美好.连 ...

  7. Android 关于“NetworkOnMainThreadException”

    网络收集的原因如下,以及解决办法: 我补充总结一下: 解决办法一:在操作网络类(socket连接)的activity的protected void onCreate(Bundle savedInsta ...

  8. Java日期与时间的处理/Date,String,Calendar转换

    public class Demo01 { //Java中Date类和Calendar简介 public static void main(String[] args) { long now=Syst ...

  9. 解决 Android Studio 乱码问题

    http://www.eoeandroid.com/thread-275485-1-1.html 很多同学都安装了Android Studio,但是发现中文是乱码,其实这个很好解决的.在IDE里点击F ...

  10. 用DOS批处理实现FTP自动上传、下载、清理文件

    用DOS批处理实现FTP自动上传.下载.清理文件 最近好像特别的忙,好久没来写点东西了,今天写了一个利用批处理程序完成FTP自动上传.下载.清理文件的程序.赶紧 记录下来,以备日后之用.功能介绍:自动 ...