ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1018. Binary Apple Tree

WHY I always TLE at #6
Posted by Kobe Bryant 28 Jul 2008 18:57
THis is my proram:





var
m:array[0..200,0..200] of int64;
k,sonr,sonl:array[0..200] of int64;
f:array[0..200,0..200] of int64;
a,b,c,i,j,q,n:longint;
procedure find(s,q:longint);
var
i:longint;
begin
  if s>0 then
  begin
  if q=0 then m[s,q]:=0
  else
  begin


     if sonl[s]>0 then find(sonl[s],q-1);
     if sonr[s]>0 then find(sonr[s],q-1);
     if m[sonl[s],q-1]+f[s,sonl[s]]>m[s,q] then m[s,q]:= m[sonl[s],q-1]+f[s,sonl[s]];
     if m[sonr[s],q-1]+f[s,sonr[s]]>m[s,q] then m[s,q]:= m[sonr[s],q-1]+f[s,sonr[s]];
     if (sonl[s]>0)and(sonr[s]>0) then
     for i:=0 to q-2do
     begin
       if sonl[s]>0 then find(sonl[s],i);
       if sonr[s]>0 then find(sonr[s],q-2-i);
       if m[sonl[s],i]+m[sonr[s],q-2-i]+f[s,sonl[s]]+f[s,sonr[s]]>m[s,q] then
       m[s,q]:=m[sonl[s],i]+m[sonr[s],q-2-i]+f[s,sonl[s]]+f[s,sonr[s]]
     end;
   end;
end;
end;






procedure build(s:longint);
var
i:longint;
begin
for i:=1 to n do
if (k[i]=0)and(f[s,i]<>-1) then
begin

   k[i]:=1;
  if (sonl[s]=0)  then sonl[s]:=i else sonr[s]:=i;




end;

if sonl[s]<>0  then build(sonl[s]);
if sonr[s]<>0  then build(sonr[s]);
end;



begin
readln(n,q);
for i:=1 to n do
for j:=1 to n do
f[i,j]:=-1;
for i:=1 to n-1 do
begin
  readln(a,b,c);
  f[a,b]:=c;
  f[b,a]:=c;
end;

k[1]:=1;
k[0]:=1;
build(1);
find(1,q);
writeln(m[1,q]);



end.