hdoj-1856-More is better【并查集】
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 18427 Accepted Submission(s): 6779
the more boys come, the better it will be. Of course there are certain requirements.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are
still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
(A ≠ B, 1 ≤ A, B ≤ 10000000)
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
4
2HintA and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
pid=1325" target="_blank">
1325
pid=1811" target="_blank">
1811
#include<stdio.h>
#include<string.h>
#include<algorithm>
int maxn=-1;
using namespace std;
int root[10000001];
int res[10000001];
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int x,int y){
int p=find(x),q=find(y);
if(p<=q) root[q]=p,res[p]+=res[q],maxn=max(maxn,res[p]); // 在合并的时候顺便查找最大值。降低时间开销。否则超时!。! else root[p]=q,res[q]+=res[p],maxn=max(maxn,res[q]);
return;
}
int main(){
int n;
while(~scanf("%d",&n)){
if(n==0){
printf("1\n");
continue;
}
memset(res,0,sizeof(res));
int i,x,y,a,b;
maxn=-1;
for(i=1;i<=10000000;++i) root[i]=i,res[i]=1;
for(i=1;i<=n;++i){
scanf("%d%d",&a,&b);
x=find(a);y=find(b);
if(x!=y){
unio(x,y);
}
}
printf("%d\n",maxn);
}
return 0;
}
hdoj-1856-More is better【并查集】的更多相关文章
- hdoj 2473 Junk-Mail Filter【并查集节点的删除】
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- HDU 1856 More is better(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdoj 3478 Catch(二分图判定+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...
- hdoj 1878 欧拉回路(无向图欧拉回路+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 思路分析:该问题给定一个无向图,要求判断该无向图是否存在欧拉回路:无向图判断存在欧拉回路的两个必 ...
- HDU 1213(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 并查集(HDOJ 1856)
并查集 英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n 合并两个集合 ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
随机推荐
- (49)zabbix事件是什么?事件来源有哪些分类
什么是zabbix 事件 在trigger的文章内,我们已经有用到事件,这个事件要讲概念真心不知道怎么说,就拿trigger事件来说,如果trigger从当前值ok转变为problem,那么我们称之为 ...
- java代码生成二维码
java代码生成二维码一般步骤 常用的是Google的Zxing来生成二维码,生成的一般步骤如下: 一.下载zxing-core的jar包: 二.需要创建一个MatrixToImageWriter类, ...
- 一、Numpy库与多维数组
# Author:Zhang Yuan import numpy as np '''重点摘录: 轴的索引axis=i可以理解成是根据[]层数来判断的,0表示[],1表示[[]]... Numpy广播的 ...
- UVa-208 Firetruck (图的DFS)
UVA-208 天道好轮回.UVA饶过谁. 就是一个图的DFS. 不过这个图的边太多,要事先判一下起点和终点是否联通(我喜欢用并查集),否则会TLE. #include <iostream> ...
- Linux下配置MySQL主从复制
一.环境准备 本次准备两台Linux主机,操作系统都为CentOS6.8, 都安装了相同版本的MySQL.(MySQL5.7). 主从服务器的防火墙都开启了3306端口. 相关信息如下: [主服务器] ...
- 网址导航[IT]
一.Linux/UNIX Linux公社:http://www.linuxidc.com/index.htm Linux命令大全:http://man.linuxde.net 伯乐在线:http:// ...
- Oracle跟踪分析数据库启动的各个阶段
目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...
- Python内置函数5
Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...
- Oracle 数据库有五个必需的后台进程,DBWR,LGWR,CKPT,SMON,PMON
SMON 是系统监视器(System Monitor)的缩写.如果Oracle实例失败,则在SGA中的任何没有写到磁盘中的数据都会丢失.有许多情况可能引起Oracle实例失败,例如,操作系统的崩溃就会 ...
- 使用Unity做2.5D游戏教程(一)
最近在研究Unity 3D,看了老外Marin Todorov写的教程很详细,就翻译过来以便自己参考,翻译不好的地方请多包涵. 如果你不了解2.5D游戏是什么,它基本上是个3D游戏而你可以想象是压扁的 ...