FarmCraft --(树形DP)

题目描述

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个妹子安装时间为

树上的每条边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

, separated by single spaces;

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

输出格式

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

数据范围与提示

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]+2*siz[a]+1)
若先走b,后走a,那么f[x]=max(f[a]+2*siz[b]+1,f[b]+1)

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

移项后:

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

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

注意根节点要特判,ans=max(2*(n-1)+1+time[1],f[1])

代码:

  

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #define M 500500
 6 using namespace std;
 7 struct abcd{
 8     int to,next;
 9 }table[M<<1];
10 int head[M],tot;
11 int n;
12 int a[M],f[M],g[M];
13 //f表示遍历所需时间,g表示遍历并装机完毕所需时间
14 void Add(int x,int y)
15 {
16     table[++tot].to=y;
17     table[tot].next=head[x];
18     head[x]=tot;
19 }
20 bool Compare(int x,int y)
21 {
22     return g[x]-f[x] > g[y]-f[y];
23 }
24 void Tree_DP(int x,int from)
25 {
26     int i;
27     for(i=head[x];i;i=table[i].next)
28         if(table[i].to!=from)
29             Tree_DP(table[i].to,x);
30     static int stack[M];int top=0;
31     for(i=head[x];i;i=table[i].next)
32         if(table[i].to!=from)
33         {
34             f[table[i].to]+=2;
35             g[table[i].to]=max(g[table[i].to]+1,f[table[i].to]);
36             stack[++top]=table[i].to;
37         }
38     sort(stack+1,stack+top+1,Compare);
39     g[x]=a[x];
40     for(i=1;i<=top;i++)
41     {
42         g[x]=max(g[x],f[x]+g[stack[i]]);
43         f[x]+=f[stack[i]];
44     }
45 }
46 int main()
47 {
48     int i,x,y;
49     cin>>n;
50     for(i=1;i<=n;i++)
51         scanf("%d",&a[i]);
52     for(i=1;i)
53     {
54         scanf("%d%d",&x,&y);
55         Add(x,y);Add(y,x);
56     }
57     Tree_DP(1,0);
58     cout<1],f[1]+a[1])<<endl;
59     return 0;
60 }
View Code

 

 

 

 

你可能感兴趣的:(FarmCraft --(树形DP))