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

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>
#include<cstring>
using namespace std;
int father[50005];
int find(int x)//找与x相关联的人
{
while(x!=father[x])//找到人
x=father[x];
return x;
}
void getfind(int a,int b)
{
int roota=find(a);
int rootb=find(b);
if(roota!=rootb)当roota和rootb不在同一集合时将其合并
father[rootb]=roota;
}
int main()
{
int n,m,i,j,a,b;
int t=1;
while(cin>>n>>m,n&&m)
{ for(i=1;i<=n;i++)
father[i]=i;
for(i=0;i<m;i++)
{
cin>>a>>b;
getfind(a,b);
} int ans=0;
for(i=1;i<=n;i++)
//cout<<father[i]<<" "<<endl;
if(father[i]==i)
ans++;
cout<<"Case "<<t<<": "<<ans<<endl;
t++;
} }

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 (幷查集)

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

  3. poj 2524 Ubiquitous Religions (并查集)

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

  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: 22389   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. mysql truncate drop delete的区别

    以下讨论,针对于mysql数据库. 为什么会想到这个问题呢? 因为项目中需要清除数据库的数据,而且需要实现自增的主键从0开始计数.所以想到总结一个几个常用的删除语法的差异. 可以做一个测试 建一个带有 ...

  2. [置顶] docker--基础镜像和dockerfile

    制作基础镜像 注意:需要在CentOS6下操作 准备工作 yum -y install febootstrap 下载ISO镜像文件到服务器 mkdir /mnt/centos6/ mount -o l ...

  3. selenium控制浏览器为手机模式

    # -*- coding: utf-8 -*- from selenium import webdriver from time import sleep mobileEmulation = {'de ...

  4. JAVA之ArrayList集合

    /** * ArrayList集合讲解 * 集合的添加.遍历.删除 */package com.test; import java.util.*; public class test7 { /**   ...

  5. Ubuntu系统日志

    一.指定系统日志路径到:  /var/log/messages 1.默认系统日志 root@HDController:~# tail -1000 /var/log/syslog Oct 20 06:2 ...

  6. 【Consul】 分布式环境中的服务注册和发现利器

    参考资料: http://www.cnblogs.com/shanyou/p/4695131.html http://blog.csdn.net/viewcode/article/details/45 ...

  7. linux /boot目录下的文件分析

    一. Linux 启动流程 首先说一下Linux系统大概的启动过程: 1. 主机加电后, 系统首先加载BIOS, 这个BIOS是以写在主板上的. 2. BIOS启动后,执行一些例如开机自检,硬件初始化 ...

  8. python解析json文件报错No JSON object could be decoded

    2017-04-25 可用Nodepad++将json文件打开并以UTF8无BOM格式保存.

  9. ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

    链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...

  10. USE [EPPM] [dbo].[REFRDEL_CLEANUP]

    USE [EPPM] GO /****** Object: StoredProcedure [dbo].[REFRDEL_CLEANUP] Script Date: 2016/4/2 16:32:29 ...