#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int father[6005];
int dp[6005][2];
int value[6005];
vector<int> list[6005];
int N, M;
void dfs(int father)
{
int len = list[father].size();
for(int i = 0; i <len; i++)
{
int child = list[father][i];
dfs(child);
dp[father][0]+= max(dp[child][0], dp[child][1]);
dp[father][1] += dp[child][0];
}
}
int main()
{
//freopen("in.txt", "r", stdin);
while (~scanf("%d", &N))
{
M++;
memset(dp, 0, sizeof(dp));
memset(value, 0, sizeof(value));
for (int i = 1; i <= N; i++)
{
cin >> value[i];
father[i] = -1;
list[i].clear();
dp[i][1] = value[i];
}
int a, b;
while (cin >> a >> b, a || b)
{
father[a] = b;
list[b].push_back(a);
}
int root = 1;
while(father[root] != -1)
{
root = father[root];
}
dfs(root);
cout << max(dp[root][0], dp[root][1]) << endl;
}
fclose(stdin);
return 0;
}
HDU 1520(树形dp)
最新推荐文章于 2019-11-05 00:42:39 发布