(Tree DP entry title) Anniversary party (no party boss) HDU - 1520

Meaning of the questions:

There is a company to hold a party. In order for everyone to be free from the constraints of his direct supervisor and have a good time, company leaders decided: If you invite someone, then we will not be invited to his direct supervisor, but that person's boss's boss, boss's boss's boss and so can invite. Everyone has the largest known only one boss. 
Known company that everyone can add to the party atmosphere for the party number, seeking an invitation program, and the maximum value of the atmosphere.

Ideas:

Tree DP entry title

① first set: Array dp [i] [0] is the i personally took part in the dance of joy when the value of the sub-tree and

                  Array dp [i] [1] is the first time i personally did not participate in this dance of joy and value of the sub-tree

② From this we can deduce for the state transition equation is i

            dp [i] [0] = a [i] + Σdp [j] [1] (i j is the parent node)  

    DP [I] [. 1] = ΣMax (DP [j] [. 1], DP [j] [0])   (I j is the parent node)

 

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
 using namespace std;
 const int maxn=6003;
 int a[maxn],dp[maxn][2];
 int n;
 vector<int> edge[maxn];
 void dfs(int x,int fa)
 {
     dp[x][0]=a[x],dp[x][1]=0;
     for(int i=0;i<edge[x].size();i++){
         if(edge[x][i]!=fa){
             dfs(edge[x][i],x);
             dp[x][0]+=dp[edge[x][i]][1];
             dp[x][1]+=max(dp[edge[x][i]][0],dp[edge[x][i]][1]);
         }    
     }
 }
 int main ()
 {
     while(scanf("%d",&n)!=EOF){
         for(int i=1;i<=n;i++)
             scanf("%d",&a[i]);
         int l,k;
         for(int i=1;i<=n;i++)
             edge[i].clear();
         memset(dp,0,sizeof(dp));
         while(scanf("%d%d",&l,&k)&&l&&k){
             edge[l].push_back(k);
             edge[k].push_back(l);
         }
        dfs(1,0);
        printf("%d\n",max(dp[1][0],dp[1][1]));
     }
    return 0;
 }

Guess you like

Origin www.cnblogs.com/overrate-wsj/p/12171553.html