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

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.

Source



#include <iostream>

#include <cstdio>

using namespace std;

int f[50005],sum;
//查找x的父节点,假设f[x]==x。他的父节点就是本身。

int find (int x)

{

if(f[x]!=x)

    f[x]=find(f[x]);

    return f[x];

}       
//假设不是一个集合(父节点不同样),合并集合(将父节点改成b的父节点)。        

void make(int a,int b)

{

    int f1=find(a);

    int f2=find(b);

    if(f1!=f2)

    {

        f[f2]=f1;

        sum--;                  //宗教数减一。

    }

}

int main()

{

  int n,m,p=1,i;

  while(scanf("%d%d",&n,&m)!=EOF)

  {

      if(n==0&&m==0) break;

      for(i=1;i<=n;i++)

        f[i]=i;

      sum=n;

      for(i=1;i<=m;i++)

      {

          int a,b;

          scanf("%d%d",&a,&b);

          make(a,b);

      }

      printf("Case %d: %d\n",p++,sum);





  }

 return 0;

}

POJ 2524 Ubiquitous Religions (幷查集)的更多相关文章

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

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

  2. poj 2524 Ubiquitous Religions (并查集)

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

  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: 22389   Accepted ...

  5. poj 2524 Ubiquitous Religions(并查集)

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

  6. POJ 2524 Ubiquitous Religions (并查集)

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

  7. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

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

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

  9. POJ 2524 Ubiquitous Religions

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

随机推荐

  1. 10,*args **kwargs 函数的命名空间。

    用户传入到函数中的实参数量不定时,或者是为了以后拓展, 此时要用到动态参数*args,**kwargs(万能参数.) *args(接受的是所有的位置参数) 以元组的形式返回给函数调用者. **kwar ...

  2. Mac 文档阅读软件Dash软件破解版

    1.Dash 破解版链接 Mac 上阅读开发文档的软件:支持java.spring.springBoot等.百度网盘下载链接和密码如下. 链接:https://pan.baidu.com/s/1RWM ...

  3. Mac设置命令别名

    文件中添加 /Users/xxx/.bash_profile 添加别名命令: alias pull='git pull origin' 注意:等号后面不能有空格 从新读取配置文件: source ~/ ...

  4. Clickomania(区间DP)

    描述 Clickomania is a puzzle in which one starts with a rectangular grid of cells of different colours ...

  5. 【NOIP2014】伤感·伤感·伤感

    Day <0 虽说初三的时候考过一次提高组,而且还考得不错,但自己还是挺看重这次NOIP的[你想想旁边两大神级别人物在死命刷题,蒟蒻怎敢颓废]于是切完所有复赛题后又做了好多好多次模拟赛,状态自己 ...

  6. NIO Channel的学习笔记总结

    摘自:http://blog.csdn.net/tsyj810883979/article/details/6876603 1.1  非阻塞模式 Java  NIO非堵塞应用通常适用用在I/O读写等方 ...

  7. 史上最详细的linux关于connect: network is unreachable 问题的解决方案

    1.虚拟机常用连接网络方式有两种:桥接和NAT. 使用桥接模式:则保证虚拟机的网段与物理机的网段保持一致.如下: 虚拟机网卡配置: 物理机使用WiFi接入网络(我用的是WiFi,你们可能用的是有线道理 ...

  8. 【BZOJ3991】寻宝游戏(虚树,DFS序,splay)

    题意:求在树中从任意点开始,经过若干个关键点回到原点的最小距离 要求支持在线将某个点设置(取消)为关键点,以及询问答案 n,m<=100000 len[i]<=10^9 思路:显然是一个虚 ...

  9. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

  10. Spring Task Schedule 及多线程

    http://spring.io/blog/2010/01/05/task-scheduling-simplifications-in-spring-3-0/‘ http://ekramalikazi ...