You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 6010, M = N << 1;
int happy[N]; // 快乐值
int in[N]; // 入度
int f[N][2]; // dp的状态结果数组
int n;
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
// 通过深度优先搜索,对树进行遍历
void dfs(int u) {
f[u][1] += happy[u];
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
dfs(v); // 继续探索它的孩子,它的值是由它的孩子来决定的
f[u][1] += f[v][0]; // 它选择了,它的孩子就不能再选
f[u][0] += max(f[v][0], f[v][1]); // 它不选择,那么它的每一个孩子,都是可以选择或者不选择的
}
}
int main() {
// 邻接表表头初始化
memset(h, -1, sizeof h);
cin >> n;
for (int i = 1; i <= n; i++) cin >> happy[i];
// 读入树
for (int i = 1; i < n; i++) { // n-1条边
int x, y;
cin >> x >> y;
add(y, x);
in[x]++; // 记录入度因为需要找出大boss
}
// 从1开始找根结点
int root = 1;
while (in[root]) root++; // 找到根结点,入度为0
// 递归
dfs(root);
// 取两个
printf("%d\n", max(f[root][0], f[root][1]));
return 0;
}