poj 2524 Ubiquitous Religions 一简单并查集
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 22389 | Accepted: 11031 |
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
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
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

#include<algorithm>
#include<iostream>
#include<cstring>
#include<set>
#include<cstdio>
using namespace std;
int father[];
int vis[];//用来标记出现过没有;
void begin(int m)
{
for(int i=;i<=m;i++)
{father[i]=i;vis[i]=;} }
int find(int x)
{
if(father[x]!=x)
{
father[x]=find(father[x]);
}
return father[x];
}
int main()
{
int m,n,x,y,a,b,mm,ans,k=;
while(scanf("%d %d",&m,&n) && m+n)
{ ans=;mm=; //mm 统计的是出现的个数,减去就是没出现的个数;
begin(m);
for(int i=;i<=n;i++)
{
scanf("%d %d",&x,&y);//输入尽量用scanf(344ms),cin(4688ms)差别略大啊;
if(x<=m && y<=m)
{
a=find(x);b=find(y);//各找各的父亲;
father[a]=b;
if(vis[x]==) //标记统计出现过多少个数;
{mm++;vis[x]=;}
if(vis[y]==)
{mm++;vis[y]=;}
}
}
for(int i=;i<=m;i++) //统计共有几个集合,实在不好想,用了个笨方法;
{
if(father[i]==i && vis[i]==)//父亲的父亲,是他本身,并且他已经出现过了
ans++;
}
printf("Case %d: %d\n",k++,ans+m-mm);
}
return ;
}
poj 2524 Ubiquitous Religions 一简单并查集的更多相关文章
- POJ 2524 独一无二的宗教(裸并查集)
题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- [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: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions (幷查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23090 Accepted: ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- 【原创】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: ...
随机推荐
- <%@ include> <jsp:include>
1. <%@ include file=”relativeURI” %> 这个效果是将引入的文件内容直接加入当前JSP然后再编译,会造成的问题就是如果引入的页面中也写了head,conte ...
- Struts2数据验证机制
1. 手动验证的实现 只需要在继承ActionSupport类的情况下,直接重写validate()方法即可.使用validate()方法可以对用户请求的多个Action方法进行验证,但其验证的逻辑是 ...
- oracle分析函数之windowing_clause--rows
Some analytic functions allow the windowing_clause. In the listing of analytic functions at the end ...
- TensorFlow进阶(五)---图与会话
图与会话 图 tf.Graph TensorFlow计算,表示为数据流图.一个图包含一组表示 tf.Operation计算单位的对象和tf.Tensor表示操作之间流动的数据单元的对象.默认Graph ...
- SVG 可伸缩矢量图形 简介 Path路径
w3school:http://www.w3school.com.cn/svg/svg_intro.asp SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG ...
- OpenGL入门学习(转载)
说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色 ...
- linux free命令详解和使用实例(查看内存使用率)
转载:http://www.jb51.net/LINUXjishu/152017.html 1.命令格式: free [参数] 2.命令功能: free 命令显示系统使用和空闲的内存情况,包括物理内存 ...
- 轻量级UIImageView分类缓存 库 AsyncImageView 使用
轻量级UIImageView分类缓存 库 AsyncImageView 使用 一: AsyncImageView 主页:https://github.com/nicklockwood/AsyncIma ...
- 取石子 (四)_nyoj_161(博弈-奇异矩阵).java
取石子 (四) 时间限制: 1000 ms | 内存限制: 65535 KB 难度: 4 描述 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是 ...
- daemon与服务(service)及重启服务的方法
简单地说,系统为了某些功能必须要提供一些服务(不论是系统本身还是网络方面),这个服务就称为service.而实现这个service的程序我们就称它为daemon.实现某个服务是需要一个daemon在后 ...