Luo Valley Course P2014 - tree DP

Tree DP, the link table memory tree, the son point not regarded as the Prerequisite root, the child nodes of the first layer of gold point x, denoted as the current sub-node i, and a second layer of gold x x total subtree how many families choose, denoted by j, the number of families and the third layer of gold x x total selected sub-tree, denoted by k, it is the optimal solution jk i selected at Branch optimal solution plus x except at the point x sub i other than the tree sub-optimal solution k selected tree branch, namely: 

        dp[x][j]=max{dp[x][j],dp[i][j-k]+dp[x][k]

#include<bits/stdc++.h>
using namespace std;
struct cyka{
    int to,pre;
}e[1000000];
int cnt,f[1000][1000],head[100000],n,m;
void add(int from,int to){                //链接表
    e[++cnt].pre=head[from];
    e[cnt].to=to;
    head[from]=cnt;
}
void dp(int x){//当前点
    for ( int I = head [X]; I; I = E [I] .PRE) {       // pieces of sub-dot 
        int Y = E [I] .to; 
        DP (Y);        // movable Noriko point 
        for ( int J = m + . 1 ; J> 0 ; J -) {           // pieces from the beginning of the selection of subjects number x 
            for ( int K = . 1 ; K <= J; K ++) {    // pieces from the beginning of the selection of subjects i The number of 
                F [X] [J] = max (F [X] [J], F [Y] [JK] + F [X] [K]);   // state transition equation 
            } 
        } 
    } 
} 
int main () { 
    Scanf ( " % D% D ",&n,&m);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        cin>>f[i][1];
        add(x,i);
    }
    dp(0);
    printf("%d\n",f[0][m+1]);
}

 

Guess you like

Origin www.cnblogs.com/passione-123456/p/11198795.html