Simple Tree DP
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cstdlib>5 using namespacestd;6 intn,w[6001],ne=0;7 BOOL inch[6001];8 intf[6001][2];9 inthd[6001];TenInlineintR () { One intx=0, f=1; A CharCh=GetChar (); - while(ch>'9'|| ch<'0') { - if(ch=='-') f=-1; theCh=GetChar (); - } - while(ch>='0'&&ch<='9') { -x=x*Ten+ch-'0'; +Ch=GetChar (); - } + returnx*F; A } at - structData { - intV,next; -} e[6001]; - voidAddintVintu) { -ne++; ine[ne].v=v; -e[ne].next=Hd[u]; tohd[u]=NE; + } - voiddpintu) { thef[u][1]=W[u]; *f[u][0]=0; $ for(intP=hd[u]; p!=0; p=E[p].next) {Panax Notoginseng intv=e[p].v; - DP (v); thef[u][1]+=f[v][0]; +f[u][0]=f[u][0]+max (f[v][1],f[v][0]); A } the } + intMain () { -n=read (); $ intl,k; $ for(intI=1; i<=n; i++) -w[i]=read (); - while(L=read (), k=Read ()) { the if(l==0&&k==0) Break; - inch[l]=1;Wuyi Add (l,k); the } - for(intI=1; i<=n; i++) Wu if(!inch[i]) { - DP (i); AboutCout<<max (f[i][0],f[i][1]) <<Endl; $ Break; - } - return 0; -}
View Code
"codevs1380" has no boss's prom.