Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5822 Accepted Submission(s): 3469
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> using namespace std; vector<int> e[205]; int n,m,dp[205][205],w[205]; void dfs(int x,int sum) { dp[x][1]=w[x]; for(int i=0;i<e[x].size();i++) { if(sum>1) dfs(e[x][i],sum-1); for(int j=sum-1;j>=1;j--) { int v=j+1; for(int k=1;k<v;k++) { dp[x][v]=max(dp[x][v],dp[x][v-k]+dp[e[x][i]][k]); } } } } int main() { int x; while(scanf("%d%d",&n,&m)!=EOF) { if(n+m==0) break; memset(dp,0,sizeof(dp)); for(int i=0;i<=n;i++) e[i].clear(); for(int i=1;i<=n;i++) { scanf("%d%d",&x,&w[i]); e[x].push_back(i); } dfs(0,m+1); printf("%d\n",dp[0][m+1]); } return 0; }
原文:http://www.cnblogs.com/water-full/p/4461582.html