Общий форумPlease help me - problem 1018? I saw the conversation between Nazarov Denis and Ivan Georgiev and tried to change my code in a proper way, but still get WA. Is this the straight order of branches description - first father, then son and then number of apples? Or it can also be first son, then father and finally number of apples? If so, then how do I know which node is the root? Here's my program: Program Apples; Const Max=100; Var af:Array[1..Max,1..Max] Of Longint;{values of f that has already been calculated} l,r:Array[1..Max] Of Longint;{Left, right sibling} na:Array[1..Max,1..2] Of Longint;{Number of apples on a branch} n,q,root:Longint; Procedure ReadData; Var i,j,x,y,v:Longint; cbroot:Array[1..Max] Of Boolean;{CanBeROOT-to find the root} Begin FillChar(l,SizeOf(l),0); FillChar(r,SizeOf(r),0); FillChar(cbroot,SizeOf(cbroot),True); Read(n,q); For i:=1 To n-1 Do Begin Read(x,y,v); If v=0 Then Begin Dec(n); Dec(q); Continue; End; { If v=0 Then Inc(q);} cbroot[y]:=False; If l[x]=0 Then Begin l[x]:=y; na[x,1]:=v; End Else Begin r[x]:=y; na[x,2]:=v; End; End; {Find the root} For i:=1 To n Do If cbroot[i] Then Begin root:=i; Break; End; {function f has not been calculated at all} For i:=1 To n Do For j:=1 To n Do af[i,j]:=-2; End; Function FMax(a,b:Longint):Longint; Begin If a>b Then FMax:=a Else FMax:=b; End; Function f(p,nb:Longint):Longint; {how much apples can be left when we have to leave nb branches on the subtree with root p?} Var res,a,b,c,d,i:Longint; Begin If af[p,nb]<>-2 Then{f is already calculated for these values} f:=af[p,nb] Else Begin If nb=0 Then res:=0 Else If nb=1 Then Begin If l[p]=0 Then{has no siblings} res:=-1 Else If r[p]=0 Then{has only left sibling} res:=na[p,1] Else {has both siblings} res:=FMax(na[p,1],na[p,2]); End Else Begin If l[p]=0 Then {has no siblings} res:=-1 Else If r[p]=0 Then Begin{has only left sibling} a:=f(l[p],nb-1); If a>=0 Then res:=na[p,1]+a Else res:=-1; End Else Begin{has both siblings} res:=-1; a:=f(l[p],nb-1); b:=f(r[p],nb-1); If a>=0 Then res:=na[p,1]+a; If b>=0 Then If na[p,2]+b>res Then res:=na[p,2]+b; For i:=0 To nb-2 Do Begin a:=f(l[p],i); b:=f(r[p],nb-2-i); If ((a>=0) And (b>=0)) Then If na[p,1]+a+na[p,2]+b>res Then res:=na[p,1]+a+na[p,2]+b End; End; End; af[p,nb]:=res; f:=res; End; End; Begin { Assign(input,'1018.in'); Reset(input);} ReadData; Writeln(F(root,q)); End. Re: For this prob, we use DP to solve, but the test cases seem to be wrong > I saw the conversation between Nazarov Denis and Ivan Georgiev and > tried to change my code in a proper way, but still get WA. > Is this the straight order of branches description - first father, > then son and then number of apples? Or it can also be first son, then > father and finally number of apples? If so, then how do I know which > node is the root? > > Here's my program: > > Program Apples; > Const Max=100; > Var af:Array[1..Max,1..Max] Of Longint;{values of f that has already > been calculated} > l,r:Array[1..Max] Of Longint;{Left, right sibling} > na:Array[1..Max,1..2] Of Longint;{Number of apples on a branch} > n,q,root:Longint; > Procedure ReadData; > Var i,j,x,y,v:Longint; > cbroot:Array[1..Max] Of Boolean;{CanBeROOT-to find the root} > Begin > FillChar(l,SizeOf(l),0); > FillChar(r,SizeOf(r),0); > FillChar(cbroot,SizeOf(cbroot),True); > Read(n,q); > For i:=1 To n-1 Do Begin > Read(x,y,v); > If v=0 Then Begin > Dec(n); > Dec(q); > Continue; > End; > { If v=0 Then > Inc(q);} > cbroot[y]:=False; > If l[x]=0 Then Begin > l[x]:=y; > na[x,1]:=v; > End Else Begin > r[x]:=y; > na[x,2]:=v; > End; > End; > {Find the root} > For i:=1 To n Do > If cbroot[i] Then Begin > root:=i; > Break; > End; > {function f has not been calculated at all} > For i:=1 To n Do > For j:=1 To n Do > af[i,j]:=-2; > End; > Function FMax(a,b:Longint):Longint; > Begin > If a>b Then > FMax:=a > Else > FMax:=b; > End; > Function f(p,nb:Longint):Longint; > {how much apples can be left when we have to > leave nb branches on the subtree with root p?} > Var res,a,b,c,d,i:Longint; > Begin > If af[p,nb]<>-2 Then{f is already calculated for these values} > f:=af[p,nb] > Else Begin > If nb=0 Then > res:=0 > Else If nb=1 Then Begin > If l[p]=0 Then{has no siblings} > res:=-1 > Else If r[p]=0 Then{has only left sibling} > res:=na[p,1] > Else {has both siblings} > res:=FMax(na[p,1],na[p,2]); > End Else Begin > If l[p]=0 Then {has no siblings} > res:=-1 > Else If r[p]=0 Then Begin{has only left sibling} > a:=f(l[p],nb-1); > If a>=0 Then > res:=na[p,1]+a > Else > res:=-1; > End Else Begin{has both siblings} > res:=-1; > a:=f(l[p],nb-1); > b:=f(r[p],nb-1); > If a>=0 Then > res:=na[p,1]+a; > If b>=0 Then > If na[p,2]+b>res Then > res:=na[p,2]+b; > For i:=0 To nb-2 Do Begin > a:=f(l[p],i); > b:=f(r[p],nb-2-i); > If ((a>=0) And (b>=0)) Then > If na[p,1]+a+na[p,2]+b>res Then > res:=na[p,1]+a+na[p,2]+b > End; > End; > End; > af[p,nb]:=res; > f:=res; > End; > End; > Begin > { Assign(input,'1018.in'); Reset(input);} > ReadData; > Writeln(F(root,q)); > End. > Help for you(+) I use dynamic programming and get WA becuse Judge IS WRONG. If there is branch with 0 applese you must: 1. delete it in any case 2. Q:=Q-1; So if branch with 0 apples have some sub-branches we delete them all! |