URAL 1018 (Golden Tree DP)

Source: Internet
Author: User
Tags integer numbers

Connection: 1018. binary Apple TreeTime limit: 1.0 secondMemory 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 e Nding points. we will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to N, where N is the total number of all enumerated points. for instance in the picture below N is equal to 5. here is an example of an enumerated tree with four branches: 2 5 \/3 4 \/1As you may know it's not convenient to pick an apples from a tree when ther E 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 tw O numbers: N and Q (2 ≤ N ≤ 100; 1 ≤ Q ≤ N −1 ). N denotes the number of enumerated points in a tree. Q denotes amount of branches that shoshould be preserved. nextN −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;-) Sampleinput output5 21 3 11 4 102 3 203 5 2021 meaning: There is an apple tree, the apple tree is a binary tree, there are N nodes in total, and the number of Tree nodes is 1 ~ N, the node numbered 1 is the root of the tree, and the edge can be understood as the branch of the tree. Each branch has several apples. Now we need to subtract several branches and keep M branches, the maximum number of apples in the M branch is required. Binary apple tree: A Golden Tree DP. This is special. Because it is a binary tree, you only need to deal with the left two sons and the right son, but I still want to use the general tree-like DP to do this, that is, it is not used as a binary tree. Idea: similar to the idea of a 0-1 backpack, select j edges in the subtree of the root node v of the u son and add them to the u. Dp [u] [k] = max (dp [u] [k], dp [u] [k-j] + dp [v] [J-1] + w) (1 <j <= k), w: the value of the edge between u and v. If an edge is selected in the v subtree, the edge between u and v is required.

# Include <stdio. h> # include <string. h> const int N = 110; int dp [N] [N], vis [N], head [N], num, m; struct edge {int st, ed, w, next;} e [N * 4]; void addedge (int x, int y, int w) {e [num]. st = x; e [num]. ed = y; e [num]. w = w; e [num]. next = head [x]; head [x] = num ++; e [num]. st = y; e [num]. ed = x; e [num]. w = w; e [num]. next = head [y]; head [y] = num ++;} void dfs (int u) {vis [u] = 1; int I, v, w, j, k, son = 0; for (I = head [u]; I! =-1; I = e [I]. next) {v = e [I]. ed; w = e [I]. w; if (vis [v] = 1) continue; dfs (v); for (k = m; k> = 1; k --) // 0-1 backpack {for (j = 1; j <= k; j ++) // select j edge in the subtree of the v node if (dp [u] [k] <dp [u] [k-j] + dp [v] [J-1] + w) // u and v have an edge, so add dp [v] [J-1], dp [u] [k] = dp [u] [k-j] + dp [v] [J-1] + w ;}} int main () {int I, x, y, w, n; while (scanf ("% d", & n, & m )! =-1) {memset (head,-1, sizeof (head); num = 0; for (I = 1; I <n; I ++) {scanf ("% d", & x, & y, & w); addedge (x, y, w);} memset (vis, 0, sizeof (vis); memset (dp, 0, sizeof (dp); dfs (1); printf ("% d \ n ", dp [1] [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.