#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct poj2342 {
/*[问题描述]:大学聚会,要求到场的任何两个人都不是上司/下属关系,每个人都有一个高兴值c[i],可正可负,
要求让总的高兴值最大*/
/*[解题思路]:f[i][0]表示以i为根的子树的最大高兴值且第i个人不去,f[i][1]表示以i为根的子树的最大高兴值且第i个人去
则f[i][0]=sum(max(f[k][0],f[k][1])),f[i][1]=c[i]+sum(f[k][0]),k是i的下属*/
int n;
int root;
int c[6005];
int f[6005][2];
int p[6005];
bool vis[6005];
void dfs(int node) {
vis[node] = true;
f[node][1] = c[node];
f[node][0] = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i] && p[i] == node) {
dfs(i);
f[node][0] += max(f[i][0], f[i][1]);
f[node][1] += f[i][0];
}
}
}
void work() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
memset(p, 0, sizeof(p));
int x, y;
while (cin >> x >> y) {
if (x == 0 && y == 0) break;
p[x] = y;
}
root = 1;
while (p[root] != 0)
root = p[root];
//cout << "root:" << root << endl;
memset(vis, 0, sizeof(vis));
dfs(root);
cout << max(f[root][0], f[root][1]) << endl;
}
};
int main()
{
poj2342 solution;
solution.work();
return 0;
}
poj2342-Anniversary party(树形动规)
最新推荐文章于 2019-05-31 21:05:57 发布