POJ 2524 :Ubiquitous Religions

简介:
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; }









本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5078519.html,如需转载请自行联系原作者


相关文章
|
存储 C语言
C 语言函数完全指南:创建、调用、参数传递、返回值解析
函数是一段代码块,只有在被调用时才会运行。 您可以将数据(称为参数)传递给函数。 函数用于执行某些操作,它们对于重用代码很重要:定义一次代码,并多次使用。
340 3
|
人工智能 分布式计算 数据处理
数据倾斜问题之数据倾斜的定义如何解决
数据倾斜问题之数据倾斜的定义如何解决
232 0
|
数据采集 数据可视化
国内77个城市建筑物轮廓(带高度)数据分享(附百度网盘)
国内77个城市建筑物轮廓(带高度)数据分享(附百度网盘)
1661 1
|
JavaScript 安全 Java
Vue.js 滑动拼图验证码实现笔记
关于验证码的使用场景还是非常多的,很多网站上的验证码可谓是五花八门,下面是我使用Vue.js实现滑动拼图验证码做的一个笔记。
Vue.js 滑动拼图验证码实现笔记
|
Java 关系型数据库 MySQL
如何快速实现邮箱注册(项目案例)
说起Web项目,学过Java的一定都做过很多,今天就介绍一个常用的功能——邮箱注册。 这个功能主要针对面向大众的一些在线系统,比如我们平时注册一些网站,都需要首先提供邮箱,然后系统自动发送邮件到注册邮箱,激活验证通过后才能使用。
如何快速实现邮箱注册(项目案例)
|
存储 分布式计算 监控
日志数据采集与大数据存储方案实践
互联网及企业客户业务系统有大量的埋点日志数据实时生成,这些日志数据往往需要长期保存并有离线计算或者实时计算的需求。本文为您介绍日志数据采集与大数据存储实践方案。
日志数据采集与大数据存储方案实践
|
JavaScript 前端开发
Axure实战15:使用Axure和JavaScript创建一个MusicPlayer音乐播放器
Axure实战15:使用Axure和JavaScript创建一个MusicPlayer音乐播放器
649 0
Axure实战15:使用Axure和JavaScript创建一个MusicPlayer音乐播放器
|
存储 数据可视化
基于 ggridges 绘制剩余使用寿命密度图
基于 ggridges 绘制剩余使用寿命密度图
183 0
|
机器学习/深度学习 算法 数据挖掘
零基础入门数据挖掘-心跳信号分类预测亚军赛事解决方案分享
大家好,我是展翅高飞hhh,一个正在努力转算法的Java程序员,今天给大家分享下天池零基础入门数据挖掘-心跳信号分类预测参赛的感受和我的解决方案,希望对大家学习有所帮助。
791 0
|
Linux 网络安全 Docker
1.docker 安装(centos7)
1.docker 安装(centos7)
1.docker 安装(centos7)