题目

In a village called Byteville, there are houses connected with N-1 roads. For each pair of houses, there is a unique way to get from one to another. The houses are numbered from 1 to . The house no. 1 belongs to the village administrator Byteasar. As part of enabling modern technologies for rural areas framework, computers have been delivered to Byteasar's house. Every house is to be supplied with a computer, and it is Byteasar's task to distribute them. The citizens of Byteville have already agreed to play the most recent version of FarmCraft (the game) as soon as they have their computers. Byteasar has loaded all the computers on his pickup truck and is about to set out to deliver the goods. He has just the right amount of gasoline to drive each road twice. In each house, Byteasar leaves one computer, and immediately continues on his route. In each house, as soon as house dwellers get their computer, they turn it on and install FarmCraft. The time it takes to install and set up the game very much depends on one's tech savviness, which is fortunately known for each household. After he delivers all the computers, Byteasar will come back to his house and install the game on his computer. The travel time along each road linking two houses is exactly 1 minute, and (due to citizens' eagerness to play) the time to unload a computer is negligible. Help Byteasar in determining a delivery order that allows all Byteville's citizens (including Byteasar) to start playing together as soon as possible. In other words, find an order that minimizes the time when everyone has FarmCraft installed.

mhy 住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子。 mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为Ci。 树上的每条边mhy能且仅能走两次,每次耗费1单位时间。mhy送完所有电脑后会回自己家里然后开始装zhx牌杀毒软件。 卸货和装电脑是不需要时间的。 求所有妹子和mhy都装好zhx牌杀毒软件的最短时间。

Input

The first line of the standard input contains a single integer N(2<=N<=5 00 000) that gives the number of houses in Byteville. The second line contains N integers C1,C2…Cn(1<=Ci<=10^9), separated by single spaces; Ci is the installation time (in minutes) for the dwellers of house no. i. 
The next N-1 lines specify the roads linking the houses. Each such line contains two positive integers a and b(1<=a<b<=N) , separated by a single space. These indicate that there is a direct road between the houses no. a and b.

Output

The first and only line of the standard output should contain a single integer: the (minimum) number of minutes after which all citizens will be able to play FarmCraft together.

Sample Input

6 1 8 9 6 3 2 1 3 2 3 3 4 4 5 4 6

Sample Output

11

HINT

Explanation: Byteasar should deliver the computers to the houses in the following order: 3, 2, 4, 5, 6, and 1. The game will be installed after 11, 10, 10, 10, 8, and 9 minutes respectively, in the house number order. Thus everyone can play after 11 minutes. If Byteasar delivered the game in the following order: 3, 4, 5, 6, 2, and 1, then the game would be installed after: 11, 16, 10, 8, 6, and 7 minutes respectively. Hence, everyone could play only after 16 minutes,

题解:

设f[x]为x的子树中全部安完软件的时间,对于x节点的a,b儿子,设他们的子树大小为siz[a],siz[b],显然遍历一遍子树a的时间就是2*siz[a],然后分类讨论

若先走a,后走b,那么f[x]=max(f[a]+1,f[b]+2siz[a]+1) 若先走b,后走a,那么f[x]=max(f[a]+2siz[b]+1,f[b]+1)

对于f[a]+1和f[b]+1我们可以不用考虑,那么如果先走a更优,当且仅当:

f[b]+2siz[a]+1<f[a]+2siz[b]+1

转移

f[a]-2siz[a]>f[b]-2siz[b]

那么直接将x的儿子按照f[i]-2siz[i]排序,先走f[i]-2siz[i]的就好了

Attention!!:根节点的软件是最后安装的,所以要特判,ans=max(2*(n-1)+1+time[1],f[1])

代码

(有些丑不要介意)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=;
int n,cnt;
int to[maxn<<],next[maxn<<],head[maxn],v[maxn],p[maxn],f[maxn],siz[maxn];
void add(int a,int b){
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
bool cmp(int a,int b){
return f[a]-*siz[a]>f[b]-*siz[b];
}
void dfs(int x,int fa){
int i,sum=;
siz[x]=;
for(i=head[x];i!=-;i=next[i])
if(to[i]!=fa)
dfs(to[i],x),siz[x]+=siz[to[i]];
p[]=;
for(i=head[x];i!=-;i=next[i])
if(to[i]!=fa)
p[++p[]]=to[i];
sort(p+,p+p[]+,cmp);
if(x!=)f[x]=v[x];
for(i=;i<=p[];i++)
f[x]=max(f[x],f[p[i]]+sum+),sum+=*siz[p[i]];
}
int main(){
scanf("%d",&n);
int i,a,b;
memset(head,-,sizeof(head));
for(i=;i<=n;i++) scanf("%d",&v[i]);
for(i=;i<n;i++) scanf("%d%d",&a,&b),add(a,b),add(b,a);
dfs(,);
printf("%d",max(f[],*(siz[]-)+v[]));
return ;
}

最新文章

  1. java06
  2. MongoDB一键式安装工具
  3. 彻底解决mysql中文乱码的办法 ???
  4. 关于Android 应用保活
  5. JS原生效果瀑布流布局的实现(一)
  6. backup1
  7. swap分区
  8. 《图解tcp/ip》读书笔记(二)
  9. HYSBZ1036 树链剖分
  10. PPT美化大师
  11. PHP中的多态
  12. (转载)在vmware中简单配置vsftpd服务器
  13. MySQL删除重复记录的方法
  14. Angularjs directive全面解读(1.4.5)
  15. 默认路由、RIPv2、OSPF、EIGRP配置(全网全通)
  16. css3关键帧动画实现轮播效果
  17. C# 利用反射动态给模型Model 赋值
  18. Lodop扁宽横向241mm*93mm这种怪异的纸张如何设置
  19. Linux系统及常用软件的安装
  20. win10 安装php

热门文章

  1. 细说Java多线程之内存可见性笔记
  2. Java实现 LeetCode 768 最多能完成排序的块 II(左右便利)
  3. Java实现蓝桥杯历届试题格子刷油漆
  4. Java实现 LeetCode 599 两个列表的最小索引总和(使用hash提高效率)
  5. Java GUI 窗体事件
  6. Java实现 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…
  7. java实现第五届蓝桥杯供水设施
  8. 利用tcpdump命令统计http的GET和POST请求
  9. CentOS8.1操作系下使用通用二进制包安装MySQL8.0(实践整理自MySQL官方)
  10. Linux学习初级篇-鸟哥的Linux私房菜 基础学习篇(第四版)