0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P1352 没有上司的舞会

R41231597
This commit is contained in:
Baoshuo Ren 2020-11-04 17:22:54 +08:00 committed by Baoshuo Ren
parent 1abfdaa2f0
commit 816c9635a7
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

42
problem/P1352/P1352.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;
/* Template: 树形 DP */
vector<int> son[6005];
int n, l, k, f[6005][2], h[6005];
bool v[6005];
void dp(int x) {
int y;
f[x][0] = 0;
f[x][1] = h[x];
for (int i = 0; i < son[x].size(); i++) {
y = son[x][i];
dp(y);
f[x][0] += max(f[y][0], f[y][1]);
f[x][1] += f[y][0];
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i < n; i++) {
cin >> l >> k;
v[l] = 1;
son[k].push_back(l);
}
int root;
for (int i = 1; i <= n; i++) {
if (!v[i]) {
root = i;
break;
}
}
dp(root);
cout << max(f[root][0], f[root][1]) << endl;
return 0;
}