Problem Solution——[POI2014]FAR-FarmCraft Tree DP + Greedy Thought

A certain boss said that this question is very watery, and this konjac,,,, has been doing it for a long time,,, QAQ is really too weak

(I feel that the short paragraph of Chinese on the question in Luogu can't understand it at all, many conditions are not available. Speaking of, it will take a while to install directly,,, obviously more than that! Fortunately, there is Baidu translation... )

The meaning of the question: a tree, at the beginning of the node 1 (root), the edge rights are all 1. Each point has a weight, to minimize max (point weight + arrival time) <---

first of all points this seems to be a DP problem, but according to intuition, , should be linked to greed, because it feels time-consuming It 's been a long time to go,

but we found that we can't do it this way, because to go to a subtree, we will finish it, and it may be very late to go to another tree at this time, so it is not necessarily superior,

but we can It is found that starting from a point, the order of going to the nodes determines the answer,

so we consider sorting

, but it is difficult to analyze if there are too many nodes, so we first analyze the two sons as an example.

f[i] represents the maximum duration with i as the root,

so the purpose is to minimize f[1], and size[i] represents how long it will take

to traverse this point, because after traversing this subtree, you can go to another one, so There is a transition equation:

f[i]=max(f[a],f[b] + size[a] + 2), where a and b are two subtrees, and +2 is the round-trip time for the edge to a , this is to go to a first,

f[i]=max(f[b],f[a] + size[b] + 2), and then it is to minimize the maximum value of these 4 formulas,

PS: etc. Wait,,, it seems to be f[a]+1????, I'm too lazy to change it,,, let's make up f[a]+1 by yourself, it doesn't affect the result,

so first deduce it according to the method in the king's game :

First, it is better to go to a first,

Then there is max(f[a],f[b] + size[a] + 2) < max(f[b],f[a] + size[b] + 2)

because f[a] + size[b ] + 2 > f[a], f[b] + size[a] + 2 > f[b],

so the maximum value of these 4 expressions cannot be f[a] or f[b],

so To make the equation true, we can only hope that

f[b] + size[a] + 2 < f[a] + size[b] + 2

, so sorting on this condition can be used to

adjust various details QAQ for a long time

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define R register int
 4 #define AC 501000
 5 #define getchar() *o++
 6 #define LL long long
 7 char READ[40001000],*o=READ;
 8 int n,ans;
 9 int cost[AC],size[AC];
10 int date[AC * 2],Next[AC * 2],Head[AC],tot;
11 struct point{
12     int size;
13     LL f;
14 }p[AC];
15 LL f[AC];
16 inline int read()
17 {
18     int x=0;char c=getchar();
19     while(c > '9' || c < '0') c=getchar();
20     while(c >= '0' && c <= '9') x=x*10+c-'0',c=getchar();
21     return x;
22 }
23 
24 inline void add(int f,int w)
25 {
26     date[++tot]=w,Next[tot]=Head[f],Head[f]=tot;
27     date[++tot]=f,Next[tot]=Head[w],Head[w]=tot;
28 }
29 
30 inline bool cmp(point a,point b)
31 {
32     return b.f + a.size < a.f + b.size;
33 }
34 
35 void pre()
36 {
37     R a,b;
38     n= read();
 39      for (R i= 1 ;i<=n;i++) cost[i]= read();
 40      for (R i= 1 ;i<n;i++ )
 41      {
 42          a=read (),b= read();
 43          add(a,b);
 44      }
 45  }
 46  
47  void DFS( int x, int fa) // It is still necessary to pass the father, otherwise the part that joins the queue below cannot be judged , because the following have already visited 
48  {
 49      int now,tot= 0 ;
 50      if (x != 1) f[x] = cost[x]; // At least you have to install it yourself, but note that the first one is the last installed 
51      for (R i=Head[x]; i ;i= Next[i])
 52      {
 53          now= date[i];
 54          if (now == fa) continue ;
 55          DFS(now,x);
 56      }
 57      for (R i=Head[x]; i ;i= Next[i]) 
 58      {
 59          now= date[i];
 60          if (now != fa) 
 61              p[++tot]=(point) {size[now],f[now]}; // Can't add on it because once the next time When DFS is called, the queue is messed up 
62      }
63      sort(p+ 1 ,p+tot+ 1 ,cmp);
 64      for (R i= 1 ;i<=tot;i++ )
 65      {
 66          f[x]=max(f[x],p[i].f + size[x] + 1 );
 67          size[x] += p[i].size + 2 ; // anyway, this counts the same thing 
68      }
 69  }
 70  
71  int main()
 72  {
 73      freopen( " in.in " , " r " ,stdin);
 74      fread(READ, 1,40000000,stdin);
75     pre();
76     DFS(1,0);
77     printf("%lld\n",max(f[1],(LL)(n * 2 - 2 + cost[1])));
78     fclose(stdin);
79     return 0;
80 }

 

[POI2014]FAR-FarmCraft

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340090&siteId=291194637