POJ 2524 Ubiquitous Religions 解题报告
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34122 | Accepted: 16477 |
Description
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
end of input is specified by a line in which n = m = 0.
Output
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
Source
Alberta Collegiate Programming Contest 2003.10.18
题解
再来并查集,作为并查集学习的专题吧,这这道题考察连通块的个数,道题较为基础,每次合并的时候加入计数一次就好,但是要是没加到图里面的点才能计数,这样直接用总的点数减去它即可得到连通块的个数
#include <iostream>
#include <cstdio> const int maxn = 1e6+7; using namespace std; int father[maxn];
int cnt = 0; void init()
{
cnt = 0;
for (int i=0; i<maxn; i++)
father[i] = i;
} int fi(int x)
{
return x == father[x] ? x : father[x] = fi(father[x]);
} void unite(int x, int y)
{
int p1 = fi(x), p2 = fi(y);
if (p1 == p2) return;
father[p1] = p2;
cnt++;
} bool same(int x, int y)
{
if (fi(x) == fi(y))
return true;
return false;
} int main()
{
int n, m, a, b, c = 1;
while (~scanf("%d%d", &n ,&m))
{
init();
if (n == 0 && m == 0) break;
while (m--)
{
scanf("%d%d", &a, &b);
unite(a, b);
} printf("Case %d: %d\n", c++, n-cnt);
}
return 0;
}
POJ 2524 Ubiquitous Religions 解题报告的更多相关文章
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- POJ 2524 Ubiquitous Religions
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 20668 Accepted: ...
- poj 2524 Ubiquitous Religions(宗教信仰)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 30666 Accepted: ...
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions (幷查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23090 Accepted: ...
- poj 2524 Ubiquitous Religions (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
随机推荐
- Struts2 Action接收POST请求JSON数据及其实现解析
一.认识JSON JSON是一种轻量级.基于文本.与语言无关的数据交换格式,可以用文本格式的形式来存储或表示结构化的数据. 二.POST请求与Content-Type: application/jso ...
- linux下vim 查找命令
在命令模式下输入/word 这个是查找文件中“word”这个单词,是从文件上面到下面查找?word 这个是查找文件中“word”这个单词,是从文件下上面到面查找
- Java程序设计模式系列之适配器模式
理解适配器设计模式需要从生活中的场景进行联系,在生活当中有那些东西能够称为适配器呢?从字面上理解,"适配"的意思就是让一个东西和另一个东西配对,能够让他们一起工作,比如大家用的笔记 ...
- TSC打印机使用教程终极版
最近公司做一个资产采集的项目,之前做过此类项目,不过没有整理资料,借这次机会写一下,做个记录. 本教程使用的打印机型号:TSC TTP-244 Plus 官方文档 一.TSC打印机安装 1.机 ...
- struts2框架下的一个简单的ajax例子
举个例子 jsp页面: <%@ page language="java" import="java.util.*" pageEncoding=" ...
- Python 操作 MYSQL
本文介绍了 Python 操作 MYSQL.执行 SQL 语句.获取结果集.遍历结果集.取得某个字 段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多, 是一桌丰盛唯美的代 ...
- Linux下进行硬盘挂载、分区、删除分区,格式化,卸载方法
本文简单介绍了下文件系统及其操作(df命令),磁盘分区.格式化,还有最主要是挂载操作. 在这里对"挂载"做个说明,我们都知道文件系统是创建在磁盘上面的,每个文件系统都有独立的ino ...
- 反编译Unity3D手机游戏
[旧博客转移 - 2015年11月17日 10:08] 现在大部分U3D手游都没有进行加密处理,要反编译其实很简单 APK是安卓的安装包,安卓是基于Linux的,Linux的安装包一般都是zip,所以 ...
- 百度BAE环境搭建
一.申请 1.http://bce.baidu.com/index.html 2.购买应用引擎BAE需要实名认证:http://console.bce.baidu.com/qualify/#/qual ...
- C#常用8种排序算法实现以及原理简介
public static class SortExtention { #region 冒泡排序 /* * 已知一组无序数据a[1].a[2].--a[n],需将其按升序排列.首先比较a[1]与a[2 ...