标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7031 Accepted Submission(s):
4121
#include<iostream> #include<cstring> #include<cstdio> #include<vector> using namespace std; #define N 205 int m,n; //int dp[N][N]; int tree[N][N],value[N]; vector<int> v[N]; void tree_dp(int root,int rest) { for(int i=1;i<=rest;i++) tree[root][i]=value[root]; for(int i=0;i<v[root].size();i++) { int u=v[root][i]; tree_dp(u,rest-1); for(int j=rest;j>=1;j--) for(int k=1;k<=j-1;k++) { tree[root][j]=max(tree[root][j],tree[root][j-k]+tree[u][k]); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF&&n&&m) { m++; memset(tree,0,sizeof(tree)); value[0]=0; for(int i=0;i<=n;i++) v[i].clear(); for(int i=1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(i); value[i]=b; } tree_dp(0,m); printf("%d\n",tree[0][m]); } return 0; }
HDU_1561_The more, The Better_树型dp
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5447599.html