-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuogu_P_1352.cpp
37 lines (37 loc) · 1 KB
/
Luogu_P_1352.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/extc++.h>
using namespace std;
namespace pbds = __gnu_pbds;
using ui = unsigned int;
using tree = vector<vector<size_t>>;
void dfs(vector<int> const& r, tree const& t, vector<pair<int, int>>& f,
size_t const& p) {
f[p].first = r[p], f[p].second = 0;
for (size_t const& i : t[p]) {
dfs(r, t, f, i);
f[p].first += f[i].second, f[p].second += max(f[i].first, f[i].second);
}
}
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t n;
cin >> n;
vector<int> r(n);
for (int& i : r) cin >> i;
tree t(n);
size_t root = 0;
for (size_t i = 0; i < n; i++) root ^= i;
for (size_t i = 1; i < n; i++) {
size_t l, k;
cin >> l >> k;
t[--k].push_back(--l);
root ^= l;
}
vector<pair<int, int>> f(n); // first: 来了 second: 没来
dfs(r, t, f, root);
cout << max(f[root].first, f[root].second);
#ifdef debug
cout.put('\n');
for (pair<int, int> const& i : f) cout << i.first << ' ' << i.second << '\n';
#endif
return 0;
}