http://poj.org/problem?id=2342
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17663
Test instructions
A company will hold a party, but in order to make the party more active, each party will not want to see his direct boss at the party, now known to everyone's active index and boss relationship (of course, there is no link), ask who (how many people) to invite who can make the total activity of the party is the largest.
States are the trade-offs of each point.
can be used dp[i,1] and dp[i,0, respectively) to express the first person to come and not to come.
When I come, dp[i][1] + = dp[j][0];//j is the subordinate of I
When I do not come, dp[i][0] +=max (dp[j][1],dp[j][0]);//j is the subordinate of I
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using
namespace Std;
typedef unsigned __INT64 ull;
Const double Pi=acos (-1.0);
Double eps=0.000001;
int max (int a,int b) {return a<b?b:a;} int tm[6005];
int n;
int vis[6005];
vector< Vector<int > > MP (6005);
int dp[6005][2];
void Dfs (int x) {int i;
DP[X][1]=TM[X];
dp[x][0]=0;
For (I=0;i<mp[x].size (); i++) {int v=mp[x][i];
DFS (v);
DP[X][1]+=DP[V][0];
Dp[x][0]+=max (Dp[v][1],dp[v][0]);
}} int main () {cin>>n; int i;
for (i=1;i<=n;i++) scanf ("%d", &tm[i]);
int x, y;
for (i=1;i<n;i++) {scanf ("%d%d", &x,&y);
Mp[y].push_back (x);
Vis[x]=1;
} scanf ("%d%d", &x,&y);
for (i=1;i<=n;i++) {if (!vis[i]) break;
} int root=i;
DFS (root); printf ("%d\n", Max (dp[root][1],dp[root][0]));
return 0;
}