标签:
#include<cstdio> #include<cstring> #include<iostream> #define INF 999999999 using namespace std; const int MAXN = 210; struct node { int to; int v; int next; }edge[MAXN*3]; int pre[MAXN],n,index,vis[MAXN],m,num[MAXN]; int dp[MAXN][MAXN]; int max(int x,int y) { return x>y?x:y; } void add(int x,int y,int z) { edge[index].to = y; edge[index].v = z; edge[index].next = pre[x]; pre[x] = index++; } void dfs(int root) { int j,k; vis[root] = 1; for(int i=pre[root]; i!=-1; i=edge[i].next){ int t = edge[i].to; if(!vis[t]){ dfs(t); for(j=m+1; j>=1; j--){ for(k=1; k<j; k++){ dp[root][j] = max(dp[root][j], dp[root][k] + dp[t][j-k]); } } } } } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { if(!n&&!m)break; index = 1; memset(num,0,sizeof(num)); memset(pre,-1,sizeof(pre)); memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); for(i=1; i<=n; i++){ int x, y; scanf("%d%d",&x,&y); num[x] ++; add(i, x, y); add(x, i, y); for(j=1; j<=m; j++){ dp[i][j] = y; } } dfs(0); printf("%d\n",dp[0][m+1]); } }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/5023725.html