timus 1018. Binary Apple Tree

Posted いいえ敗者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了timus 1018. Binary Apple Tree相关的知识,希望对你有一定的参考价值。

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 ≤ Q ≤ N − 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should 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.

Output

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

Sample

inputoutput
5 2
1 3 1
1 4 10
2 3 20
3 5 20
21

 

题目大意:n个点 n-1条边,现在要保留Q条边,求保留下的边的去权值和的最大值。

把边的权值映射到点上,边的权值相当于这个点指向根节点的权值,所以问题转换成对点的操作。

先统计出以当前点为根节点的子树的点数(包括当前根节点),然后dp,

这里dp,以u为根节点保留j个点能得到最大值,状态转移方程

dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v]k]+val)

val是v到u的权值。

 

/* ***********************************************
Author        :guanjun
Created Time  :2016/10/15 15:43:48
File Name     :timus1018.cpp
************************************************ */
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
struct node{
    int y;
    int val;
};

vector<node>v[110];
int sz[110],n,m,num;
int dp[110][110];
void dfs(int u,int fa){
    num++;
    sz[u]=1;
    for(int i=0;i<v[u].size();i++){
        int y=v[u][i].y;
        if(y==fa)continue;
        dfs(y,u);
        sz[u]+=sz[y];
    }
}
void dfs2(int u,int fa){
    for(int i=0;i<v[u].size();i++){
        int y=v[u][i].y;
        int val=v[u][i].val;
        if(y==fa)continue;
        //cout<<u<<" "<<sz[u]<<endl;
        dfs2(y,u);
        for(int j=sz[u];j>1;j--){
            for(int k=1;k<j;k++){
                dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[y][k]+val);
            }
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    while(cin>>n>>m){
        num=0;
        int x,y,z;
        cle(sz);
        for(int i=1;i<n;i++){
            cin>>x>>y>>z;
            v[x].push_back({y,z});
            v[y].push_back({x,z});
        }
        cle(dp);
        dfs(1,-1);
        dfs2(1,-1);
        cout<<dp[1][m+1]<<endl;
    }
    return 0;
}

 

以上是关于timus 1018. Binary Apple Tree的主要内容,如果未能解决你的问题,请参考以下文章

URAL-1018 Binary Apple Tree---树形DP

Timus Online Judge 1001. Reverse Root

Leetcode 1018. Binary Prefix Divisible By 5

Binary Prefix Divisible By 5 LT1018

LeetCode --- 1018. Binary Prefix Divisible By 5 解题报告

LeetCode --- 1018. Binary Prefix Divisible By 5 解题报告