| 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's looks just like a binary tree, i.e. any biparous branch splits up to exactly, and new branches. We'll enumerate by integers the root of the binary Apple tree, points of branching and the ends of twigs. This is the distinguish different branches by their ending points. We'll assume that root of the tree always be numbered by 1 and all numbers used for enumerating be numbered in range from 1 To N, where n is the total number of all enumerated points. For instance in the picture belowN was equal to 5. Here's an example of a enumerated tree with four branches:
As May know it's not convenient to pick an apples from a tree when there is too much of branches. That's why some of them should is removed from a tree. Interested in removing branches in the the minimal loss of apples. So your is given amounts of apples on a branches and amount of branches the should be preserved. Your task is to determine what many apples can remain on a tree after removing of excessive branches.
Input
First line of input contains-numbers: n and Q (2≤ n ≤ 100; 1≤ Q ≤ n−1). N denotes the number of enumerated points in a tree. Qdenotes amount of branches that should be preserved. Next N −1 lines contains descriptions of branches. Each description consists of a three an integer numbers divided by spaces. The first of them define branch by it ' s ending points. The third number defines the number of apples on this branch. You may assume this no branch contains more than 30000 apples.
Output
Output should contain the only number-amount of apples, can be preserved. And don ' t forget to preserve tree ' s root;-)
Sample Input
| input |
Output |
5 21 3 11 4 102 3 203 5 20 |
21st |
Source
problem Source: Ural State University Internal Contest ' #2tree-shaped DPf[current Node []= optimal solution for selected node points]when DP is 1 more, it is because the current node also has to be selected, in order to select sub-nodes.
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn= $;9 structedge{Ten intv,w; One intNXT; A }E[MXN]; - inthd[mxn],mct=0; - voidAdd_edge (intUintVintdis) { thee[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=dis;hd[u]=MCT; - return; - } - intF[MXN][MXN]; + intNUM[MXN]; - intn,m; + voiddpintUintFA) { A inti,j,k; atnum[u]=0; - for(i=hd[u];i;i=e[i].nxt) { - intv=e[i].v; - if(V==FA)Continue; - DP (V,U); -num[u]+=num[v]+1; in for(j=num[u];j;--j) { - for(k=j;k;k--){ toF[u][j]=max (f[u][j],f[u][j-k]+f[v][k-1]+E[I].W); + } - } the } * return; $ }Panax Notoginseng intMain () { -scanf"%d%d",&n,&m); the inti,j; + intu,v,d; A for(i=1; i<n;i++){ thescanf"%d%d%d",&u,&v,&d); + Add_edge (u,v,d); - Add_edge (v,u,d); $ } $dp1,0); -printf"%d\n", f[1][m]); - return 0; the}
URAL1018 Binary Apple Tree