[Poi2014]FarmCraft

題目描述

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
樹規+貪心搞一波。。
考試又考了貪心。。
先樹規一下處理出走完其子樹所需要的時間和其子樹完全下載完軟件的時間

按dp數組排序貪心搞一搞就行了

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#define Max(x,y) (x)>(y)?(x):(y)
#define ll long long
using namespace std;
const int MAXN = 500005;
int n,e=1,first[MAXN],size[MAXN],w[MAXN];
ll val[MAXN],Judge,Ans,f[MAXN];
bool vis[MAXN];
vector<int>G[MAXN];
vector<int>::iterator it;
 
 
template<typename _t>
inline _t read(){
    _t x=0,f=1;
    char ch=getchar();
    for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-')f=-f;
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+(ch^48);
    return x*f;
}
 
struct edge{
    int u,v,next;
}a[MAXN*3];
 
void push(int u,int v){
    a[e].u=u;a[e].v=v;
    a[e].next=first[u];
    first[u]=e++;
}
 
inline int cmp(int x,int y){return f[x]-2*size[x]>f[y]-2*size[y];}
 
void __dfs(int u,int fa){
    size[u]=1;
    for(int i=first[u];i;i=a[i].next)
        if(a[i].v!=fa){
            G[u].push_back(a[i].v);
            __dfs(a[i].v,u);
            size[u]+=size[a[i].v];
        }
}
 
void dfs(int u){
    size[u]=1;int num=0,cnt=0;f[u]=val[u];
    for(int i=0;i<G[u].size();i++){
        dfs(G[u][i]);
        size[u]+=size[G[u][i]];
    }
    if(G[u].size()==0)return;
    std :: sort(G[u].begin(),G[u].end(),cmp);
    int tmp = 0;
    for(int i=0;i<G[u].size();i++){
        f[u] = Max(f[u],f[G[u][i]]+tmp+1);
        tmp += (size[G[u][i]])*2;
    }
}
 
int main(){
    n=read<int>();
    for(int i=1;i<=n;i++)val[i]=read<ll>(),G[i].clear();
    for(int i=1;i<n;i++){
        int u=read<int>(),v=read<int>();
        push(u,v);push(v,u);
    }
    __dfs(1,0);dfs(1);
    printf("%lld\n",Max(f[1],val[1]+n*2-2));
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章