原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html
题目描述
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牌杀毒软件的最短时间。
输入
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.
输出
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.
样例输入
6
1 8 9 6 3 2
1 3
2 3
3 4
4 5
4 6
样例输出
11
题解
贪心
设f[i]表示子树i全部安装完成所需的最小总时间。
那么对于一个某结点x,f[x]一定大于等于c[x]。
若其为非叶子结点,考虑其子树a和b。
若先安装a再安装b,则a消耗的时间为f[a]+1,b消耗的时间为2*si[a]+f[b]+1
若先安装b再安装a,则a消耗的时间为2*si[b]+f[a]+1,b消耗的时间为f[b]+1
若先安装a合适,则必有2*si[a]+f[b]+1>2*si[b]+f[a]+1,即f[a]-2*si[a]<f[b]-2*si[b]
于是可以将x的所有子树按照f-2si从小到大排序,依次统计答案。
由于题目描述必须先完成2~n再完成1,所以应先将1的时间看作0,再分开计算。
#include <cstdio>
#include <algorithm>
#define N 500010
using namespace std;
struct data
{
int f , si;
}k[N] , a[N];
int head[N] , to[N << 1] , next[N << 1] , cnt , c[N];
bool cmp(data a , data b)
{
return a.f - 2 * a.si > b.f - 2 * b.si;
}
void add(int x , int y)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
void dfs(int x , int fa)
{
int i , tot = 0 , now = 1;
k[x].f = c[x] , k[x].si = 1;
for(i = head[x] ; i ; i = next[i]) if(to[i] != fa) dfs(to[i] , x) , k[x].si += k[to[i]].si;
for(i = head[x] ; i ; i = next[i]) if(to[i] != fa) a[++tot] = k[to[i]];
sort(a + 1 , a + tot + 1 , cmp);
for(i = 1 ; i <= tot ; i ++ ) k[x].f = max(k[x].f , a[i].f + now) , now += 2 * a[i].si;
}
int main()
{
int n , i , x , y , t;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &c[i]);
t = c[1] , c[1] = 0;
for(i = 1 ; i < n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
dfs(1 , 0);
printf("%d\n" , max(k[1].f , t + 2 * (k[1].si - 1)));
return 0;
}