Tree-based dynamic programming

Source: Internet
Author: User

Problem description

Many times, the DP (Dynamic programming) We do is often based on very simple data structures, such as one-dimensional arrays, two-dimensional arrays, or even higher-dimensional arrays. Today hihocoder , when the brush, encountered a problem, very interesting, is the DP on the tree.

Topic
1055: Brush Paint

Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe

On the last mention, Little Ho has a gray often fun tree toys! This tree toy is made up of n balls and N-1 sticks, all of which are numbered in small ho, and these numbers are at 1. Within the range of N, each stick is connected to two different balls, and there are no more than two different paths between any two spheres that can reach each other. Yes, this is the story of this tree toy!

The quality of the small Ho's tree toy doesn't seem to be very good, just a few weeks of playing, then paint off!

"It was a nightmare!" Little Ho held a tree-toy eye with a tearful way.

"What's so sad about it, do you have to buy some paint brushes?" "Little Hi says it can't be understood."

"Can you do that?" Small Ho immediately elated up, immediately ran out to buy back paint, but little ho body money is not enough-so he only bought a limited amount of paint, these paint can give m a knot color, which means that little ho can not be his beloved tree toys in every knot painted on!

Small Ho bowed his head for a long time-he did not want to select only part of the knot to paint, nor to find small hi to borrow money, but soon, He came up with a great idea: to paint a portion of the junction that contains the number 1th node (the connection here is that some of the painted nodes can reach each other and do not pass through the unpainted nodes), and then remove the remaining nodes!

So which nodes do you choose to paint? Little Ho thought about trying to score points for every knot--he wanted to stay at the end, which is the grading of those painted knots that could be as high as possible!

So, how does little ho do it?

Tip One: Dynamic planning on the tree? In fact, the old contact already!

Input

Each test point (input file) has and has only one set of test data.
The first behavior of each set of test data is two integers n, M, meaning as described earlier.
Second behavior for each set of test data n integers, where the I integer VI represents the score of the node labeled I
Each group of test data of the line 3~n+1, each line describes a stick, wherein the I+1 Act Two integer Ai,bi, which represents the number of two balls connected by the root of the I stick.
For 100% of data, meet N<=10^2,1<=ai<=n, 1<=bi<=n, 1<=vi<=10^3, 1<=m<=n
Little hi Tip: Those with array storage tree edge remember to open twice times the size Oh!

Output

For each set of test data, the output of an integer ans indicates the maximum possible number of points that can be scored for the paint junction.
Sample input
10 4
370 328 750 930 604 732 159 167 945 210
1 2
2 3
1 4
1 5
4 6
4 7
4 8
6 9
5 10
Sample output
2977

Title Link: http://hihocoder.com/problemset/problem/1055

Exercises
#include <iostream>#include <vector>#include <cstdio>#include <cstdlib>#include <vector>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace STD;classSolution { Public: vector<int>*adj;Const Static intMax_n = -+Ten;intN, M, V[max_n];intF[max_n][max_n];voidSolve () {adj =New  vector<int>[Max_n];Cin>> N >> M; for(inti =0; i < N; i++)Cin>> V[i]; for(inti =0; I < n1; i++) {intA, B;Cin>> a >> b; adj[a-1].push_back (b-1); } COMPUTE (0, -1);cout<< f[0][M] << Endl; }voidComputeintRtintHIGHER_RT) {//RT is the root ID         for(inti = adj[rt].size ()-1; i>=0;        i--) {Compute (ADJ[RT][I], RT);    } compute_f (RT, HIGHER_RT); }voidCompute_f (intTintRoot) {//Compute f (t, 1..M), here T belongs to [0..n-1]        //root denotes the root ID, if id = =-1, then T is tree root,        //Otherwise t have a root in the tree         for(inti =0; I <= M; i++) F[t][i] =0; f[t][1] = V[t]; for(intAdj_idx =0; Adj_idx < Adj[t].size (); adj_idx++) {if(Root = = Adj[t][adj_idx])Continue; for(intm = m; M >=2; m--) { for(intM_child =1; M_child < M; m_child++) {intT_child = Adj[t][adj_idx];                F[T][M] = max (F[t][m], F[t_child][m_child] + f[t][m-m_child]); }            }        }    }};intMain () {solution solution; Solution.solve ();return 0;}

Tree-based dynamic programming

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.