Topic Links:
https://cn.vjudge.net/problem/URAL-1018
Main topic:
Give you a tree, each side has a side right, to 1 as the root node, Q bar side of the number of children (q+1 points), edge weights and maximum.
Problem Solving Ideas:
DP[ROOT][J], which represents the root node, preserving the maximum edge and the J nodes.
Dp[root][j]=max (Dp[root][j],dp[root][j-t]+dp[son][t]+len);
T ranges from 1 to j-1, since each point is updated from dp[][1]
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -+Ten;4typedefLong Longll;5 structnode6 {7 intV, W;8 node () {}9NodeintVintW): V (v), W (w) {}Ten }; OneVector<node>MAP[MAXN]; A intNUM[MAXN];//Num[i] Indicates the number of points in a subtree with the I node as root - intDP[MAXN][MAXN];//Dp[i][j] indicates that only J-edge maximum weights are in subtrees with the I node as root - voidDfsintRootintFA) the { -Num[root] =1; - for(inti =0; I < map[root].size (); i++) - { + intv = map[root][i].v, w =MAP[ROOT][I].W; - if(v = = FA)Continue;//No backtracking +Dfs (v, root);//Update your son's information first. ANum[root] + = Num[v];//current number of nodes in root subtree at for(intj = Num[root]; J >=1; j--)//update the DP of the parent node - { - for(intK =1; K < J && K <= Num[v]; k++)//K cannot be equal to j,k=j when the number of root points is 0 -DP[ROOT][J] = max (Dp[root][j], dp[root][j-k] + dp[v][k] +W); - } - } in } - intMain () to { + intN, K; - while(SCANF ("%d%d", &n, &k)! =EOF) the { *Memset (DP,0,sizeof(DP)); $ for(inti =1; I < n; i++)Panax Notoginseng { - intu, V, W; thescanf"%d%d%d", &u, &v, &W); + map[u].push_back (Node (v, W)); A Map[v].push_back (Node (u, W)); the } +Dfs1, -1); -cout<<dp[1][k +1]<<endl;//contains K-bars, or k+1 points $ } $ return 0; -}
URAL-1018 Binary Apple Tree---treeview dp