为什么我的程序总是雷打不动的10分?

program vijos1642;

type link=^node;

node=record

data:integer;

left,right:link;

end;

var a:link;

n,m:integer;

w,have:array [1..1000] of integer;

f:array [1..1000,0..1000] of longint;

function pre(p:link):integer;

var q:link;

begin

q:=p^.left;

while qnil do

begin

have[p^.data]:=have[p^.data]+pre(q);

q:=q^.right;

end;

pre:=have[p^.data];

end;

function find(p:link;x:integer):link;

var v:link;

begin

if p=nil then exit(nil);

if p^.data=x then exit(p)

else

begin

v:=find(p^.left,x);

if vnil then exit(v)

else find:=find(p^.right,x);

end;

end;

procedure init;

var i,j,k,l:integer;

p,q,r:link;

flag:boolean;

begin

fillchar(f,sizeof(f),0);

for i:=1 to n do

begin

readln(w[i],k);

p:=find(a,i);

if p=nil then

begin

new(p);

p^.data:=i;

p^.left:=nil;

p^.right:=nil;

end;

flag:=false;

for j:=1 to k do

begin

read(l);

r:=find(a,l);

if rnil then q:=r

else

begin

new(q);

q^.data:=l;

q^.left:=nil;

q^.right:=nil;

end;

if (j=1) and (p^.left=nil) then p^.left:=q

else

begin

if not(flag) then

begin

p:=p^.left;

flag:=true;

end;

if j=1 then

while p^.rightnil do

p:=p^.right;

p^.right:=q;

p:=q;

end;

end;

end;

for i:=1 to n do

begin

if w[i]>0 then f:=w[i];

have[i]:=1;

end;

end;

function min(x,y:integer):integer;

begin

if x>y then exit(y);

exit(x);

end;

function max(x,y:longint):longint;

begin

if x>y then max:=x

else max:=y;

end;

procedure main(p:link);

var i,j:integer;

q:link;

begin

q:=p^.left;

while qnil do

begin

main(q);

q:=q^.right;

end;

q:=p^.left;

while qnil do

begin

for j:=min(have[p^.data],m) downto 1 do

for i:=1 to min(have[p^.data]-1,j-1) do

f[p^.data,j]:=max(f[p^.data,j],f[q^.data,i]+f[p^.data,j-i]);

q:=q^.right;

end;

end;

procedure outp;

var i,j:integer;

ans:longint;

begin

for i:=1 to n do

for j:=0 to m do

ans:=max(ans,f);

writeln(ans);

end;

begin

readln(n,m);

new(a);

new(a^.left);

new(a^.right);

a^.left:=nil;

a^.right:=nil;

a^.data:=1;

init;

pre(a);

main(a);

outp;

end.

1 条评论

  • 1

信息

ID
1642
难度
8
分类
动态规划 | 树形DP 点击显示
标签
递交数
1802
已通过
257
通过率
14%
被复制
4
上传者