0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 13:56:26 +00:00

285. 没有上司的舞会

https://www.acwing.com/problem/content/submission/code_detail/13122554/
This commit is contained in:
Baoshuo Ren 2022-04-07 14:30:07 +08:00
parent 42d532b182
commit 772fff1906
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
3 changed files with 56 additions and 0 deletions

50
AcWing/285/285.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 6005;
int n, h[N], f[N][2], root;
std::vector<int> g[N];
bool vis[N];
void dfs(int u) {
f[u][0] = 0;
f[u][1] = h[u];
for (int v : g[u]) {
dfs(v);
f[u][0] += std::max(f[v][0], f[v][1]);
f[u][1] += f[v][0];
}
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1, l, k; i < n; i++) {
cin >> l >> k;
vis[l] = true;
g[k].push_back(l);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
root = i;
break;
}
}
dfs(root);
cout << std::max(f[root][0], f[root][1]) << endl;
return 0;
}

BIN
AcWing/285/data/7.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
AcWing/285/data/7.in (Stored with Git LFS) Normal file

Binary file not shown.