题目描述
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));
}