Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 23171
Accepted: 11406

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

额。。难道题单错了?。。

。我一看就是并查集。

。所以就这么水过了。。

并查集一A水过

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath> using namespace std; #define M 100500
int p[M];
int n, m; void start()
{
for(int i=1; i<=n; i++)
p[i] = i;
} int find(int x)
{
return p[x] == x ? x : p[x] = find( p[x] );
} void Kruskal(int x, int y)
{
int xx = find(x);
int yy = find(y);
if(xx!=yy)
p[yy] = xx;
} int main()
{
int cas = 0;
while(scanf("%d%d", &n, &m) &&n &&m)
{
cas++;
start();
int ans = 0;
for(int i=1; i<=m; i++)
{
int x; int y;
scanf("%d%d", &x, &y);
Kruskal(x, y);
}
for(int i=1; i<=n; i++)
{
if( p[i]==i )
ans++;
}
printf("Case %d: %d\n", cas, ans);
} return 0;
}

POJ 2524 :Ubiquitous Religions的更多相关文章

  1. 【48.47%】【POJ 2524】Ubiquitous Religions

    Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32364 Accepted: 15685 Description There a ...

  2. POJ2524:Ubiquitous Religions (并查集模板)

    Description There are so many different religions in the world today that it is difficult to keep tr ...

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

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

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

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

  5. POJ 2524 Ubiquitous Religions

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

  6. Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏

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

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

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

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

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

  9. poj 2524 Ubiquitous Religions 一简单并查集

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

随机推荐

  1. LintCode 二叉树的层次遍历 II

    中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...

  2. FreeLink开源呼叫中心设计思想

    上一篇大概说了国内外优秀的呼叫中心系统: 国内外优秀呼叫中心系统简单介绍 借鉴上述呼叫中心系统,我们的设计新一代呼叫中心例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  3. Swift - 给表格TableView添加页眉和页脚

    UITableView具有var tableHeaderView:UIView?属性和var tableFooterView:UIView?属性,可以通过给其赋值来创建列表TableView的页眉和页 ...

  4. Android SDK Manager安装报错

    安装出错了,上网百度一大堆说法,又说版本兼容问题的,有说http换https的,我这个菜鸟真搞不明白,自己捣腾一下午总算弄上去了! 问题: 安装完Eclipse和ADT却无法更新SDK. 启动Andr ...

  5. Linux下套接字具体解释(三)----几种套接字I/O模型

    參考: 网络编程–IO模型演示样例 几种server端IO模型的简介及实现 背景知识 堵塞和非堵塞 对于一个套接字的 I/O通信,它会涉及到两个系统对象.一个是调用这个IO的进程或者线程,还有一个就是 ...

  6. 《C++语言基础》实践參考——数组作数据成员

    返回:贺老师课程教学链接 [项目5 - 数组作数据成员]阅读教材P255例8.4.注意到类中的数据成员能够是数组.设计一个工资类(Salary),当中类的数据成员例如以下: class Salary ...

  7. 插件 - 提示窗体(ArtDialog)

    效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...

  8. qt新进程工作目录的设置(工作目录确实是被子进程继承的,但也可以设置)

    经过试验,qt启动一个新的进程时,这个进程的工作目录是继承父进程的,无论是通过start还是startDetached来启动. 其实对于linux系统,qt底层应该也是调用fork.exec之类的函数 ...

  9. Hibernate获取Connection

    package com.trendcom.base.util; import java.sql.Connection; import java.sql.SQLException; import jav ...

  10. iphone之NSArray比较大小

    //排序 NSArray *tempArray = [[NSArray alloc]initWithArray:self.contentArray]; tempArray = [tempArray s ...