-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcWing285.cpp
47 lines (47 loc) · 864 Bytes
/
AcWing285.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
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string.h>
using namespace std;
const int N = 6010;
int h[N], e[N], ne[N], idx;
bool has_father[N];
int happy[N];
int f[N][2];
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++;
}
void dfs(int x)
{
f[x][1] = happy[x];
for (int i = h[x]; i != -1; i = ne[i])
{
int j = e[i];
dfs(j);
f[x][1] += f[j][0];
f[x][0] += max(f[j][1], f[j][0]);
}
}
int main()
{
int n;
cin >> n;
memset(h, -1, sizeof(h));
for (int i = 1; i <= n; i++)
{
cin >> happy[i];
}
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
has_father[a] = true;
add(b, a);
}
//找到根节点的编号
int root = 1;
while(has_father[root]) root ++;
dfs(root);
cout << max(f[root][1], f[root][0]);
}