1. 程式人生 > >[BZOJ]3829: [Poi2014]FarmCraft

[BZOJ]3829: [Poi2014]FarmCraft

with called mst minimum 需要 col online exp com

  題解: 怎麽看上去 這麽不可做啊......想了想 樹dp的話

  設 $ dp[x] $表示以x為根的子樹安裝完成的最大時間

  我們考慮 對兩個子樹決策時

  若 先走x 則 $ ans=max(ans,max(f[x]+1,f[y]+2*(size[x])+1)) $

  若 先走y 則 $ ans=max(ans,max(f[x]+2*(size[y])+1,f[y]+1)) $

  對上述兩個式子考慮

    若 $ f[y]+2*size[x]<f[x]+2*size[y] $ 則走x

  那麽 若$ f[x]-2*size[x]>f[y]-2*size[y] $ 則走x

  那麽我們對於每個節點的兒子按照 $ f[x]-2*size[x] $排序 貪心走即可

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=5e5+10;
const double eps=1e-8;
#define ll long long
using namespace std;
const int inf=2e9;
struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar();
    return x*f;
}

int n,a[MAXN];
int dp[MAXN],num[MAXN];

typedef struct node{
    int v,k;
    friend bool operator<(node aa,node bb){return aa.k>bb.k;}
}node;
node st[MAXN];
int tot;

void dfs(int x,int pre){
    num[x]=1;
    link(x){
	if(j->t==pre)continue;
	dfs(j->t,x);
	num[x]+=num[j->t];
    }
    if(x!=1)dp[x]=a[x];
    int sum=0;tot=0;
    link(x){
	if(j->t==pre)continue;
	st[++tot]=(node){j->t,dp[j->t]-2*num[j->t]};
    }
    sort(st+1,st+tot+1);
    inc(i,1,tot)dp[x]=max(dp[x],dp[st[i].v]+sum+1),sum+=2*num[st[i].v];
}


int main(){
    n=read();
    inc(i,1,n)a[i]=read();
    int u,v;
    inc(i,2,n)u=read(),v=read(),add(u,v),add(v,u);
    dfs(1,0);
    dp[1]=max(dp[1],a[1]+2*(n-1));
    printf("%d\n",dp[1]);
}

  

3829: [Poi2014]FarmCraft

Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 554 Solved: 270
[Submit][Status][Discuss]

Description

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,

[BZOJ]3829: [Poi2014]FarmCraft