Luogu P1352 Prom without a boss (tree-shaped DP water question)

Topic description

A university has N staff, numbered 1~N. There is a subordinate relationship between them, that is to say, their relationship is like a tree with the principal as the root, and the parent node is the direct boss of the child node. Now there is an anniversary banquet. Every time a staff member is invited to the banquet, a certain happiness index Ri will increase. However, if the boss of a staff member comes to the dance party, then the staff member will not come to the dance party anyway. So, please program and calculate, which staff can be invited to maximize the happiness index, and find the maximum happiness index.

Input and output format

Input format:

 The first line contains an integer N. (1<=N<=6000)

Next N lines, the i+1th line represents the happiness index Ri of employee i. (-128<=Ri<=127)

Next N-1 lines, each line input a pair of integers L, K. Indicates that K is L's direct boss.

Enter 0 0 on the last line

output format

output maximum happiness index

Input and output example

input sample

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample output

5

Water question for tree DP. 
Water point 1: Each employee has only one boss, so a father array is enough to store pictures.
Water point 2: The state is less thief: only to go and not to go.
So, let's take a look at the dynamic transfer equation.
f[i][1]+=f[son][0];
f[i][0]+=max(f[son][1],f[son][0]);
The dynamic transition equation is well understood. The i represents the subtree with i as the follower, and 0 and 1 represent two states. Of course, the son refers to the son of i. How to find it in the program can be reflected. 
If he goes, the son will definitely not go, if he does not go, the son can go or not, find an optimal solution.
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 const int MAXN=6000+50;
 6 int n;
 7 int boss[MAXN];
 8 int dp[MAXN][2];
 9 void tree_dp(int k)
10 {
11     for(int i=1;i<=n;i++)
12     {
13         if(boss[i]==k)
14         {
15             tree_dp(i);
16             dp[k][0]+=max(dp[i][1],dp[i][0]);
17             dp[k][1]+=dp[i][0];
18         }
19     }
20 }
21 int main()
22 {
23     scanf("%d",&n);
24     for(int i=1;i<=n;i++)scanf("%d",&dp[i][ 1 ]);
 25      int root= 1 ;//This root node can be assigned a random value. You know, when looking for the root node upwards, no matter where it starts, it will eventually reach the root node
 26      for ( int i= 1 ;i<=n;i++ )
 27      {
 28          int l,k;
 29          scanf( " %d%d " ,&l,& k);
 30          boss[l]= k;//only save him Boss
 31      }
 32      while (boss[root]){root= boss[root];}//If there is a boss, keep approaching the tree until there is no boss
 33      tree_dp(root);
 34      printf( " %d\ n ",max(dp[root][ 1 ],dp[root][ 0 ]));//The boss goes and chooses the best solution
 35      return  0 ;
 36 }

 

 
 


Guess you like

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