Luogu P2015 Two-prong apple tree (tree-shaped DP)

Source: Internet
Author: User

Topic title Description

There is an apple tree, if the branch has a fork, it must be forked (that is, 2 there is no 1 sons of the knot)

This tree N has a node (leaf point or Branch fork Point), numbered 1-N , tree root number must be 1 .

We describe the position of a branch with the number of nodes connected at each end of a branch. Here's a tree with a 4 branch.

2   5 \ /   3   4   \ /    1

Now there are too many branches to prune. But some branches have apples on them.

Given the number of branches that need to be retained, find out how many apples you can keep.

Input output Format input format:

1 2 number of lines, N and Q ( 1<=Q<=N , 1<N<=100 ).

NRepresents the number of nodes in the tree that Q represents the amount of branches to retain. N-1the next line describes the information of the branch.

Each line 3 is an integer, and the first two are the number of nodes to which it is connected. The 3 number of apples on this branch.

The apples on each branch are not more than 30000 one.

Output format:

A number, the maximum number of apples that can be retained.

Input and Output Sample input example:
5 21 3 11 4 102 3 203 5 20
Sample output:
21
Exercises

is also a tree DP classic entry, I use a memory search here

dp[i][j]Represents i j The maximum value left by the remaining bars in the node.

Code
#include <bits/stdc++.h>using namespace std;struct node {int value, left, right, father;} Tree[110];int edge[110][ 110];int connected[110][3];int dp[110][110];int N, q;inline void Build (const int &now) {tree[now].value = edge[tree[  Now].father][now];      for (register int i (0); i < 3; ++i) {if (Connected[now][i] && connected[now][i]! = tree[now].father) {        if (!tree[now].left) {tree[now].left = Connected[now][i];      Tree[tree[now].left].father = Now;        } else {tree[now].right = connected[now][i];      Tree[tree[now].right].father = Now;    } Build (Connected[now][i]);  }}}inline int Dfs (const int &now, const int &leave) {if (!leave | |!tree[now].left) return 0;  if (Dp[now][leave]) return dp[now][leave];                        Dp[now][leave] = max (Dfs (Tree[now].left, leave-1) + Tree[tree[now].left].value, Dfs (Tree[now].right, leave-1) + Tree[tree[now].right].value); for (register int i (0); i < leave-1; ++i) {Dp[now][leave] = max (Dp[now][leave], Dfs (tree[ Now].left, i) + Tree[tree[now].left].value + Dfs (tree[now].right, leave  -2-i) + Tree[tree[now].right].value); } return Dp[now][leave];}  int main (int argc, char **argv) {scanf ("%d%d", &n, &q);    for (register int i (1), U, V, W; i < n; ++i) {scanf ("%d%d%d", &u, &v, &w);    EDGE[U][V] = Edge[v][u] = w; Connected[u][connected[u][0]?    (Connected[u][1]? 2:1): 0] = v; Connected[v][connected[v][0]?  (Connected[v][1]? 2:1): 0] = u;  } Build (1); printf ("%d\n", Dfs (1, q));}

Luogu P2015 Two-fork apple tree (tree-shaped DP)

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.