Title: Give a Tree a tree with a root, and give the capacity. Maximum weight at no more than the capacity. The prerequisite is that the parent node is selected to select child nodes.
Problem Analysis: A group backpack on a tree.
PS: The case of M is 0 o'clock in the special sentence.
The code is as follows:
# include<iostream># include<cstdio># include<vector># include<cstring># include< algorithm>using namespace Std;const int n=105;const int inf=1000000000;int n,m;int bug[n];int w[n];int Dp[N][N]; Vector<int>e[n];void init () {memset (dp,0,sizeof (DP)); for (int i=1;i<=n;++i) {scanf ("%d%d", bug+i,w+i); E[i]. Clear ();} int a,b;for (int i=1;i<n;++i) {scanf ("%d%d", &a,&b); E[a].push_back (b); E[b].push_back (a);}} int gettroopers (int x) {return (x+19)/20;} int dfs (int u,int fa) {int x=gettroopers (Bug[u]), for (int i=x;i<=m;++i) dp[u][i]=w[u];for (int i=0;i<e[u].size (); + +i) {int v=e[u][i];if (V==FA) Continue;dfs (V,u), for (int. j=m;j>=x;--j) {for (int k=1;k<=j-x;++k) {Dp[u][j]=max (dp[ U][j],dp[v][k]+dp[u][j-k]);}}} void Solve () {if (m==0) printf ("0\n"), Else{dfs (1,-1);p rintf ("%d\n", Dp[1][m]);}} int main () {while (~scanf ("%d%d", &n,&m) && (n!=-1| | M!=-1)) {init (); Solve ();} return 0;}
HDU-1011 Starship Troopers (tree-shaped dp+ Group backpack)