More is better
- 题目描述:
-
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#include<iostream>
#include<stdio.h>
#include<algorithm>
#define N 10000001
//将一个数定义在预处理部分
using namespace std;
int path[N];
int sum[N];
int findroot(int a){
int temp=a;
while (path[a] != -){
a=path[a];
}
int temp2;
//改进,使树的高度变矮,宽度增加,方便找根
while (path[temp]!= -){
temp2=path[temp];
path[temp]=a;
temp=temp2;
}
return a;
} int main (){
int n;
while (cin>>n){
for (int i=;i<=N;i++){
path[i]=-; sum[i]=;
}
int ans=;
int a,b;
while (n--){
cin >>a>>b;
a=findroot(a);
b=findroot(b);
if (a!=b){
path[a]=b;
sum[b]+=sum[a];
sum[a]=;
}
}
for (int i=;i<=N;i++){
if (sum[i] > ans)
ans =sum[i];
}
cout<<ans<<endl;
}
return ;
}跟上一道畅通工程几乎一样
加了一个在合并集合时同时合并(加)元素个数的功能
ps:不能想着在找根的过程中数元素个数,因为不一定是叶子,数出来的不对,
随机推荐
- swagger 自动生成接口测试用例
---整体更新一波--- 1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的: 2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加 ...
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- ant_<target>标签含义与使用
<target>标记目标 目标是一个或多个任务的集合,任务是一段可执行的代码:构建文件中包含一个项目,在项目内部声明了所有目标: <target name = "run&q ...
- Odd Gnome【枚举】
问题 I: Odd Gnome 时间限制: 1 Sec 内存限制: 128 MB 提交: 234 解决: 144 [提交] [状态] [命题人:admin] 题目描述 According to t ...
- spring-cloud-config——Quick Start
参考资料: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cl ...
- 1.6 安全认证与授权(springboot与安全)
引言:以下文档是学习尚硅谷关于springboot教学视频后整理而来! 一.安全 认证(Authentication):证明你是谁? 授权(Authorization):你能干什么? 参考资料: Sp ...
- 一个SQL查询出每门课程的成绩都大于80的学生姓名
name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 ...
- Java实现将文件或者文件夹压缩成zip
最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类. 这个工具类的功能为: ( ...
- php搜索附近人及显示男生女生分开
// 滚动切换标签样式 switchTab: function (e) { this.setData({ currentTab: e.detail.current }); this.checkCor( ...
- Java中使用HTTP阻塞式调用服务器API
应用场景:前端页面点击刷新,调用服务器A上Java接口,然后A调用服务器B的后台Python接口实时刷新后台数据库. 在这个场景中会涉及到两个问题:异步,Python服务器压力 (一)解决Python ...