Anniversary party
the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your
task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 6010
#define max(a,b) (a)>(b)?(a):(b)
int n,dp[N][2],v[N];
//dp[i][0]表示不选择i点时,i点及其子树能选出的最大值
//dp[i][1]表示选择i点时,i点及其子树能选出的最大值
vector<int> child[N];
void dfs(int x)
{
for(int i=0;i<child[x].size();++i)
{
int y=child[x][i];
dfs(y);
dp[x][1]+=dp[y][0];
dp[x][0]+=max(dp[y][0],dp[y][1]);
}
//dp[x][1]++;
}
int main()
{
int a,b;
for(int i;scanf("%d",&n)!=EOF;)
{
for(i=0;i<=n;++i)
child[i].clear();
memset(dp,0,sizeof(dp));
memset(v,0,sizeof(v));
for(i=1;i<=n;++i)
scanf("%d",&dp[i][1]);
for(;scanf("%d%d",&a,&b);)
{
if((a+b)==0) break;
child[b].push_back(a);
v[a]=1;
}
for(i=1;i<=n;++i)
if(v[i]==0)
{
dfs(i);
break;
}
printf("%d\n",max(dp[i][0],dp[i][1]));
}
return 0;
}