Ural 1018 Binary Apple Tree

Source: Internet
Author: User
Tags integer numbers

Ural 1018 Binary Apple Tree
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 looks just like a binary tree, I. e. any biparous branch splits up to exactly two new branches. we will enumerate by integers the root of binary apple tree, points of branching and the ends of twigs. this way we may distinguish different branches by their ending points. we will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1N, WhereNIs the total number of all enumerated points. For instance in the picture belowNIs equal to 5. Here is an example of an enumerated tree with four branches:

2   5 \ /   3   4   \ /    1
As you may know it's not convenient to pick an apples from a tree when there are too much of branches. that's why some of them shocould be removed from a tree. but you are interested in removing branches in the way of minimal loss of apples. so your are given amounts of apples on a branches and amount of branches that shoshould be preserved. your task is to determine how many apples can remain on a tree after removing of excessive branches. inputFirst line of input contains two numbers: NAnd Q(2 ≤ N≤ 100; 1 ≤ QN? 1 ). NDenotes the number of enumerated points in a tree. QDenotes amount of branches that shoshould be preserved. Next N? 1 lines contains descriptions of branches. each description consists of a three integer numbers divided by spaces. the first two of them define branch by it's ending points. the third number defines the number of apples on this branch. you may assume that no branch contains more than 30000 apples. outputOutput shoshould contain the only number-amount of apples that 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
21

At the beginning, I learned about tree-like dp. Welcome to try it.

There is an apple tree, and its branches are completely Binary Trees. Each branch has some apples. Node number: 1 ~ N (1 is the root node ). Now, because there are too many branches, we need to keep q Root Branches. Ask how many apples can be retained.

The data given in the question is not given in the order from the following node to the sub-node, and how to build it has been bothering me for a long time (War 5 scum -. -). Finally, the subscript of the two sons stored by the struct is beautifully created.

Next we will talk about transfer. Obviously, we should not only consider the node, but also consider the number of edges selected for the tree with the node as the root.

Dp [t] [k]: the maximum number of apples that can be retained by a subtree with t as the root.

From this we can find that if k edges are retained on a node, there are two situations.

1: select only one subtree. So dp [t] [k] = max (dp [t. [k-1] + w [t] [t. l], dp [t. [k-1] + w [t] [t. r]) // indicates selecting an edge of the K-1 on the subtree (left or right) and adding the root node to the edge of the subtree.

2: select two subtree. Then there are two fixed edges (the root node to the left and right sides 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 left subtree and the right subtree select a total of K-2 edges.

Therefore, output dp [1] [q.

# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; struct Node {int l, r;} T [105]; int n, q; int dp [105] [105], map [105] [105]; void buildTree (int t) {// build int flag = 0; for (int I = 0; I <= n; I ++) {if (map [t] [I] &! T [I]. l &&! T [t]. r ){//! T [I]. l 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]. [I-1] + map [s] [T [s]. l], dp [T [s]. [I-1] + map [s] [T [s]. r]); for (int j = 0; j
    
     
> N> q; int a, B, c; for (int I = 1; I
     
      
> A> B> c; map [a] [B] = map [B] [a] = c;} buildTree (1); Tdp (1 ); cout <dp [1] [q] <
      
       

Related Article

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.