URAL 1018 Binary Apple Tree(樹形dp入門題)

畫船聽雨發表於2014-04-26

URAL上簡單的樹形dp,但是都錯了題意。。。sad,是保留m個樹幹,不是刪掉m個樹幹。求保留的最多的蘋果有多少。根據樹的特點,每個dp[i][j]表示第i個節點保留了j個樹幹。dp[i][j] = max(dp[i][j], dp[x1][k-1]+map[x1][i] + dp[x2][j-k-1] + map1[x2][i]).就是多的這個節點是屬於哪個子樹上的。

1018. Binary Apple Tree

Time limit: 1.0 second
Memory limit: 64 MB
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.

Input

First line of input contains two numbers: N and Q (2 ≤ N ≤ 100;1 ≤ QN − 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should 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.

Output

Output should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)

Sample

input output
5 2
1 3 1
1 4 10
2 3 20
3 5 20
21
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 110;
using namespace std;

vector<int>g[maxn];
vector<int>now[maxn];
int dp[maxn][maxn];
int vis[maxn];
int map1[maxn][maxn];
int n, m;

void change(int x)
{
    vis[x] = 1;
    for(int i = 0; i < g[x].size(); i++)
    {
        if(vis[g[x][i]])
            continue;
        now[x].push_back(g[x][i]);
        change(g[x][i]);
    }
}

void dfs(int x)
{
    dp[x][0] = 0;
    for(int i = 0; i < now[x].size(); i++)
        dfs(now[x][i]);
    if(now[x].size() == 0)
        return;
    int ll = now[x][0];
    int rr = now[x][1];
    for(int i = 1; i <= m; i++)
    {
        for(int j = 0; j <= i; j++)
        {
            int xx = 0;
            if(j != 0)
                xx += (dp[ll][j-1]+map1[x][ll]);
            if(i-j != 0)
                xx += (dp[rr][i-j-1]+map1[x][rr]);
            dp[x][i] = max(dp[x][i],xx);
        }
    }
}

int main()
{
    while(cin >>n>>m)
    {
        for(int i = 0; i <= n; i++)
        {
            now[i].clear();
            g[i].clear();
            map1[i][i] = 0;
        }
        memset(dp, 0 , sizeof(dp));
        memset(vis, 0 , sizeof(vis));
        int u, v, w;
        for(int i = 0; i < n-1; i++)
        {
            cin >>u>>v>>w;
            g[u].push_back(v);
            g[v].push_back(u);
            map1[u][v] = w;
            map1[v][u] = w;
        }
        change(1);
        dfs(1);
        cout<<dp[1][m]<<endl;
    }
}


相關文章