动态DP 学习笔记

· · 算法·理论

动态DP 是什么

动态 DP 问题是猫锟在 WC2018 讲的黑科技,一般用来解决树上的带有点权(边权)修改操作的 DP 问题.——OIwiki

前置芝士

矩阵、广义矩阵乘法、线段树、树链剖分

关于广义矩阵乘法

定义广义矩阵乘法 A \times B = C 为

C_{i,j} = \max_{k=1}^{n} (A_{i,k} + B_{k,j})

可以证明广义矩阵乘法具有结合律,可以用快速幂、线段树等快速维护。

关于广义矩阵乘法结合律的证明

A(BC)_{i,j}=\max_{k=1}^{n} (A_{i,k} + \max_{k'=1}^{n} (B_{k,k'} + C_{k',j}))=\max_{k=1}^{n} \max_{k'=1}^{n} (A_{i,k} + B_{k,k'} + C_{k',j}) \\ (AB)C_{i,j}=\max_{k=1}^{n} (\max_{k'=1}^{n} (A_{i,k'} + B_{k',k}) + C_{k,j})=\max_{k=1}^{n} \max_{k'=1}^{n} (A_{i,k} + B_{k,k'} + C_{k',j})

所以有 A(BC)=(AB)C。

请注意,该广义矩阵乘法是不满足交换律的,所以在下文中请注意合并顺序是从 dfn 大的向小的合并。

算法讲解

例题1 P4719 【模板】动态 DP

给定一棵 n 个点的树,点带点权。

有 m 次操作,每次操作给定 x,y,表示修改点 x 的权值为 y。

你需要在每次操作之后求出这棵树的最大权独立集的权值大小。

保证 1\le n,m\le 10^5,1 \leq u, v , x \leq n,-10^2 \leq a_i, y \leq 10^2。

规定下文对于矩阵 f,f_{x,y} 代表该矩阵第 x 行,第 y 列的元素值。

首先,先考虑暴力 DP 怎么做,记 f_{i,0/1} 为 i 号点不选/选时,以 i 为根的子树最大权独立集的权值大小,令 son_i 为 i 的儿子集合,w_i 为 i 的权值,则有转移式:

f_{i,0}=\sum_{j \in son_i}{\max(f_{j,0},f_{j,1})} \\ f_{i,1}=\sum_{j \in son_i}{f_{j,0}} + w_i

单次复杂度是 O(n),可以解决不带修的问题,但本题丧心病狂地带上了修改操作,此时如果暴力重跑时间复杂度是 O(nm) 的,是无法通过 1 \leq n,m \leq 10^5 的数据的。

考虑如何优化,对于树肯定是不好操作的,我们考虑转化成链,直接使用树链剖分把树拍成若干条链。

首先先把转移式变成更方便转移的形式,定义 g_{i,0/1} 为只考虑轻儿子,i 号点不选/选时,以 i 为根的子树最大权独立集的权值大小,这样我们就可以把重儿子单独拎出来操作。记重儿子编号为 s,此时的转移式变为:

f_{i,0}=g_{i,0} + \max(f_{s,0},f_{s,1}) \\ f_{i,1}=g_{i,1} + f_{s,0}

发现可以使用广义矩阵乘法化为矩阵形式:

\begin{bmatrix} f_{s,0} & f_{s,1} \end{bmatrix} \times \begin{bmatrix} g_{i,0} & g_{i,1} \\ g_{i,0} & -\infty \end{bmatrix} = \begin{bmatrix} f_{i,0} & f_{i,1} \end{bmatrix}

记 i 的轻儿子集合为 son'_{i},g 的转移为:

g_{i,0}=\sum_{j \in son'_i}{\max(f_{j,0},f_{j,1})} \\ g_{i,1}=\sum_{j \in son'_i}{f_{j,0}} + w_i

可以发现,每次修改操作本质改的是 x 的 g,这时,对于修改的点 x,我们只需要:

所以,我们可以知道,最后的时间复杂度是 O(n+k^3m \log^2 n),其中 k 是转移矩阵大小,由于 k=2 所以其实可以把 k^3 当常数算,该复杂度可以通过本题。

但是,如果数据范围再大点呢?

例题2 P4751 【模板】动态 DP(加强版)

给定一棵 n 个点的树,点带点权。

有 m 次操作,每次操作给定 x,y,表示修改点 x 的权值为 y。

你需要在每次操作之后求出这棵树的最大权独立集的权值大小。

数据范围 n \leq 1 \times 10^6,m \leq 3 \times 10^6。保证任意时刻各点点权的绝对值 \leq 100。

这时,O(n+k^3m \log^2 n) 已经难以通过本题了,我们需要更快的算法。

考虑树链剖分常见优化:全局平衡二叉树,即以类似 LCT 的方式,对每条链建一颗二叉树并将这些二叉树用虚边连接起来。

具体的,定义:

Lsize_u = 1 + |son'_u|

取重链的 Lsize 带权中点作为父亲,递归构造全局平衡二叉树,可以证明树高的量级是 O(\log n) 的,具体证明详见关于 Splay 复杂度的证明。

之后,对于每条链,构造完二叉树后枚举每个节点递归每个轻儿子继续构造并将其父亲设为自己。

时间复杂度是 O(n+k^3m \log n)。

代码如下:

//code by xuanxuanmeow
#include <bits/stdc++.h>
using namespace std;
#define l(u) tree[u].ls
#define r(u) tree[u].rs
const int N = 1e6 + 5;
const int M = 3e6 + 5;
const int K = 2;
const int inf = 0x3f3f3f3f;
struct matrix{
    int n,m,s[K + 1][K + 1];
    matrix() {
        n = m = 2;
        memset(s,-0x3f,sizeof(s));
    }
    inline matrix operator * (const matrix x) const{
        matrix res;
        res.n = n,res.m = x.m;
        for (int i = 1;i <= n;++i)
            for (int j = 1;j <= x.m;++j){
                res.s[i][j] = -inf;
                for (int k = 1;k <= K;++k) res.s[i][j] = max(res.s[i][j],s[i][k] + x.s[k][j]);
            }
        return res;
    }
};
struct node{
    int ls,rs,fa;
    matrix val,w;
}tree[N];
struct tree_node{
    int dfs_order,hnode,siz,top,fa,sizer,lsiz,f[2],g[2],tail;
}trees[N];
struct edge{
    int to,nxt;
}edges[M << 1];
int n,m,mp[N],head[N],w[N],tot,top,u,v,lstans,root,val[N];
inline void pushup(int u){
    if (!l(u) && !r(u)) tree[u].w = tree[u].val;
    else if (!l(u) && r(u)) tree[u].w = tree[r(u)].w * tree[u].val;
    else if (l(u) && !r(u)) tree[u].w = tree[u].val * tree[l(u)].w;
    else tree[u].w = tree[r(u)].w * tree[u].val * tree[l(u)].w;
}
inline void build(int &u,int l,int r){
    if (l > r) { u = 0; return; }
    int res = l;
    long long sum = val[r] - val[l - 1],lf = l,rf = r;
    while (lf <= rf){
        long long mid = (lf + rf) >> 1;
        if (((val[mid] - val[l - 1]) << 1) > sum) res = mid,rf = mid - 1;
        else lf = mid + 1;
    }
    u = mp[res];
    build(l(u),l,res - 1),build(r(u),res + 1,r);
    tree[l(u)].fa = tree[r(u)].fa = u,pushup(u);
}
inline void addedge(int u,int v){
    edges[++tot] = {v,head[u]},head[u] = tot;
}
inline void dfs1(int u,int fa){
    trees[u].siz = 1,trees[u].fa = fa;
    for (int i = head[u];i;i = edges[i].nxt){
        if (edges[i].to == fa) continue;
        dfs1(edges[i].to,u);
        trees[u].siz += trees[edges[i].to].siz;
        if (trees[edges[i].to].siz > trees[trees[u].hnode].siz) trees[u].hnode = edges[i].to;
    }
    trees[u].lsiz = trees[u].siz - trees[trees[u].hnode].siz;
}
inline void dfs2(int u,int toper){
    if (!u) return;
    trees[u].dfs_order = ++top,mp[top] = u,trees[u].top = toper,trees[trees[u].top].tail = max(trees[trees[u].top].tail,top),trees[u].g[1] += w[u];
    dfs2(trees[u].hnode,toper);
    for (int i = head[u];i;i = edges[i].nxt){
        if (edges[i].to == trees[u].fa || edges[i].to == trees[u].hnode) continue;
        dfs2(edges[i].to,edges[i].to);
        trees[u].g[0] += max(trees[edges[i].to].f[1],trees[edges[i].to].f[0]),trees[u].g[1] += trees[edges[i].to].f[0];
    }
    trees[u].f[0] = max(trees[trees[u].hnode].f[0],trees[trees[u].hnode].f[1]) + trees[u].g[0];
    trees[u].f[1] = trees[u].g[1] + trees[trees[u].hnode].f[0];
    tree[u].val.s[1][1] = tree[u].val.s[2][1] = trees[u].g[0];
    tree[u].val.s[1][2] = trees[u].g[1],tree[u].val.s[2][2] = -inf;
}
inline int builds(int u){
    int rt;
    build(rt,trees[u].dfs_order,trees[u].tail);
    for (int i = trees[u].dfs_order;i <= trees[u].tail;++i){
        int up = mp[i];
        for (int j = head[up];j;j = edges[j].nxt){
            if (edges[j].to == trees[up].fa) continue;
            tree[builds(edges[j].to)].fa = up;
        }
    }
    return rt;
}
inline long long modify(int u,int x){
    tree[u].val.s[1][2] += x - w[u],w[u] = x;
    while (u){
        int fa = tree[u].fa;
        if (fa && l(fa) != u && r(fa) != u){
            matrix a = tree[u].w;
            pushup(u);
            matrix b = tree[u].w;
            tree[fa].val.s[1][1] = tree[fa].val.s[2][1] = tree[fa].val.s[1][1] + max(b.s[1][1],b.s[1][2]) - max(a.s[1][1],a.s[1][2]);
            tree[fa].val.s[1][2] += b.s[1][1] - a.s[1][1];
        }else pushup(u);
        u = fa;
    }
    return max(tree[root].w.s[1][1],tree[root].w.s[1][2]);
}
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
char ch;
inline void read(int &x){
    x = 0;
    int f = 1;
    char ch = nc();
    while (ch < '0' || ch > '9'){
        if (ch == '-') f = -1;
        ch = nc();
    }
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48),ch = nc();
    x *= f;
}
int main(){
    read(n),read(m);
    for (int i = 1;i <= n;++i) read(w[i]);
    for (int i = 1;i < n;++i){
        read(u),read(v);
        addedge(u,v);
        addedge(v,u);
    }
    dfs1(1,0),dfs2(1,1);
    for (int i = 1;i <= n;++i) val[i] = val[i - 1] + trees[mp[i]].lsiz;
    root = builds(1);
    while (m--){
        read(u),read(v);
        u ^= lstans;
        cout << (lstans = modify(u,v)) << "\n";
    }
}