对上面有要求的树形dp:0115A

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/135608809

http://47.92.197.167:5283/contest/451/problem/1

我初始的思路是维护一个 dp(i,j)dp(i,j) ,表示以 ii 根,向下染黑最远 jj 层,但这样子统计贡献就很难维护。

不妨换个思路,这个 jj 变成往上钦定要至少 jj 层。那样子每个染黑的时候的贡献是已经确定了的。

而对于儿子的 jj ,往上转移时只会转移到 j1,j,j+1j-1,j,j+1

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ubsan: undefined
// accoders
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
#define int long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
#define Z(x) (x) * (x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 5010
int n, m, i, j, k, T;
int w[N], val[N];

namespace Sol1 {
int cr[N], b[N], ans, u, v, l, r;
vector<int> G[N];
// queue<int> q;
int q[N];
int check() {
int i, sum = 0, l, r;
l = r = 0;
// memset(cr, -1, sizeof(cr));
for (i = 1; i <= n; ++i) cr[i] = -1;
for (i = 1; i <= n; ++i)
if (!b[i])
cr[i] = 0, q[++r] = i;
if (l == r)
return val[n - 1] * n;
while (l < r) {
u = q[++l];
for (int v : G[u])
if (cr[v] == -1)
cr[v] = cr[u] + 1, q[++r] = v;
}
for (i = 1; i <= n; ++i)
if (cr[i] > 0)
sum += val[cr[i] - 1];
// debug(">> %lld \n", sum);
return sum;
}
void dfs(int x, int s) {
if (x > n) {
ans = max(ans, check() - s);
return;
}
b[x] = 0;
dfs(x + 1, s);
b[x] = 1;
dfs(x + 1, s + w[x]);
}
void Main() {
ans = 0;
for (i = 1; i < n; ++i) {
u = read();
v = read();
G[u].pb(v);
G[v].pb(u);
}
dfs(1, 0);
printf("%lld", ans);
}
} // namespace Sol1

namespace Sol3 {
int dp[N][N], ans, u, v, sum = 0;
vector<int> G[N];
void Mx(int &a, int b) { a = max(a, b); }
void dfs(int x, int fa) {
++sum;
int flg = 0;
dp[x][0] = 0;
for (int j = 1; j <= n; ++j) dp[x][j] = val[j - 1] - w[x];
for (int y : G[x])
if (y != fa) {
dfs(y, x);
for (int j = 0; j <= n; ++j) {
k = dp[y][j];
k = max(k, dp[y][j + 1]);
if (j)
k = max(k, dp[y][j - 1]);
dp[x][j] += k;
}
}
}
void Main() {
// for(i = 1; i <= n; ++i) ans += val[n - 1] - w[i];
memset(dp, 0x80, sizeof(dp));
for (i = 1; i < n; ++i) {
u = read();
v = read();
G[u].pb(v);
G[v].pb(u);
}
dfs(1, 0);
// assert(sum == n);
for (i = 0; i <= n; ++i) {
// debug("%lld : %lld\n", i, dp[1][i]);
Mx(ans, dp[1][i]);
}
printf("%lld", ans);
}
} // namespace Sol3

signed main() {
freopen("duck.in", "r", stdin);
freopen("duck.out", "w", stdout);
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n = read();
for (i = 1; i <= n; ++i) w[i] = read();
for (i = 0; i < n; ++i) val[i] = read();
// if(n <= 20) return Sol1 :: Main(), 0;
Sol3 ::Main();
// Sol2 :: Main();
return 0;
}