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:不能想着在找根的过程中数元素个数,因为不一定是叶子,数出来的不对,
随机推荐
- python 设置位置参数是整数类型
- Linux 查看系统负载
查看系统负 # 查看系统负载 命令:uptime :: up :, users, load average: 0.00, 0.00, 0.00 注:load average: 0.00, 0.00, ...
- VS2013的x86汇编语言开发环境配置
转载:https://blog.csdn.net/infoworld/article/details/45085415 转载:https://blog.csdn.net/u014792304/arti ...
- 【常见错误】Quartz常见错误
1.集群之后把其中一个Quartz服务停了,其他的也不接手工作 问题描述 集群之后,A节点执行了大多数任务,B节点大部分时间处于空闲,停掉A节点,B节点也不会接手工作. 解决方式 修改Quartz的配 ...
- 8th,常用模块、正则表达式
re模块 什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.内嵌在Python中,通过re模块实现.正则表达式模 ...
- windows 添加开始菜单
C:\Users\用户名(为你设置的电脑名称)\AppData\Roaming\Microsoft\Windows\Start Menu C:\ProgramData\Microsoft\Window ...
- Vue:(一)概况
Vue:https://cn.vuejs.org/ (一)Vue概况 Vue本身并不是一个框架 Vue结合周边生态构成一个灵活的.渐进式框架 声明式渲染 组件系统 客户端路由 状态管理 构建工具 (二 ...
- linux中搭建vue-cli
1 安装nvm依赖并配置环境变量在 sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh ...
- python+selenium,实现带有验证码的自动化登录功能
python+selenium的环境准备,请自行安装完成,这里直接贴代码,方便做项目时直接使用. import time from selenium import webdriver from PIL ...
- java笔记 -- GregorianCalendar和DateFormateSymbols 类方法
java.util.GregorianCalendar new GregorianCalendar() 构造一个日历对象, 用于表示默认地区,默认时区的当前时间. new GregorianCalen ...