Ural 1018 binary apple tree (tree DP)

Source: Internet
Author: User

Link: (-_-) ZZ

There is an apple tree. The Apple Tree is a binary tree with N nodes in total. The tree node number 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.

Idea: tree-like DP, DP [I] [J] indicates the number of apples with J edges when the root node is I. There are two cases for selecting the left subtree and the right subtree.

1: All J edges are on the left or right sub-number.

2: J lines of edge have left Tree and right tree together (now to discuss the situation, how many lines on the left, how many lines on the right, the left or the right of the maximum J-2, it should be the two from the root node to the two nodes)

PS: the first tree-like DP with claws

Code:

# Include <stdio. h> # include <string. h> # define max (A, B) A> B? A: bstruct {int L, R;} tree [102]; int n = 0, m = 0, map [102] [102], used [102], DP [102] [102]; void build_tree (INT c) {int I = 0, flag = 0; for (I = 1; I <= N; I ++) {If (Map [C] [I] &! Used [I]) {If (! Tree [C]. L) tree [C]. L = I, flag = 1, used [I] = 1; else if (! Tree [C]. r) // only the left and right vertices {tree [C]. R = I, flag = 1, used [I] = 1; break ;}} if (! Flag) return; build_tree (tree [C]. l); build_tree (tree [C]. r);} void dp (INT c) {int I = 0, j = 0, L = tree [C]. l, r = tree [C]. r; If (! Tree [C]. l) return; DP (tree [C]. l); DP (tree [C]. r); DP [C] [1] = max (Map [C] [L], map [C] [r]); // select the big side on the left or right of the current node for (I = 2; I <= m; I ++) {DP [C] [I] = max (DP [l] [I-1] + map [C] [L], DP [r] [I-1] + map [C] [r]); // select the left or right subtree for (j = 0; j <I-1; j ++) // select DP [C] [I] = max (DP [C] [I] for both left and right subtree. DP [l] [J] + dp [r] [i-j-2] + map [C] [l] + map [C] [r]); // The subtree on the left or right is a maximum of I-2 edges and should have two edges from the root node to the left and right nodes} int main () {int I = 0, u = 0, V = 0, cost = 0; while (scanf ("% d ", & N, & M )! = EOF) {memset (MAP, 0, sizeof (MAP); memset (used, 0, sizeof (used); memset (tree, 0, sizeof (tree )); memset (DP, 0, sizeof (DP); used [1] = 1; for (I = 1; I <n; I ++) {scanf ("% d", & U, & V, & cost); If (Map [u] [v] <cost) map [u] [v] = map [v] [u] = cost;} build_tree (1); DP (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.