Basic tree-type DP. DP[I][J] I means someone, J says the man is coming. Transfer equation See procedure.
Note Pre-processing: First start all dp[i][1] for the given weight. First the people who have no subordinates into the team.
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int n,r[6005],fath[6005],num[6005];int dp[6005][3];void BFS () {queue <int> q;for (int i=1;i<=n;i++) if (num[i]= =0) Q.push (i), while (!q.empty ()) {int Head=q.front (); Q.pop ();DP [Fath[head]][0]+=max (dp[head][0],dp[head][1]);DP [ Fath[head]][1]+=dp[head][0];num[fath[head]]--;if (num[fath[head]]==0) Q.push (Fath[head]);}} int main () {memset (num,0,sizeof (num)), scanf ("%d", &n), for (int i=1;i<=n;i++) {scanf ("%d", &r[i]);DP [i][1]= R[i];} for (int i=1;i<=n-1;i++) {int x,y;scanf ("%d%d", &x,&y); fath[x]=y;num[y]++;} int yjq_naive,cx_ak;scanf ("%d%d", &yjq_naive,&cx_ak); BFS (); int maxn=0;for (int i=1;i<=n;i++) for (int j=0;j <=1;j++) Maxn=max (Maxn,dp[i][j]);p rintf ("%d\n", MAXN); return 0;}
Codevs 1380 no boss's prom