1 #include<stdio.h> 2 #include<string.h> 3 int max(int a,int b) 4 { 5 if (a>b) return a; 6 return b; 7 } 8 int m; 9 int dp[150][150],son[150],bug[150],pos[150],f[150][150],hash[150]; 10 void dfs(int node) 11 { 12 int i,j,k; 13 if (hash[node]) return; 14 int need=(bug[node]+19)/20; 15 for (i=need;i<=m;i++) dp[node][i]=pos[node]; 16 for (i=1;i<=son[node];i++) 17 { 18 int t=f[node][i]; dfs(t); 19 for (j=m;j>=need;j--) 20 for (k=1;k<=j-need;k++) 21 dp[node][j]=max(dp[node][j],dp[node][j-k]+dp[t][k]); 22 } 23 hash[node]=1; 24 return; 25 } 26 int main() 27 { 28 int n,i,x,y,t; 29 while (~scanf("%d%d",&n,&m)&&(n!=-1&&m!=-1)) 30 { 31 for (i=1;i<=n;i++) scanf("%d%d",&bug[i],&pos[i]); 32 memset(son,0,sizeof(son)); 33 memset(hash,0,sizeof(hash)); 34 memset(dp,0,sizeof(dp)); 35 for (i=1;i<n;i++) 36 { 37 scanf("%d%d",&x,&y); 38 if (x>y) {t=x; x=y; y=t;} 39 son[x]++; 40 f[x][son[x]]=y; 41 } 42 if (m==0) printf("0\n"); 43 else 44 { 45 dfs(1); 46 printf("%d\n",dp[1][m]); 47 } 48 } 49 return 0; 50 } 51
http://acm.hdu.edu.cn/showproblem.php?pid=2412