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

input output
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;
}
时间: 2024-10-23 16:43:32

timus 1018. Binary Apple Tree的相关文章

ural 1018 Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory 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 enu

URAL 1018 Binary Apple Tree 树形DP 好题 经典

1018. Binary Apple Tree Time limit: 1.0 secondMemory 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 enu

Ural 1018 binary apple tree(显性树的树dp)

题意:一棵含n个节点的树,保留m条边,使含m条边的子树的边权和最大: 思路:树dp.求含m+1个节点边权和最大的子树.对每个分支节点有三种操作:剪去左子树:剪去右子树:将其节点数合理分配给左右子树: 记以x为根,含k个节点的子树的最大边权和为g[x][k]. 若x为叶节点,则g[x][k]为x通往父节点的边权:若非叶节点,枚举k-1个节点分配在左右子树的所有可能方案,找到最佳方案: #include<cstdio> #include<cstring> #include<alg

BNUOJ 13358 Binary Apple Tree

Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 101864-bit integer IO format: %lld      Java class name: (Any) Let's imagine how apple tree looks in binary computer world. You're right, it

URAL1018 Binary Apple Tree

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 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 e

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

URAL1018 Binary Apple Tree(树形DP)

题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数 转移就是左子树若干个,右子树若干个转移.. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #d

【树状数组】POJ 3321 Apple Tree

/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod