"Problem description"
Ural University has a staff of N and is numbered 1~n. They have affiliation, which means that their relationship is like a tree rooted in the headmaster, and the parent node is the direct boss of the child node. Each employee has a happiness index. There is now an anniversary party, which asks the staff to have the most happiness index. However, no staff is willing to attend with the direct boss.
Input
The first line is an integer n. (1<=n<=6000)
Next n lines, line i+1 represents the happiness index RI for staff i. ( -128<=ri<=127)
Next N-1 line, enter a pair of integer l, K for each line. Indicates that K is the direct boss of L.
Last line input 0, 0.
Output
Output the maximum happiness index.
"Algorithmic Analysis"
Similar to the 0-1 Backpack tree DP Basic problem, state transfer is described in the code.
"Program Code"
1 varA,FA:Array[1..6000] ofLongint;2F:Array[1..6000,0..1] ofLongint;3SArray[1..6000] ofLongint;4 I,j,k,n,m,ans,u,v:longint;5 functionMax (x,y:longint): Longint;6 begin7 ifX>y ThenExit (x)Elseexit (y);8 End;9 procedureDP (x:longint);Ten varI:longint; One begin A ifs[x]=0 Then beginF[x,0]:=0; F[x,1]:=A[X]; ExitEnd; - fori:=1 toN Do ifFa[i]=x Then - begin the DP (i); -Inc (F[X,0],max (F[i,1],f[i,0])); -Inc (F[X,1],f[i,0]); - End; + End; - begin + READLN (n); A fori:=1 toN Doreadln (A[i]); at fori:=1 toN-1 Do - begin - readln (u,v); -fa[u]:=v; - Inc (S[v]); - End; in readln (u,v); - fori:=1 toN Do iffa[i]=0 Thenk:=i; to fori:=1 toN DoF[i,1]:=A[i]; + DP (k); -Writeln (Max (F[k,1],f[k,0])); the End.
Disclaimer: This blog post for Blogger original blog, do not reprint without permission.
[Codevs 1380] No boss's prom