Description
Let's imagine how Apple tree looks inbinary computer world. You're right, it's looks just like a binary tree, i.e. Anybiparous Branch splits up to exactly, and new branches. We'll enumerate byintegers the root of binary apple tree, points of branching and the ends oftwigs. 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 usedfor enumerating be numbered in range from 1 to N, where n are the total NUMBEROF all enumerated points. For instance in the picture below N was equal to 5.Here are an example of a enumerated tree with four branches:
2 5
\ /
3 4
\/
1
As may know it's not convenient to Pickan apples from a tree when there is too much of branches. That's why some ofthem should is removed from a tree. But is interested in removing branchesin the minimal loss of apples. So your is given amounts of apples on abranches and amount of branches, that should is preserved. Your task is todetermine what many apples can remain on a tree after removing of excessivebranches.
Input
First line of input contains, Numbers:nand 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 containsdescriptions of branches. Each description consists of a three an integer numbersdivided by spaces. The first of them define branch by it ' s ending points. The third number defines the number of apples on this branch. Assumethat 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 ' sroot;-)
Sampleinput
5 2
1 3 1
1 4 10
2 3 20
3 5 20
Sampleoutput
21st
Main topic:
There is an n-relationship with the right binary tree to cut Q root branch to seek the maximum weight of the whole tree and.
Ideas:
Because it is the optimal value, I think of DP because it is a naked two fork tree so think of the naked tree dp, because it is a naked tree dp so put on the naked state transfer equation.
F[i,j]:=max (F[i.left,k]+a[i,i.left],f[i.right,j-k]+a[i,i.right]), 0<=k<=j
Where F[i,j] indicates that I is the root node, cutting the optimal value of J Root Branch, A[i,j] is connected to the weight on the branch I, J. F[1,Q] is the answer. Note to start converting to a binary tree.
Source Code/pas
typetree=record L,r:longint;end;var N,q:longint; A,f,v:array[0..100,0..100]of Longint; T:array[0..100]of tree;function Max (x,y:longint): Longint;begin if X>y then max:=x else max:=y;end;procedure Init;va R i,x,y,z:longint;begin Readln (N,Q); For i:=1 to N-1 do begin READLN (x, y, z); A[x,y]:=z; A[y,x]:=z; V[x,y]:=1; V[y,x]:=1; End;end;procedure Create (X:longint), Var i:longint;begin for I:=1 to N does if (v[x,i]<>0) then BEGIN if (T[X].L =0) then t[x].l:=i else if (t[x].r=0) then t[x].r:=i; v[i,x]:=0; v[x,i]:=0; Create (i); End;end;procedure Dp;var I,j,k:longint;begin for i:=1 to n do F[i,1]:=max (A[I,T[I].L],A[I,T[I].R]); For j:=2 to Q does for I:=1 to n do begin F[i,j]:=max (A[i,t[i].r]+f[t[i].r,j-1],a[i,t[i].l]+f[t[i].l,j-1]); For k:=0 to J-2 do F[i,j]:=max (F[I,J],F[T[I].L,K]+F[T[I].R,J-2-K]+A[I,T[I].L]+A[I,T[I].R]); End;end;begin Init; Create (1); dp Writeln (F[1,q]); end.
Binary Apple tree_ Problem Solving report _ural1018_ tree DP