0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-11 22:06:27 +00:00

#440. 大白兔的聚会

https://sjzezoj.com/submission/12241
This commit is contained in:
Baoshuo Ren 2021-07-01 14:15:07 +08:00 committed by Baoshuo Ren
parent e7613a7db1
commit 105ce7cac4
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

33
S2OJ/440/440.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
int n, l, k, root, dp[6005][2], p[6005];
void dfs(int x) {
for (int i = 1; i <= n; i++) {
if (p[i] == x) {
dfs(i);
dp[x][0] += max(dp[i][0], dp[i][1]);
dp[x][1] += dp[i][0];
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> dp[i][1];
}
while (cin >> l >> k && (l != 0 && k != 0)) {
p[l] = k;
}
for (int i = 1; i <= n; i++) {
if (!p[i]) {
root = i;
}
}
dfs(root);
cout << max(dp[root][0], dp[root][1]) << endl;
return 0;
}