hdu1856
Mr Wang wants some boys to help him with a project. Because the project is rather complex, 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.
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
4
2
解:这个题是一个并查集的题,标记的红色字体是题目的关键,他只想留住有朋友关系的人。但是当n等于0时,也就是所有的人都互不认识的时候,就随机找一个人留下,当时这一点没有看到。当时看题目以为是如果所有人中的若干个人是朋友关系,就选出任意两个;如果一个人与其他的人无关系就让这个人留下,结果看到给出的例子纠结了好久,不知道哪里错了。正确的是合并两个人的时候判断一下两个人是否已经直接或者间接有朋友关系,再合并一下这个集合中所包含元素的个数,找出最大的集合人数。所以做题的时候不能看到题目想当然,结合数据理解题意。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int pre[maxn],num[maxn];
void creat()
{
for(int i=;i<maxn;i++)
{
pre[i]=i;
num[i]=;
}
}
int findroot(int root)
{
if(root==pre[root])
return root;
pre[root]=findroot(pre[root]);
return pre[root];
}
int main()
{
int t,n,m,maxx;
while(scanf("%d",&t)!=EOF)
{
if(t==)
{
puts("1");
continue;
}
maxx=-;
creat();
while(t--)
{
scanf("%d %d",&n,&m);
int root1=findroot(n);
int root2=findroot(m);
if(root1!=root2)
{
if(root1<root2)
swap(root1,root2);
pre[root2]=root1;
num[root1]+=num[root2];
maxx=max(maxx,num[root1]);
}
}
printf("%d\n",maxx);
}
return;
}
hdu1856的更多相关文章
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- hdu-1856 More is better---带权并查集
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1856 题目大意: 一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数 解题思路: ...
- hdu1856 More is better 基础并查集
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...
- hdu1856 选出更多的孩子
题目大意: 老师选取2个学生对应的号码,这两人视作朋友,同时朋友的朋友也可以看成自己的朋友. 最后老师选出一个人数最多的朋友圈. 这里学生的人数不大于10^7,所以操作时需要极为注意,操作步数能省则省 ...
- hdu1856 More is better (并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- hdu1856 并查集
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1856/ 题目就是要求并查集中各树的大小的最大值,我们只要在根节点处存树的大小就可以,合并也是合并根节点的数,最后 ...
- 并查集(Union-Find) 应用举例 --- 基础篇
本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...
随机推荐
- Python爬虫与一汽项目【综述】
项目来源 这个爬虫项目是 去年实验室去一汽后的第一个项目(基本交工,现在处于更新维护阶段).内容大概是,获取到全国31个省份政府的关于汽车的招标公告,再用图形界面的方式展示爬虫内容.在完成政府招标采购 ...
- 硬件笔记之MacMini开启HiDPI
0x00 概述 先科普一下,有关retina和HiDPI那点事 ,Macmini在2k显示器的显示太小了,看起来费眼,没办法,苹果原生HiDPI是支持4k显示器的,所以以后买显示器,直接买4k的一步到 ...
- pywinauto简单操作写字板的例子
前段时间写了做web程序界面自动化的简单例子,今天写一下windows gui程序界面自动化测例子吧. ps.咱中国人YinKaisheng封装的UIAutomation库也很好用,https://g ...
- STA 141A, Homework
STA 141A, Homework 2Due April 30th 2019 (by 8 am) Name:Student ID:Section:Names of your study mates: ...
- [Python数据挖掘]第6章、电力窃漏电用户自动识别
一.背景与挖掘目标 相关背景自查 二.分析方法与过程 1.EDA(探索性数据分析) 1.分布分析 2.周期性分析 2.数据预处理 1.数据清洗 过滤非居民用电数据,过滤节假日用电数据(节假日用电量明显 ...
- Xml & Tomcat
文档声明: 简单声明, version : 解析这个xml的时候,使用什么版本的解析器解析 <?xml version="1.0" ?> encoding : 解析xm ...
- 【JavaScript】标准日期、中国标准时间、时间戳、毫秒数互转
转载自:https://blog.csdn.net/IT429/article/details/78341847 看到的一篇比较有用的前端js时间转换方法,留个备份 首先要明确这三种格式是什么样子的: ...
- 8位、16位、32位单片机中的“XX位”指什么?
32位单片机的32位是指单片机的“字长”,也就是一次运算中参与运算的数据长度,这个位是指二进制位. 如果总线宽度与CPU一次处理的数据宽度相同,则这个宽度就是所说的单片机位数. 如果总线宽度与CPU一 ...
- Mysql推荐使用规范
一.基础规范 使用InnoDB存储引擎支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 推荐使用utf8mb4字符集无需转码,无乱码风险, 支持emoji表情以及部分不常见汉字 ...
- shell脚本中gsub的应用
(1)文件filename的内容 cat awk_file 1 2 3 $1,200.00 1 2 3 $2,300.00 1 2 3 $4,000.00 (2)去掉第四列的$和,并汇总第四列的和 a ...