>Link
luogu P1352
ybtoj树上求和
>解题思路
经典例题 划水
>代码
**#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 6010
using namespace std;
struct edge
{
int to, nxt;
} e[N * 2];
int n, a[N], f[N][5], cnt, h[N];
void dfs (int now, int fath)
{
for (int i = h[now]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fath) continue;
dfs (v, now);
f[now][1] += f[v][0];
f[now][0] += max (f[v][1], f[v][0]);
}
f[now][1] += a[now];
}
int main()
{
int x, y;
scanf ("%d", &n);
for (int i = 1; i <= n; i++) scanf ("%d", &a[i]);
for (int i = 1; i < n; i++)
{
scanf ("%d%d", &x, &y);
e[++cnt] = (edge){y, h[x]}; h[x] = cnt;
e[++cnt] = (edge){x, h[y]}; h[y] = cnt;
}
dfs (1, 0);
printf ("%d", max (f[1][1], f[1][0]));
return 0;
}**