HDOJ1011 (Tree-type motion regulation)

Source: Internet
Author: User

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineMax (a>b)? a:bConst intNMax = the;intMap[nmax][nmax];intD[nmax][nmax];intVisit[nmax];structroom{intbugs, Brains;} Room[nmax];intN, M;voiddpintU//The dynamic programming here is equal to layered processing, one layer at a time. Recursive non-recursive mutual mates. {Visit[u]=1; intR = (Room[u].bugs + +) / -;  for(inti = M; I >= R; I--) D[u][i]= Room[u].brains;//Initialize   for(intv =1; v <= N; ++v) {if(Map[u][v] &&!Visit[v])      {DP (v);  for(intj = M; J >= R; J--)//It is important to note that from large to small, this avoids interference from the node itself. The d here only needs to handle the other subtrees.      {        //If you want to use recursion here, from M to R, perform update processing of U.          for(intK =1; K <= J-r; K + +) D[u][j]= Max (D[u][j], d[u][j-k] +D[v][k]); }    }  }}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) {    if(N = =-1) Break;  for(inti =1; I <= N; ++i) scanf ("%d%d", &room[i].bugs, &room[i].brains); memset (Map,0,sizeof(map));  for(inti =1; i < N; ++i) {intA, B; scanf ("%d%d", &a, &b); MAP[A][B]= Map[b][a] =1; }    if(! M//if M is zero, special judgment is required, special data{printf ("0\n"); Continue; } memset (d,0,sizeof(d)); memset (Visit,0,sizeof(visit)); DP (1); printf ("%d\n", d[1][m]); }  return 0;}

HDOJ1011 (Tree-type motion regulation)

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.