1. 程式人生 > >(樹形dp)B - Binary Apple Tree

(樹形dp)B - Binary Apple Tree

Let’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 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
\ /
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 should 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 should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.

題意是形如二叉樹的蘋果樹,有n個結點,要求我們選出q個樹杈使蘋果的總數儘可能多
選出q個樹杈的dp方程有點難想,轉換為選出q+1個結點,可以用dp[i][j]表示第i個結點保留j個結點的最大值,則dp[i][j] = max(dp[i的左兒子][k] + dp[i的右兒子][j-k-1]) + w;
題目可能給的不是正規的二叉樹,先dfs生成二叉樹

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 100 + 4;
int dp[maxn][maxn];
vector< pair<int, int> > E[maxn];
struct node {
    int l, r, w;
    node() {}
    node(int _l, int _r, int _w) : l(_l), r(_r), w(_w){}
} od[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    while(cin >> n >> q) {
        for(int i = 1; i <= n; i++) E[i].clear();
        memset(dp, 0, sizeof(dp));
        int u, v, w;
        for(int i = 1; i < n; i++) {
            cin >> u >> v >> w;
            E[u].emplace_back(make_pair(v, w));
            E[v].emplace_back(make_pair(u, w));
        }
        function<void(int, int)> dfs = [&](int u, int pre) {
            if(E[u].size() == 1 && E[u][0].first == pre) {
                od[u].l = od[u].r = 0;
                return;
            }
            for(auto& x : E[u]) {
                int v = x.first, w = x.second;
                if(v == pre) continue;
                if(od[u].l) od[u].r = v;
                else od[u].l = v;
                od[v].w = w;
                dfs(v, u);
            }
        };
        dfs(1, 0);
        function<int(int, int, int)> tree_dp = [&](int u, int pre, int sz) {
            if(sz <= 0) return 0;
            if(dp[u][sz]) return dp[u][sz];
            if(od[u].l == 0 && od[u].r == 0) return od[u].w;
            int mx = 0;
            for(int i = 0; i < sz; i++) {
                int l = tree_dp(od[u].l, u, i);
                int r = tree_dp(od[u].r, u, sz - i - 1);
                mx = max(mx, l + r);
            }
            return dp[u][sz] = mx + od[u].w;
        };
        tree_dp(1, 0, q + 1);
        cout << dp[1][q + 1] << endl;
    }
}