Hdu 1561 tree DP beginners

Source: Internet
Author: User

Http://www.cnblogs.com/zjh10/articles/2032294.html

This topic indicates the value of each vertex. To obtain a vertex, you must first obtain the maximum value of M vertex.

Obviously, there is a dependent backpack, and it should be dependent on it (although I don't think the question is clearly stated )~

In this case, we will build dp [I] [j], I is the point I, and j selects the points in it .....

In this way, we find that it is actually a tree, and the value of the vertex depends on the method of selecting the child vertex.

Equation dp [k] [j] = max (dp [k] [j], dp [k] [j-kk] + dp [t [k] [I] [kk])

It indicates selecting k points in the subvertex of k plus the current k Number and clicking j-kk points and directly clicking j points on k number...

We can know that dp [k] [1] = val [k], because you must select yourself as the vertex =

There can be two basic expressions for such a question. One is to determine the position of 1 first (which may be a constant optimization), and the other is to final Add the val,

If dependency is used to form a forest, the tree must be converted into a forest before the tree-like DP can be implemented.

Add a root node 0.

Dp [I] [j] indicates the optimal value for selecting j points in the I-root subtree.

The answer is dp [0] [m + 1]: A root node is added.

View Code

# Include <stdio. h>
# Include <string. h>
Int n, m;
Int num [250];
Int map [250] [250];
Int dp [250] [250];
Bool vis [250];
Int max (int a, int B ){
Return a> B? A: B;
}
Void dfs (int p)
{
Int I, j, k;
Vis [p] = true;
For (I = 1; I <= num [p]; I ++)
{
Int t = map [p] [I];
If (! Vis [t]) dfs (t );
For (j = m; j> = 2; j --) // you do not need to update the status when selecting one, because it is mandatory to be added, that is, you must first select
{
For (k = 1; k <j; k ++)
{
If (dp [t] [j-k]! =-1 & dp [p] [k]! =-1)
Dp [p] [j] = max (dp [p] [j], dp [p] [k] + dp [t] [j-k]);
}
}
}
}
Int main ()
{
Int I, j;
While (scanf ("% d", & n, & m), n | m)
{
Int a, B;
Dp [0] [1] = 0;
Memset (num, 0, sizeof (num ));
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & a, & B );
Dp [I] [1] = B;
Map [a] [++ num [a] = I;
}
M ++; // Add a vertex. The forest is converted into a tree.
For (I = 0; I <= n; I ++ ){
Dp [I] [0] = 0; vis [I] = 0;
For (j = 2; j <= m; j ++ ){
Dp [I] [j] =-1;
}
}
Dfs (0 );
Printf ("% d \ n", dp [0] [m]);
}
Return 0;
}

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.