Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 26678   Accepted: 13127

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
训练上是分治法,但我的第一感觉是并查集,所以先写并查集,以后再补分治
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream> using namespace std;
const int Max=50010;
int pre[Max];
int find(int x)
{
int i=x,r=x,j;
while(pre[r]!=r)
{
r=pre[r];
}
while(pre[i]!=i)//压缩路径
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void Build(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
}
}
int main()
{
int n,m;
int x,y;
int w=1;
while(scanf("%d %d",&n,&m))
{
if(n==0&&m==0)
{
break;
}
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
while(m--)
{
scanf("%d %d",&x,&y);
Build(x,y);
}
int sum=0;
for(int i=1;i<=n;i++)
{
if(pre[i]==i)
{
sum++;
}
}
printf("Case %d: %d\n",w++,sum); }
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏的更多相关文章

  1. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  2. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  3. strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏

    (一) strace 命令    用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...

  4. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  5. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  6. refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏

    refresh的停车场 TimeLimit: 1000ms Memory limit: 65536K 题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是 ...

  7. JAVA 关于Icon,Image,ImageIcon的简单的对比参考 分类: Java Game 2014-08-14 17:08 210人阅读 评论(0) 收藏

    转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...

  8. c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏

    使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...

  9. 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏

    最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...

随机推荐

  1. 与PostgreSQL相关的工具

    Pentaho Data Integration(kettle):一个优秀的抽取.转换.加载(Extract Transform and Load,ETL)工具 Pentaho  Report Ser ...

  2. SWF类标准开头Tag

    [SWF(width="800", height="600", backgroundColor="#ffffff", frameRate=& ...

  3. html 圆角边框

    <input style="border-radius: 10px;" type="submit" value="确认"> bo ...

  4. 转:Python获取随机数(中文)

    下面介绍下random中常见的函数. 前提:需要导入random模块 >>>import random 1.random.random random.random() 用于生成一个0 ...

  5. PHP和JS实现多按钮提交表单

    JS: <html> <head> <script> function submitit1() //交由程序1处理 { document.myForm.action ...

  6. 树形dp Anniversary party(HDU1520)

    题意:给出一棵树,(上下级关系)每个节点都有一个权值,要求选出一些节点满足这些节点任意连个点都不是直接的上下级关系,可以得到的最大权值是多少? 分析:对于每个点有两个状态选或者不选,用状态数组dp[u ...

  7. Java基础(57):Eclipse中环境配置(视图字体颜色行号调试快捷键等等)

    1:Eclipse的基本配置 A:程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:I ...

  8. 关于MVC4中EFCoderFirst 数据迁移的三句经典指令

    首先输入这句指令     enable-migrations -contexttypename SchoolContext  ---------(SchoolContext为你设置的数据库名)它会自动 ...

  9. mac ruby rails安装(使用rvm)

    mac的场合: which ruby -> /usr/bin/ruby -> 这是mac自带的ruby,我们希望能用管理ruby的版本. 安装rvm curl -L https://get ...

  10. Power Gating的设计(模块)

    Switching Fabric的设计: 三种架构:P沟道的switch vdd(header switch),N沟道的switch vss(footer switch),两个switch. 但是如果 ...