标签:树形dp
3 2 0 1 0 2 0 3 7 4 2 2 0 1 0 4 2 1 7 1 7 6 2 2 0 0
5 13
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #define ll long long using namespace std; const int maxn=205; ll dp[maxn][maxn]; int visi[maxn]; vector <int> mq[maxn]; int n,m; void dfs(int cur) { visi[cur]=1; for(int i=0;i<mq[cur].size();i++) { int p=mq[cur][i]; if(!visi[p]) dfs(p); for(int j=m+1;j>=1;j--) for(int k=1;k<j;k++) { if(dp[cur][k]!=-1&&dp[p][j-k]!=-1) dp[cur][j]=max(dp[cur][j],dp[cur][k]+dp[p][j-k]); } } } int main() { int a,i,j; ll b; while(~scanf("%d%d",&n,&m)&&(m+n)) { for(i=0;i<=n;i++) mq[i].clear(); memset(visi,0,sizeof(visi)); memset(dp,-1,sizeof(dp)); /*for(i=0;i<=n;i++) for(j=1;j<=m+1;j++) dp[i][j]=-1;*/ for(i=1;i<=n;i++) { scanf("%d%I64d",&a,&b); mq[a].push_back(i); dp[i][1]=b; } dp[0][1]=0; dfs(0); printf("%I64d\n",dp[0][m+1]); } return 0; }
hdu 1561The more, The Better(树形dp&01背包),布布扣,bubuko.com
hdu 1561The more, The Better(树形dp&01背包)
标签:树形dp
原文地址:http://blog.csdn.net/coraline_m/article/details/25385941