F[I,0] indicates that the first person does not attend the dance
f[i,1] indicates that the first person attends the dance
F[i,1]=sigma (f[j,0]) +v[i] J for Children of I
F[i,1]=sigma (Max (f[j,0],f[j,1)) J for Children of I
Ans=max (f[root,0],f[root,1])
Program CODEVS1380;typeArr=RecordU,v,next:longint; End;Constmaxn=10000;varegArray[0.. MAXN] ofarr; Last:Array[0.. MAXN] ofLongint; A:Array[0.. MAXN] ofLongint; F:Array[0.. MAXN,0..1] ofLongint; FA:Array[0.. MAXN] ofLongint; I,n,u,v,j,root:longint;procedureAdd (u,v:longint);beginInc (J); EG[J].U:=u; EG[J].V:=v; Eg[j].next:=Last[u]; Last[u]:=J;End;functionMax (a,b:longint): Longint;begin ifA>b ThenExit (a)Elseexit (b);End;procedureDFS (u:longint);varI,v,sum1,sum2:longint;begin iflast[u]=-1 Then beginF[u,0]:=0; F[u,1]:=A[u]; Exit End; I:=last[u]; sum1:=0; sum2:=0; whilei<>-1 Do beginV:=eg[i].v; DFS (v); SUM1:=sum1+f[v,0]; SUM2:=sum2+max (F[v,0],f[v,1]); I:=Eg[i].next; End; F[u,0]:=sum2; F[u,1]:=sum1+A[u];End;beginREADLN (n); J:=-1; fori:=1 toMaxn Dolast[i]:=-1; fori:=1 toN Doreadln (A[i]); READLN (V,u); while(v<>0) and(u<>0) Do beginFa[v]:=u; Add (U,V); READLN (V,u); End; fori:=1 toN Do iffa[i]=0 Thenroot:=i; DFS (root); Writeln (Max (F[root,0],f[root,1]));End.
CODEVS1380 prom with no boss (tree DP)