【树型dp】ural 1018

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.

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. 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



题意:一颗二叉苹果树树上结苹果,要求对其进行剪枝,求保留Q根树枝能保留的最多到苹果数。
树型dp,当年树型dp入门就是用的这个题,
f[i][j]表示以i为根节点的子树保留j条边所能得到的最多的苹果
 f[root][j + k + 1] = max(f[left][j] + f[right][k] + a[root][i]); (left和right是root的儿子)

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

const int M = 1100;
int n, m;

long f[M][M];
long a[M][M], map[M][M];
void init()
{
    for(long i = 1; i <= n - 1; i++)
    {
        long x, y, z;
        scanf("%d%d%d",&x, &y, &z);
        map[x][0]++;
        map[x][map[x][0]] = y;
        a[x][map[x][0]] = z;
        map[y][0]++;
        map[y][map[y][0]] = x;
        a[y][map[y][0]] = z;
    }
    for (long i = 1; i <= 1000;  i++)
    for (long j = 1; j <= 1000;  j++)
        f[i][j] = -1;
}
long max(long a, long b)
{
    return a > b ? a : b;
}
void dp(long root, long fa)
{
    //cout <<root <<' '<<fa<<endl;
    f[root][0] = 0;
    for (long i = 1; i <= map[root][0];  i++)
    {
        if (map[root][i] != fa)
        {
            long tmp = map[root][i];
            dp(tmp , root);

           for (long j = m; j >= 0; j--)
            {
                if (f[root][j] >= 0)
                {
                    for (long k = m; k >= 0; k--)
                    {
                        if((j + k <= m)&&(f[tmp][k] >= 0))
                        {
                            f[root][j + k + 1] = max(f[root][j + k + 1], f[root][j] + f[tmp][k] + a[root][i]);
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    init();
    dp(1, 0);
    printf("%d\n", f[1][m]);
    return 0;
}




全部评论

相关推荐

点赞 评论 收藏
分享
刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务