Ural 1018 Binary Apple Tree

Source: Internet
Author: User
Tags integer numbers

1018. Binary Apple Treetime limit:1.0 Second
Memory limit:64 Mblet ' s Imagine how apple tree looks in binary computer world. You're right, it's looks just like a binary tree, i.e. any biparous branch splits up to exactly, and new branches. We'll enumerate by integers the root of the binary Apple tree, points of branching and the ends of twigs. This is the distinguish different branches by their ending points. We'll assume that root of the tree always be numbered by 1 and all numbers used for enumerating be numbered in range from 1 To N, where NIs the total number of all enumerated points. For instance in the picture below Nis equal to 5. Here's an example of a enumerated tree with four branches:
2   5 \/   3   4   \/    1
As may know it's not convenient to pick an apples from a tree when there is too much of branches. That's why some of them should is removed from a tree. Interested in removing branches in the the minimal loss of apples. So your is given amounts of apples on a branches and amount of branches the should be preserved. Your task is to determine what many apples can remain on a tree after removing of excessive branches. Inputfirst line of input contains, numbers: Nand Q(2≤ N≤100; 1≤ QN? 1). NDenotes the number of enumerated points in a tree. QDenotes amount of branches that should be preserved. Next N? 1 lines contains descriptions of branches. Each description consists of a three an integer numbers divided by spaces. The first of them define branch by it ' s ending points. The third number defines the number of apples on this branch. You may assume this no branch contains more than 30000 apples. Outputoutput should contain the only number-amount of apples, can be preserved. And don ' t forget to preserve tree ' s root;--) Sample
input Output
5 21 3 11 4 102 3 203 5 20
21st

Just started to learn the tree-like DP, welcome to spray.

There is an apple tree, its branches conform to the complete binary tree. There are some apples on each branch. The node designator is 1~n (1 is the root node). Now because there are too many branches, you need to keep the Q root branch. Ask how many apples you can keep.

The data given is not in accordance with the node to the sub-node of the order given, how to build it has been bothering me for a long time (war five slag-. -)。 Finally, the use of the structure to store two sons of the subscript more beautifully built out.

Next, transfer. Obviously we not only have to consider the node, but also consider how many edges are selected for the tree with the node as its root.

DP[T][K]: The subtree that has t for the root retains the maximum number of apples that the K-Edge (branch) can hold.

From this we can see that if the K-bar is retained in a node, there are two cases.

1: Select only one subtrees tree. Then dp[t][k] = max (Dp[t.l][k-1] + W[T][T.L], dp[t.r][k-1] + W[T][T.R])//means selecting the K-1 bar in the subtree (left or right) and adding the root node to the edge of the subtree.

2: Select two sub-trees. Then there are two fixed edges (the root node to its left and right side of the child), dp[t][k] = W[t][t.l] +W[T][T.R] + max (Dp[t.l][j] + dp[t.r][k-j-2])/* (j:0 ~ K-2) */.

That is, the Saozi right subtree selects a total of k-2 edges.

Thus, the output DP[1][Q] is good.

#include <vector> #include <cstring> #include <iostream> #include <algorithm>using namespace std;struct Node {int L, r;}     T[105];int N, Q;int dp[105][105], map[105][105];void buildtree (int t) {//achievement int flag = 0; for (int i=0; i<=n; i++) {if (Map[t][i] &&! T[I].L &&! T[T].R) {//! T[I].L is not the parent node if (!            T[T].L) t[t].l = i;            else T[T].R = i;        flag = 1;        }} if (flag) {buildtree (T[T].L);    Buildtree (T[T].R);    }}void TDP (int s) {if (t[s].l = = 0) return;    TDP (T[S].L);    TDP (T[S].R); for (int i=1; i<=q; i++) {//transfer dp[s][i] = max (Dp[t[s].l][i-1] + MAP[S][T[S].L], dp[t[s].r][i-1] + MAP[S][T[S].R]        ); for (int j=0; j<i-1; J + +) {Dp[s][i] = max (Dp[s][i], dp[t[s].l][j] + dp[t[s].r][i-j-2] + map[s][t[s].l] + ma        P[S][T[S].R]);    }}}int Main () {cin >> n >> q;    int A, b, C; for (int i=1; i<n; i++) {cin >> a>> b >> C;    MAP[A][B] = map[b][a] = C;    } buildtree (1);    TDP (1);    cout << Dp[1][q] <<endl; return 0;}


Ural 1018 Binary Apple Tree

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.