POJ 2486-apple Tree (DP) (difficult)

Source: Internet
Author: User



Test instructions: A tree, n points (1-n), n-1, each point has a weight, to start from 1, walk v-step, up to the right value to traverse


idea: (thinking transfer from http://blog.csdn.net/libin56842/article/details/10101807)
Tree-shaped DP, a classic tree-shaped DP. It is easy to think of the maximum number of apples that can be obtained by using dp[root][k] in a subtree that is rooted in root, and then we are accustomed to thinking of assigning K steps to all sub-nodes of root, that is, to carry out a backpack, so that the optimal solution of this state can be obtained. However, there is a problem, that is, when carrying out a backpack, whether the return of a child after the end of the root node will have an impact on whether the latter can still be assigned, in order to solve this problem, we only need to add one dimension in the state can be, with dp[root][k][0] Indicates the maximum number of steps to take in the subtree root, and finally back to root, dp[root][k][1] indicates the maximum number of k steps in the subtree root, and the last not to go back to root. Thus, the equation of state transition can be obtained:


Dp[root][j][0] = MAX (dp[root][j][0], dp[root][j-k][0] + dp[son][k-2][0]);//starting from S, to go back to s, need to walk two more steps s-t,t-s, assigned to the T subtree K step, Other subtrees j-k step, all returned
DP[ROOT][J]][1] = MAX (dp[root][j][1], dp[root][j-k][0] + dp[son][k-1][1]);//go through the other subtrees of S, go back to S, traverse the T subtree, do not return in the current subtree, take one more step

DP[ROOT][J][1] = MAX (dp[root][j][1], dp[root][j-k][1] + dp[son][k-2][0]);//Do not go back to s (other subtree to s), return in the T subtree, same as two more steps


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Vector>using namespace Std;int n,c;int cnt[110];vector<int> es[110];int dp[110][220][2];void dfs (int u,int pa)    {for (int i=0;i<=c;i++) dp[u][i][1]=dp[u][i][0]=cnt[u];        for (int i=0;i<es[u].size (); i++) {int v=es[u][i];        if (V==PA) continue;        DFS (V,U);                    for (int j=c;j>=1;j--) for (int k=0;k<j;k++) {if (k<=j-2) {                    DP[U][J][1] = max (dp[u][j][1],dp[v][k][1]+dp[u][j-k-2][1]);                Dp[u][j][0] = max (dp[u][j][0],dp[v][k][1]+dp[u][j-k-2][0]);            } dp[u][j][0]=max (dp[u][j][0],dp[v][k][0]+dp[u][j-k-1][1]);        }}}int Main () {while (~scanf ("%d%d", &n,&c)) {memset (dp,0,sizeof (DP));            for (int i=1;i<=n;i++) {es[i].clear ();        scanf ("%d", &cnt[i]);   }     for (int i=1;i<n;i++) {int u,v;            scanf ("%d%d", &u,&v);            Es[u].push_back (v);        Es[v].push_back (U);        } dfs (1,0);    printf ("%d\n", dp[1][c][0]); } return 0;}


POJ 2486-apple Tree (DP) (difficult)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.