Test instructions: There are n people, the next n line is the value of N, and then the next n line gives L,k said that L is the Boss is K, here note l and K can not appear at the same time
Link: Point Me
DP[I][1] + = dp[j][0],
DP I [0] + = max{dp[j][0],dp[j][1]}, where j is the child node of I.
The second time, it feels like water.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; AtypedefLong Longll; - #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); the Const intmaxn=6010; - intN,m,tt; - intAA[MAXN]; - inttot,head[maxn],dp[maxn][2],inch[MAXN]; + structEdge - { + intTo,next; A }EDGE[MAXN]; at voidAddedge (intUintv) - { -edge[tot].to=v; -edge[tot].next=Head[u]; -head[u]=tot++; - } in intDfsintu) - { to for(inti=head[u];i!=-1; i=edge[i].next) + { - intv=edge[i].to; the Dfs (v); *dp[u][1]+=dp[v][0]; $dp[u][0]+=max (dp[v][0],dp[v][1]);Panax Notoginseng } - returnMax (dp[u][0],dp[u][1]); the } + voidInit () A { thetot=0; +memset (head,-1,sizeof(head)); - CL (DP); $Clinch); $ } - intMain () - { the inti,j,k; - #ifndef Online_judgeWuyiFreopen ("1.in","R", stdin); the #endif - while(SCANF ("%d", &n)! =EOF) Wu { - init (); About for(i=1; i<=n;i++) $scanf"%d", &dp[i][1]); - intb; - while(SCANF ("%d%d",&a,&b)) - { A if(a==0&&b==0) Break; + Addedge (b,a); the inch[a]++; - } $ intans=0; the for(i=1; i<=n;i++)//The title means a tree the { the if(!inch[i]) ans=DFS (i); the } -printf"%d\n", ans); in } the}
POJ 2342 && HDU 1520 Tree DP