Links: http://poj.org/problem?id=2486
Test instructions: A tree (apple) with N nodes (n<=100) on the tree, starting at node 1. There are several apples on each node, I can do the K-step operation (k<=200), each operation is moved from the current node to the adjacent node, and after the adjacent nodes will eat all the apples above and the apple no longer, adjacent to the two nodes are connected to each other. Ask how many apples can be eaten after a K-step operation.
Idea: Just start to think is the general problem of the tree-shaped backpack, dp[i][j] represents the root of I walk J nodes in the tree can eat the number of apples, to carry out state transfer, but so transfer the words of each transfer consumption is 2 (one back), But it is possible that the situation is not to go back to the starting point directly to a point and stop not moving forward.
So the state transfer above cannot satisfy the requirement. So expand to dp[i][j][k].
K=0, which represents the last stop in the subtree of I, does not return to the node i;k=1, which represents a J node in the subtree of I and finally goes back to the point J.
State transition equation:
Dp[u][i+2][1]=max (Dp[u][i+2][1],dp[u][i-j][1]+dp[v][j][1]), which means that the return to the root node can be pushed back to the sub-node.
Dp[u][i+1][0]=max (Dp[u][i+1][0],dp[u][i-j][1]+dp[v][j][0]), which indicates that a situation that does not return to the root node can be updated by returning the original to the root node by not returning to the child node.
Dp[u][i+2][0]=max (Dp[u][i+2][0],dp[u][i-j][0]+dp[v][j][1]), which indicates that a situation that does not return to the root node can be updated by returning the condition of the root node to the child node.
This grouping backpack represents a grouping of ideas, because the two situations represented by dp[i][j][0] and dp[i][j][1 are contradictory and must not be present at the same time. When the last update returns to the root node, the maximum value in all states is the answer.
Code:
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring > #include <ctime> #include <ctype.h> #include <iostream> #include <map> #include <queue > #include <set> #include <stack> #include <string> #include <vector> #define EPS 1e-8#define INF 0x3fffffff#define maxn 105#define maxm 205#define PI acos ( -1.0) #define seed 31//131,1313typedef Long Long ll;typedef u nsigned Long Long ull;using namespace Std;int head[maxn],top,ans,dp[maxn][maxm][2],n,k,x,y,a[maxn];void init () {memset (Head,-1,sizeof (head)); Memset (Dp,0,sizeof (DP)); memset (A,0,sizeof (a)); Top=0; Ans=0;} struct edge{int v; int next;} Edge[maxn*2];void Add_edge (int u,int v) {edge[top].v=v; Edge[top].next=head[u]; head[u]=top++;} void Dfs (int u,int f) {dp[u][0][0]=dp[u][0][1]=a[u]; for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; if (v==f) continue; DfS (v,u); for (int i=k;i>=0;i--) {for (int j=i;j>=0;j--) {Dp[u][i+2][1]=max (dp[u][ I+2][1],DP[U][I-J][1]+DP[V][J][1]); Dp[u][i+1][0]=max (Dp[u][i+1][0],dp[u][i-j][1]+dp[v][j][0]); Dp[u][i+2][0]=max (dp[u][i+2][0],dp[u][i-j][0]+dp[v][j][1]); }}}}int Main () {while (~scanf ("%d%d", &n,&k)) {init (); for (int i=1; i<=n; i++) scanf ("%d", &a[i]); for (int i=1;i<=n-1;i++) {scanf ("%d%d", &x,&y); Add_edge (x, y); Add_edge (Y,X); } dfs (a); int ans=0; for (int i=0;i<=k;i++) {if (Dp[1][i][0]>ans) ans=dp[1][i][0]; if (Dp[1][i][1]>ans) ans=dp[1][i][1]; } printf ("%d\n", ans); } return 0;}
POJ 2486 Apple Tree-shaped dp+ Group backpack