L1352 no party boss

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define rep(i, a, b) for (int i = a; i <= b; ++i)
 4 const int N = 6007;
 5 
 6 int n, a[N], f[N][2]; vector <int> e[N];
 7  
 8 inline void dfs(int u, int fa) {
 9     f[u][1] = a[u];
10     for (auto v : e[u]) { if (v == fa) continue;
11         dfs(v, u);
12         f[u][0] += max(f[v][0], f[v][1]);
13         f[u][1] += f[v][0];
14     }
15 }
16 
17 int main() {
18     scanf("%d", &n);
19     rep(i ,1, n) {
20         scanf("%d", &a[i]);
21     } int root = n * (n + 1) / 2;
22     rep(i, 1, n) { int x, y;
23         scanf("%d%d", &x, &y);
24         e[y].push_back(x); root -= x;
25     }
26     dfs(root, 0);
27     printf("%d\n", max(f[root][0], f[root][1]));
28     return 0;
29 }

 

Guess you like

Origin www.cnblogs.com/Fo0o0ol/p/11101546.html