answer
Tree \ (\ text {DP} \ ) introductory questions.
We set \ (dp [i] [0/1 ] \) denotes \ (i \) nodes selected \ (/ \) do not choose the greatest happiness index.
State transition equation:
\ (DP [I] [0] = A [I] + \ sum_ {v∈son [U]} DP [V] [. 1] \) , where \ (a [i] \) for each employees of the happiness index.
\ (dp [i] [1 ] = \ sum_ {v∈son [u]} \ max {(dp [v] [1], dp [v] [0])} \)
The answer is \ (\ max {(DP [RT] [0], DP [RT] [. 1])} \) , where \ (RT \) is not superior employees.
Transfer click.
Code
#include <bits/stdc++.h>
#define itn int
#define gI gi
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar();
return f * x;
}
const int maxn = 6003;
int n, a[maxn], tot, head[maxn], ver[maxn * 2], nxt[maxn], ans, vis[maxn], rt;
int dp[maxn][2];//0:xuan 1:buxuan
inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void dfs(int u, int f)
{
dp[u][0] = a[u];
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == f) continue;
dfs(v, u);
dp[u][0] += dp[v][1];
dp[u][1] += max(dp[v][0], dp[v][1]);//状态转移
}
}
int main()
{
n = gi();
for (int i = 1; i <= n; i+=1) a[i] = gi();
for (int i = 1; i < n; i+=1)
{
int u = gi(), v = gi();
add(u, v);
add(v, u);
vis[u] = 1;
}
int h = gi(), o = gi();
for (int i = 1; i <= n; i+=1) if (!vis[i]) {rt = i; break;}//找到根节点,即没有上司的员工编号
dfs(rt, 0);
printf("%d\n", max(dp[rt][0], dp[rt][1]));//答案就是根节点选/不选取max
return 0;
}
to sum up
Thus, we can conclude tree \ (\ text {DP} \ ) substantially in the form of state:
\ (dp [i] [... ] \) represents \ (I \) state nodes.