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:不能想着在找根的过程中数元素个数,因为不一定是叶子,数出来的不对,
随机推荐
- Android项目开发第二天,关于GitHub
一. 今天在网上学习了如何使用GitHub,了解了GitHub是干什么的. 作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户.随着越来越多的应用程序转移到了云上,Github已经 ...
- 【python】python2.x中的除法
在生信分析中有许多时候我们需要用到除法,在经历无数次break out 之后我终于发现原来python 2.x中只有整除,而没有浮点除法,这就是没有基础的弊病. 那么如何在python 2.x中运用除 ...
- Vue基础进阶 之 自定义指令
自定义指令-----钩子函数 自定义指令 除了内置指令,Vue也允许用户自定义指令: 注册指令:通过全局API Vue.directive可以注册自定义指令: 自定义指令的钩子函数: bind: in ...
- dubbo多网卡时,服务提供者的错误IP注册到注册中心导致消费端连接不上
使用了虚拟机之后,启动了dubbo服务提供者应用,又连了正式环境的注册中心: 一旦dubbo获取的ip错误后, 这种情况即使提供者服务停掉,目前dubbo没有能力清除这类错误的提供者: (需要修改源码 ...
- 年度游戏圈2018白皮书解析手游折扣app哪个好及靠谱程度分析
2018年,随着全国暂停游戏版本的审核和发布<综合防控儿童青少年近视实施方案>(控制新的在线游戏数量),游戏行业受到的影响不小. 在游戏产业中,游戏行业2018年的收入同比增长5.2%,远 ...
- Python3 tkinter基础 Menu add_cascade 多级菜单 add_separator 分割线
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 多台linux主机间免密码登录
即在一台主机上登录另一台主机. 有2台linux主机A.B.A输入命令ssh B的ip地址以连接B,发现需要输入B的登录密码,怎样不需要输入密码呢? 步骤1: 在主机A中,输入ssh-keygen - ...
- JavaScript(类型转换、条件语句、循环、函数)
类型装换 转为数字类型 // Number console.log(Number(undefined)); //NaN console.log(Number(null)); //0 console.l ...
- (免费电影)苹果手机合并.ts视频
代码教程:https://mp.weixin.qq.com/s/6Oo8TOruePUxotC11zp0ag
- nodejs点滴
1.exports与module.exports http://cnodejs.org/topic/5231a630101e574521e45ef8 因为require指向了module.export ...