Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 26678 | Accepted: 13127 |
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
are numbered 1 to n. The 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
训练上是分治法,但我的第一感觉是并查集,所以先写并查集,以后再补分治#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) 收藏的更多相关文章
- 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 ...
- 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 ...
- strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏
(一) strace 命令 用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- 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 ...
- refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏
refresh的停车场 TimeLimit: 1000ms Memory limit: 65536K 题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是 ...
- JAVA 关于Icon,Image,ImageIcon的简单的对比参考 分类: Java Game 2014-08-14 17:08 210人阅读 评论(0) 收藏
转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...
- c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏
使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...
- 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏
最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...
随机推荐
- SQL循环添加表中的字段
USE BookDiscuss DECLARE @num INT DECLARE @numdiff INT DECLARE @table VARCHAR(50 ...
- 创建Java类并实例化深入理解
package com.sanguosha.java; import java.util.Scanner;//导入包 public class TestPerson { public static v ...
- Android Studio更新升级方法(转)
自从2013 Google I/O大会之后,笔者就将android ide开发工具从eclipse迁移到Android Studio了,android studio一直在更新完善,为了与时俱进,我们当 ...
- WEB-INF/views/menu/list.jsp (line: 26, column: 58) equal symbol expected
根本原因是由于单引号和双引号的混乱使用导致的. 解决办法: 将双引号里面的双引号改成单引号: 单引号里面的双引号该成单引号. 我的问题好像又不是这样的,<c:forEach var=" ...
- IntelliJ IDEA 14注册码
User:xring Key:21423-V4P36-U7W8K-8CYUK-93HXA-MKGZ5 User:arix Key:52998-LJT74-J7YEX-UPVT3-Q5GUF-5G4B5 ...
- Workflow Mailer Notifications设置
参考:http://www.docin.com/p-651716490.html http://www.360doc.com/content/12/0218/15/3200886_187602886. ...
- 解决vim无法返回上次的位置
就是在vim的配置文件 ~/.vimrc 中添加一行这个: au BufReadPost * |if line("'\"") <= line("$&quo ...
- zw版_Halcon-delphi系列教程_卫星航拍精确打击目标自动识别
<zw版·Halcon-delphi系列原创教程> 卫星航拍精确打击目标自动识别 这几天,俄罗斯空军在叙利亚大显神威,美军有意见了,说俄罗斯是狂轰滥炸,不是精确打击. 不过,战斗 ...
- Ceph的集群全部换IP
由于要对物理机器要做IP规划,所有物理机统一做到35网段,对于ceph集群来说,是有一定工作量的. 前提条件,ceph集群正常.原来的所有集群在44网段.mon地址是172.17.44.22 在44网 ...
- MySQL rename database如何做?
虽然MySQL里面有rename database的语法,但是只是在5.1.7 to 5.1.23提供的,其他版本并没有,要想做rename操作该如何做呢?percona提供了一个shell #!/b ...