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< ...
随机推荐
- clipboard 在 vue 中的使用
简介 页面中用 clipboard 可以进行复制粘贴,clipboard能将内容直接写入剪切板 安装 npm install --save clipboard 使用方法一 <template&g ...
- Django 模版语法 三
使用自定义simple_tag 在 app01 下面创建 templatetags 文件夹,在创建 my_tag.py 文件,内容如下: from django import template fro ...
- 避免使用aireplay-ng指令时出现AP通道不对的方法
本方法搜集网络:具体使用为在调试网卡为监听模式,使用airodump-ng指令扫描wifi后,需要先执行"airmon-ng stop wlan0"指令,然后再进行一系列抓包等操作 ...
- Hive中文注释乱码解决方案(2)
本文来自网易云社区 作者:王潘安 执行阶段 launchTask 回到Driver类的runInternal方法,看以下执行过程.在runInternal方法中,执行过程调用了execute方法 ...
- map 插入数据的方式局别
#include<map> #include<iostream> usingnamespace std; int main() { map <int, int> m ...
- git删除本地所有的更改
删除本地所有为暂存的修改: git checkout -f 如果有修改以及加入暂存区的话 那么 使用如下命令: git reset --hard git clean -xdf
- HDU 2435 There is a war
There is a war Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...
- xtu数据结构 G. Count the Colors
G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- 刷题总结——愤怒的小鸟(NOIPDAY2T3)
题目: 题目背景 NOIP2016 提高组 Day2 T3 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔.简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Ki ...
- 如何将文件上传到ftp
方法1(推荐,炒鸡简单):双击我的电脑,在地址栏里输入你的ftp地址回车(比如: ftp://220.103.86.96),然后会弹出一个输入登录账号和密码的对话框,输入你的ftp账号和密码回车便进入 ...