Last night debug for a long time always find out what was wrong, and this morning another look found that they have been promoted to tease than Beta 2.0 Version.
Personally feel this is entitled HDU 4003 of the weakened version.
Treat every subtrees tree as a class of goods, and choose at most one item in each category. Then the problem turns into the most basic packet knapsack problem.
Dp[s][c][k] c = = 1 o'clock, indicating that the S node is not returned when the maximum gain of k, c = = 0 o'clock, indicating the return of the S node when the maximum gain of K-step.
You can update the DP from bottom to top of Dfs. It is worth mentioning that to ensure that each node's dp[s][c][k] will only be DFS once, that is, the update cannot be repeated.
Because duplicate DFS is not guaranteed to select more than one item in each category.
#include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include < cstring> #include <queue> #include <cmath> #include <stack> #include <map> #pragma comment ( linker, "/stack:1024000000"); #define EPS (1e-8) #define LL long long#define ULL unsigned long long ll#define _ll __int64#de Fine _inf 0x3f3f3f3f#define Mod 1000000007#define LM (A, B) (((ULL) (a)) << (b)) #define RM (A, B) (((ULL) (a) >> )) using namespace Std;const LL MAXN = 10010;struct n{int u,v,w,next;} edge[maxn*2];int head[maxn];int top;void Link (in T u,int v,int w =-1) {edge[top].u = u; EDGE[TOP].V = v; EDGE[TOP].W = W; Edge[top].next = Head[u]; Head[u] = top++;} void Init_head_top (int n) {memset (head,-1,sizeof (int) * (n+2)); Top = 0;} LL dp[110][2][210]; LL price[210];void dfs (int s,int pre,int c,int k) {if (K < 0) return; if (dp[s][c][k]! =-1) return; Dp[s][1][0] = Price[s]; Dp[s][0][0] = Price[s]; for (iNT P = head[s]; P! =-1; p = edge[p].next) {if (edge[p].v! = Pre) {if (c = = 1) dfs (EDGE[P].V,S,1,K-1); DFS (EDGE[P].V,S,0,K-2); }} for (int p = head[s]; p! =-1; p = edge[p].next) {if (edge[p].v! = Pre) {if (c = = 1) {for (int i = k;i >= 0;-i) {if (dp[s][1][i]! =-1) {for (int j = k-i-2;j >= 0;--j) if (dp[edge[p].v][0][j]! = -1) dp[s][1][i+j+2] = max (Dp[s][1][i+j+2],dp[s][1][i] + dp[edge[p].v][0][j]); } if (Dp[s][0][i]! =-1) {for (int j = k-i-1;j >= 0;-- j) if (dp[edge[p].v][1][j]! =-1) dp[s][1][i+j+1] = max (dp[s][1][i+j+1],d P[s][0][i] + dp[edge[p].v][1][j]); } } } for (int i = k;i >= 0;-i) {if (dp[s][0][i]! =-1) { for (int j = k-i-2;j >= 0;--j) if (dp[edge[p].v][0][j]! =-1) dp[s] [0] [I+j+2] = max (Dp[s][0][i+j+2],dp[s][0][i] + dp[edge[p].v][0][j]); }}}} if (c = = 1) {for (int i = 0;i <= k; ++i) dp[s][c][i] = max (Dp[s][c] [i], (LL) 0); } for (int i = 0;i <= k; ++i) dp[s][0][i] = max (Dp[s][0][i], (LL) 0);} int main () {//freopen ("Ts.txt", "w", stdout); int n,k,i,u,v; while (scanf ("%d%d", &n,&k)! = EOF) {memset (dp,-1,sizeof (DP)); Init_head_top (n); for (i = 1; I <= n; ++i) scanf ("%lld", &price[i]); for (i = 1; i < n; ++i) {scanf ("%d%d", &u,&v); Link (U,V); Link (V,u); } dfs (1,-1,1,K); LL Max = 0;//int ww = k;//for (i = 1;i <= n; ++i)//{//for (int j = 0;j <= 1; ++j)//{//for (k = 0; K <= ww; ++K)//{//if (Dp[i][j][k] > 0)//printf ("i =%d J =%d K =%d DP = %lld\n ", i,j,k,dp[i][j][k]);//}//}//}////k = ww; for (i = 0; I <= k; ++i) {max = max (max,dp[1][1][i]); } printf ("%lld\n", Max); } return 0;}