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. BRVAH(让RecyclerView变得更高效) (2)

    本文来自网易云社区 作者:吴思博 1.2 宫格和列表的混排样式 关于 Grid 和List 的混排样式,Grid 样式是一行有多个,而 List样式是一行只有一个. 我们可以把 List 样式看成是G ...

  2. 基于注解实现SpringBoot多数据源配置

    1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...

  3. 【U+B+D】三层框架 原理+实例

    导读:三层的学习,也终于得到收获了.这个过程很艰辛,不止一次的想放弃.在这一个学习过程中,真的很感谢师傅的尽心.耐心.费心.其实真的很脆弱,现在回想起来都很不可思议. 一.基本概况 1,什么是三层 我 ...

  4. Oracle 数据库有五个必需的后台进程,DBWR,LGWR,CKPT,SMON,PMON

    SMON 是系统监视器(System Monitor)的缩写.如果Oracle实例失败,则在SGA中的任何没有写到磁盘中的数据都会丢失.有许多情况可能引起Oracle实例失败,例如,操作系统的崩溃就会 ...

  5. Dialog共通写法(一个button)

    一个button的: package jp.co.hyakujushibank.view import android.app.Dialogimport android.content.Context ...

  6. [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)

    传送门 DP f[i][j]表示前i个中选j个的最优解 预处理g[i][j]表示选i~j对答案的贡献 那么就可以n^3乱搞了! 注意边界 #include <cstdio> #includ ...

  7. 【CCF】通信网络 简单搜索

    去重!不然有环就直接挂掉了...0分 #include<iostream> #include<cstdio> #include<string> #include&l ...

  8. Spoj-DWARFLOG Manipulate Dwarfs

    Manipulate Dwarfs In a small village beyond seven hills and seven seas, Snow White lives together wi ...

  9. Laravel 之文件上传

    配置文件: config/filesystems.php, 新建存储空间 'uplaods' => [ 'driver' => 'local', 'root' => storage_ ...

  10. webstorm调试(一)提示css未使用的选择器Selector is never used

    一.css未使用的选择器Selector 今天写vue的时候,给动态绑定了一个class属性,然后样式里面就给了warning,看起来怪怪的,很不舒服