mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 14:36:27 +00:00
285. 没有上司的舞会
https://www.acwing.com/problem/content/submission/code_detail/13122554/
This commit is contained in:
parent
42d532b182
commit
772fff1906
50
AcWing/285/285.cpp
Normal file
50
AcWing/285/285.cpp
Normal 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
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
BIN
AcWing/285/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user