Ural 1018 Binary Apple Tree (Tree dp | classic)

Source: Internet
Author: User

Url-1018 to give a edge of the value of the Binary Tree, node number is 1 ~ N, 1 is the root node. Cut down some edges and keep only q edges. The root node of the subtree composed of q edges must be 1. What is the maximum weight of this subtree? The idea is a classic tree dp question. According to the questions I have done so far, many of them are derived from this question. F (I, j) indicates subtree I, and retains the maximum weight of j nodes (note that it is a node. The weight of each edge, which is considered as the weight of the son node of the connected two nodes. Then, we can group all the I subtree, that is, each subtree can choose 1, 2,... J-1 side allocated to it. Status transfer: f (I, j) = max {f (I, j-k) + f (v, k) | 1 <= k <j} | v is the son of I} ans = f (1, q + 1) Code

/** =================================================== =================== * This is a solution for ACM/ICPC problem ** @ source: ural-1018 Binary Apple Tree * @ description: Tree dp * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * Copyright (C) 2013/09/01 All rights reserved. * ===================================================== ===================*/# include <iostream >#include <cstdio> # Include <algorithm> # include <vector> # include <cstring> # define MP make_pairusing namespace std; typedef pair <int, int> PII; typedef long int64; const int INF = 0x3f3f3f; const int MAXN = 110; vector <PII> adj [MAXN]; int n, q; int tot [MAXN]; int f [MAXN] [MAXN]; int dfs (int u, int fa) {tot [u] = 1; for (int e = 0; e <adj [u]. size (); ++ e) {int v = adj [u] [e]. first; if (v = fa) continue; tot [u] + = dfs (v, U) ;}for (int e = 0; e <adj [u]. size (); ++ e) {int v = adj [u] [e]. first; int w = adj [u] [e]. second; if (v = fa) continue; for (int I = tot [u]; I> 0; -- I) {for (int j = 1; j <I & j <= tot [v]; ++ j) {f [u] [I] = max (f [u] [I], f [u] [I-j] + f [v] [j] + w) ;}}return tot [u] ;}int main () {while (~ Scanf ("% d", & n, & q) {for (int I = 0; I <= n; ++ I) adj [I]. clear (); for (int I = 0; I <n-1; ++ I) {int u, v, w; scanf ("% d ", & u, & v, & w); adj [u]. push_back (MP (v, w); adj [v]. push_back (MP (u, w);} memset (f, 0, sizeof (f); dfs (1,-1); printf ("% d \ n ", f [1] [q + 1]);} return 0 ;}

 


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.