Solution -「洛谷 P4719」「模板」"动态 DP" & 动态树分治
\(\mathcal{Description}\)
Link.
给定一棵 \(n\) 个结点的带权树,\(m\) 次单点点权修改,求出每次修改后的带权最大独立集。
\(n,m\le10^5\)。
\(\mathcal{Solution}\)
不考虑修改,显然 DP。令 \(f(u,0/1)\) 表示选 / 不选结点 \(u\),\(u\) 子树内的带权最大独立集。那么:
\[\begin{cases}f(u,0)=\sum_v\max\{f(v,0),f(v,1)\}\\f(u,1)=\sum_vf(v,0)+a_u\end{cases}
\]
引入修改,我们自然想用数据结构维护转移。那么就需要进行树链剖分(不一定是重链剖分,这里以复杂度更小的 LCT 为例)。假设 \(u\) 的实儿子是 \(s_u\),我们单独维护 \(s_u\) 的贡献,而把其它儿子一起考虑。设 \(g(u,0/1)\) 表示选 / 不选结点 \(u\),结点 \(u\) 及其虚儿子的子树们的带权最大独立集。有:
\[\begin{cases}g(u,0)=\sum_{v\not=s_u}\max\{f(v,0),f(v,1)\}\\g(u,1)=\sum_{v\not=s_u}f(v,0)+a_u\end{cases}
\]
用它来表示 \(f\):
\[\begin{cases}f(u,0)=g(u,0)+\max\{f(s_u,0),f(s_u,1)\}\\f(u,1)=g(u,1)+f(s_u,0)\end{cases}
\]
写成矩乘:
\[\begin{bmatrix}f(u,0)\\f(u,1)\end{bmatrix}=\begin{bmatrix}g(u,0)&g(u,0)\\g(u,1)&-\infty\end{bmatrix}\begin{bmatrix}f(s_u,0)\\f(s_u,1)\end{bmatrix}
\]
令 \(G_u=\begin{bmatrix}g(u,0)&g(u,0)\\g(u,1)&-\infty\end{bmatrix}\),每次修改,仅会影响 \(\mathcal O(\log n)\) 个链头,也即是 \(\mathcal O(\log n)\) 个虚实交替的位置的 \(G\) 需要修改,就可以维护了。
考虑到本题的 LCT 并没有“动”,其实就不需要打翻转标记。仅在 access
操作时虚实儿子会发生变化(ch[x][1]
变虚,y
变实),那么直接在当前结点的矩阵除去 y
的贡献,加上 ch[x][1]
的贡献即可。初始时,只初始化 fa[]
的信息,这样所有边都是虚边,\(f(u,0/1)=g(u,0/1)\),做一遍无修改的 DP 把初始的矩阵挂到树上就行啦。
\(\mathcal{Code}\)
#include <cstdio>
#include <cstring>
#include <assert.h>
const int MAXN = 1e5, NINF = 0xc0c0c0c0;
int n, m, ecnt, a[MAXN + 5], head[MAXN + 5], f[MAXN + 5][2];
int fa[MAXN + 5], ch[MAXN + 5][2];
inline int max_ ( const int a, const int b ) { return a < b ? b : a; }
inline char fgc () {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread ( p = buf, 1, 1 << 17, stdin ), p == q )
? EOF : *p ++;
}
inline int rint () {
int x = 0, f = 1; char s = fgc ();
for ( ; s < '0' || '9' < s; s = fgc () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = fgc () ) x = x * 10 + ( s ^ '0' );
return x * f;
}
inline void wint ( int x ) {
if ( x < 0 ) putchar ( '-' ), x = -x;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
struct Edge { int to, nxt; } graph[MAXN * 2 + 5];
struct Matrix {
int n, m, mat[2][2];
Matrix () {}
Matrix ( const int tn, const int tm ): n ( tn ), m ( tm ), mat {} {}
inline int* operator [] ( const int key ) { return mat[key]; }
inline Matrix operator * ( Matrix t ) {
assert ( m == t.n );
Matrix ret ( n, t.m ); memset ( ret.mat, 0xc0, sizeof ret.mat );
for ( int i = 0; i < n; ++ i ) {
for ( int k = 0; k < m; ++ k ) {
for ( int j = 0; j < t.m; ++ j ) {
ret[i][j] = max_ ( ret[i][j], mat[i][k] + t[k][j] );
}
}
}
return ret;
}
} G[MAXN + 5], S[MAXN + 5];
inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
}
inline bool nroot ( const int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
inline void pushup ( const int x ) {
S[x] = G[x];
if ( ch[x][0] ) S[x] = S[ch[x][0]] * S[x];
if ( ch[x][1] ) S[x] = S[x] * S[ch[x][1]];
}
inline void rotate ( const int x ) {
int y = fa[x], z = fa[y], k = ch[y][1] == x;
fa[x] = z; if ( nroot ( y ) ) ch[z][ch[z][1] == y] = x;
ch[y][k] = ch[x][k ^ 1]; if ( ch[x][k ^ 1] ) fa[ch[x][k ^ 1]] = y;
pushup ( ch[fa[y] = x][k ^ 1] = y ), pushup ( x );
}
inline void splay ( const int x ) {
for ( int y; nroot ( x ); rotate ( x ) ) {
if ( nroot ( y = fa[x] ) ) {
rotate ( x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y );
}
}
}
inline void access ( int x ) {
for ( int y = 0; x; x = fa[y = x] ) {
splay ( x );
if ( ch[x][1] ) {
G[x][0][0] += max_ ( S[ch[x][1]][0][0], S[ch[x][1]][1][0] );
G[x][1][0] += S[ch[x][1]][0][0];
}
if ( y ) {
G[x][0][0] -= max_ ( S[y][0][0], S[y][1][0] );
G[x][1][0] -= S[y][0][0];
}
G[x][0][1] = G[x][0][0];
ch[x][1] = y, pushup ( x );
}
}
inline void initDP ( const int u, const int fath ) {
fa[u] = fath, f[u][1] = a[u], G[u] = Matrix ( 2, 2 );
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fath ) {
initDP ( v, u );
f[u][0] += max_ ( f[v][0], f[v][1] );
f[u][1] += f[v][0];
}
}
G[u][0][0] = G[u][0][1] = f[u][0];
G[u][1][0] = f[u][1], G[u][1][1] = NINF;
S[u] = G[u];
}
int main () {
n = rint (), m = rint ();
for ( int i = 1; i <= n; ++ i ) a[i] = rint ();
for ( int i = 1, u, v; i < n; ++ i ) {
u = rint (), v = rint ();
link ( u, v ), link ( v, u );
}
initDP ( 1, 0 );
for ( int x, y; m --; ) {
x = rint (), y = rint ();
access ( x ), splay ( x );
G[x][1][0] += y - a[x], a[x] = y;
pushup ( x ), splay ( 1 );
wint ( max_ ( S[1][0][0], S[1][1][0] ) ), putchar ( '\n' );
}
return 0;
}