{202错误,调不出来}
二叉苹果树(apple)
有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。我们用一根树枝两端连接的结点的编号来描述一根树枝的位置。如图是一棵有4个树枝的树。
现在这棵树枝条太多了,需要剪枝。但是树枝上长有苹果。
给定需要保留的树枝数量,求出最多能留住多少苹果。
输入:
第1行:结点数N和要保留的树枝数量M
(1<=M<= N,1<N<=100)。
接下来N-1行描述树枝的信息:
每行3个整数,前两个是它连接的结点的编号。第3个数是这根树枝上苹果的数量。
每根树枝上的苹果不超过30000个。
输出:一个数,最多能留住的苹果的数量。
样例输入
5 2
1 3 1
1 4 10
2 3 20
3 5 20
样例输出
21
type tree=record
lch,rch:longint;
l,r:longint;
end;
const maxn=10;
var branch:array [1..maxn,1..maxn] of longint;
f:array[1..maxn,1..maxn] of longint;
v:array[1..maxn] of boolean;
t:array[1..maxn] of tree;
n,m:longint;
procedure init;
var i,j,k,o:longint;
begin
readln(n,m);
for o:=1 to n-1 do
begin
readln(i,j,k);
branch[i,j]:=k;
branch[j,i]:=k;
end;
end;
procedure treemake(x:longint);
var i:longint;
begin
for i:=1 to n do
begin
if ((branch[x,i]<>0) and (not v[i])) then
begin
if t[i].lch=0 then
begin
t[i].lch:=i;
t[i].l:=branch[x,i];
end
else
begin
t[i].rch:=i;
t[i].r:=branch[x,i];
end;
end;
v[i]:=false;
treemake(i+1);
end;
end;
procedure appletrees(x,y:longint);
var i,j,temp:longint;
begin
if ((t[x].lch=0) or (y=0)) then
begin
f[x,y]:=0;
exit;
end;
if y=1 then
begin
f[x,y]:=t[x].l;
if f[x,y]<t[x].r then
f[x,y]:=t[x].r;
end
else
begin
temp:=t[x].l+t[x].r;
for i:=0 to y-2 do
begin
appletrees(t[x].lch,i);
appletrees(t[x].rch,y-i-2);
if temp+f[t[x].lch,i]+f[t[x].rch,y-i-2]>f[x,y] then
f[x,y]:=temp+f[t[x].lch,i]+f[t[x].rch,y-i-2];
end;
appletrees(t[x].lch,y-1);
appletrees(t[x].rch,y-1);
if f[x,y]<t[x].l+f[t[x].lch,y-1] then
f[x,y]:=t[x].l+f[t[x].lch,y-1];
if f[x,y]<t[x].r+f[t[x].r,y-1] then
f[x,y]:=t[x].r+f[t[x].rch,y-1];
end;
end;
begin
fillchar(branch,sizeof(branch),0);
fillchar(f,sizeof(f),0);
fillchar(v,sizeof(v),false);
v[1]:=true;
init;
treemake(1);
appletrees(1,n);
writeln(f[1,n]);
end.
二叉苹果树(apple)
有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。我们用一根树枝两端连接的结点的编号来描述一根树枝的位置。如图是一棵有4个树枝的树。
现在这棵树枝条太多了,需要剪枝。但是树枝上长有苹果。
给定需要保留的树枝数量,求出最多能留住多少苹果。
输入:
第1行:结点数N和要保留的树枝数量M
(1<=M<= N,1<N<=100)。
接下来N-1行描述树枝的信息:
每行3个整数,前两个是它连接的结点的编号。第3个数是这根树枝上苹果的数量。
每根树枝上的苹果不超过30000个。
输出:一个数,最多能留住的苹果的数量。
样例输入
5 2
1 3 1
1 4 10
2 3 20
3 5 20
样例输出
21
type tree=record
lch,rch:longint;
l,r:longint;
end;
const maxn=10;
var branch:array [1..maxn,1..maxn] of longint;
f:array[1..maxn,1..maxn] of longint;
v:array[1..maxn] of boolean;
t:array[1..maxn] of tree;
n,m:longint;
procedure init;
var i,j,k,o:longint;
begin
readln(n,m);
for o:=1 to n-1 do
begin
readln(i,j,k);
branch[i,j]:=k;
branch[j,i]:=k;
end;
end;
procedure treemake(x:longint);
var i:longint;
begin
for i:=1 to n do
begin
if ((branch[x,i]<>0) and (not v[i])) then
begin
if t[i].lch=0 then
begin
t[i].lch:=i;
t[i].l:=branch[x,i];
end
else
begin
t[i].rch:=i;
t[i].r:=branch[x,i];
end;
end;
v[i]:=false;
treemake(i+1);
end;
end;
procedure appletrees(x,y:longint);
var i,j,temp:longint;
begin
if ((t[x].lch=0) or (y=0)) then
begin
f[x,y]:=0;
exit;
end;
if y=1 then
begin
f[x,y]:=t[x].l;
if f[x,y]<t[x].r then
f[x,y]:=t[x].r;
end
else
begin
temp:=t[x].l+t[x].r;
for i:=0 to y-2 do
begin
appletrees(t[x].lch,i);
appletrees(t[x].rch,y-i-2);
if temp+f[t[x].lch,i]+f[t[x].rch,y-i-2]>f[x,y] then
f[x,y]:=temp+f[t[x].lch,i]+f[t[x].rch,y-i-2];
end;
appletrees(t[x].lch,y-1);
appletrees(t[x].rch,y-1);
if f[x,y]<t[x].l+f[t[x].lch,y-1] then
f[x,y]:=t[x].l+f[t[x].lch,y-1];
if f[x,y]<t[x].r+f[t[x].r,y-1] then
f[x,y]:=t[x].r+f[t[x].rch,y-1];
end;
end;
begin
fillchar(branch,sizeof(branch),0);
fillchar(f,sizeof(f),0);
fillchar(v,sizeof(v),false);
v[1]:=true;
init;
treemake(1);
appletrees(1,n);
writeln(f[1,n]);
end.