HDU 1011 Tree-shaped backpack

Source: Internet
Author: User

Obviously a tree-shaped backpack, note the case of M 0.

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =101;7 intHead[n];8 intBn[n];9 intValue[n];Ten intDp[n][n]; One intN, M, E; A  - structEdge - { the     intV, next; -} edge[n <<1]; -  - voidAddedge (intUintv) + { -EDGE[E].V =v; +Edge[e].next =Head[u]; AHead[u] = e++; at } -  - voidDfsintUintFA) - { -     intTMP = (Bn[u] + +) / -; -      for(inti = tmp; I <= m; i++) Dp[u][i] =Value[u]; in      for(inti = Head[u]; I! =-1; i =edge[i].next) -     { to         intv =edge[i].v; +         if(v = = FA)Continue; - Dfs (v, u); the          for(intj = m; J >= tmp; j-- ) *         { $              for(intK =1; J-k >= tmp; k++ )Panax Notoginseng             { -DP[U][J] = max (Dp[u][j], dp[u][j-k] +dp[v][k]); the             } +         } A     } the } +  - intMain () $ { $      while(SCANF ("%d%d", &n, &m)! =EOF) -     { -         if(n = =-1&& m = =-1) Break; the          for(inti =1; I <= N; i++ ) -         {Wuyiscanf"%d%d", BN + I, value +i); the         } -E =0; WuMemset (Head,-1,sizeof(head)); -          for(inti =1; I < n; i++ ) About         { $             intu, v; -scanf"%d%d", &u, &v); - Addedge (U, v); - Addedge (V, u); A         } +         if( !m) the         { -Puts"0"); $             Continue; the         } theMemset (DP,0,sizeof(DP)); theDfs1, -1 ); theprintf"%d\n", dp[1][m]); -     } in     return 0; the}

HDU 1011 Tree-shaped backpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.