标签:mis one cbo problem pen algorithm put ret bottom
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9938 Accepted Submission(s): 5788
#include<cstdio> #include<algorithm> #include<cstring> #define lol long long #define maxn 200 using namespace std; int n,m; int to[maxn+5]; int nex[maxn+5]; int head[maxn+5]; int cnt; void addedge(int u,int v) { to[cnt]=v; nex[cnt]=head[u]; head[u]=cnt++; } int val[maxn+5]; int dp[maxn+5][maxn+5]; void dfs(int x) { for(int i=1;i<=m;i++) dp[x][i]=val[x]; for(int i=head[x];i!=-1;i=nex[i]) { dfs(to[i]); //分组背包 for(int j=m;j>1;j--)//--保证每组只用一个 for(int k=1;k<j;k++) dp[x][j]=max(dp[x][j],dp[x][j-k]+dp[to[i]][k]); } } int main() { while(scanf("%d%d",&n,&m),n||m) { memset(head,-1,sizeof(head)); memset(val,0,sizeof(head)); cnt=0; for(int i=1,a,b;i<=n;i++) { scanf("%d%d",&a,&b); addedge(a,i); val[i]=b; } m++; memset(dp,0,sizeof(dp)); dfs(0); printf("%d\n",dp[0][m]); } return 0; }
hdu1561 The more, The Better (树形DP)
标签:mis one cbo problem pen algorithm put ret bottom
原文地址:https://www.cnblogs.com/acboyty/p/9826732.html