跳到正文

题解

Bro 懂我的在树上 dp 树状数组进行树链剖分

clx201022约 19 分钟
原文

这是 2025年12月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年12月5日
收录
2025年12月12日
更新
2026年9月16日
篇幅
约 19 分钟
题目
P14637

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

点我。

贪心

首先我们可以在 eps 秒内想出一个贪心,即对于每个点 uu 填等于 max⁡v∈sonumex(Sv)\max_{v\in son_u} mex(S_v) 的数就可以让 mexu=max⁡v∈sonumex(Sv)+1mex_u=\max_{v\in son_u} mex(S_v)+1。但是这个贪心正确性是错的,交上去只有 24 分。

DP

考虑贪心为什么是错的。

容易发现,每个点并不只有为了自己考虑这个选项。
看到样例:

其中,点 5 可以不为自己考虑,转而为上面的祖先考虑,奉献自己让祖先的 mexmex 变高。

设 dpi,j,kdp_{i,j,k} 为点 ii 在 mex(Si)=jmex(S_i)=j,并且还有 kk 个点为上面的祖先考虑时的最大总收益。但是复杂度 O(n3)O(n^3),明显过不了。

树链剖分

(红边为重边,蓝边为轻边)1

集中注意力,可以注意到每个点 uu 的 mex(Su)←mex(Sv)+k(v∈sonu)mex(S_u)\gets mex(S_v)+k(v\in son_u),其中 kk 为点 uu 子树(包括自身)中所有为 uu 做贡献的节点数。我们将这个最大的儿子列为重儿子,而那条边为重边,由重边连成的树链叫重链。

因此,可以将状态改成枚举树链剖分的结构。列出状态 dpi,j,kdp_{i,j,k} 表示节点 ii,位于当前链的第 kk 个节点,在到根节点的路径上最长的重链为 jj 的最大总价值。状态转移时将 jj 与 kk 取 max⁡\max 即可,时间复杂度为 O(nm2)O(nm^2)。

正确性证明

下面是笔者的个人见解,不保证严谨,欢迎各位在讨论区进行讨论。

点 uu 的 mexmex 就是 uu 子树所有点所含有的全值的集合的补集的最小值。对于它最大的子节点 vv 的 mexmex,其一定包含了所有较小的数,但更大的数就需要从其他的 sonu≠vson_u\ne v 中的子树中的点或 uu 自身中取到,也就是说,uu 将重儿子指定为其 mex(Sv)mex(S_v) 最大的 vv 是最优的。

对于一个点贡献的计算,我们可以这样考虑:它要么使当前子树 mexmex 变大,要么使一个祖先节点 mexmex 变大。而一旦一个重儿子 mexmex 变大,其就会影响其父亲,但轻儿子不然。

如果这个点的贡献使其一个祖先非重儿子 mexmex 最大,那么就说明当前的结构是更劣的,必然存在一个更优的树链剖分。

如果这个点为自己做贡献,mex(Su)←mex(Sv)+k(v∈sonu)mex(S_u)\gets mex(S_v)+k(v\in son_u),否则,k=0k=0 ,即它自身的 mexmex 必然为其最大的子节点的 mexmex。对于最优的树链剖分,其重儿子即为 mexmex 最大的子节点。

因此,每个点所能做出的最大贡献就取决于其到根节点的路径上最长的一条重链的长度,只需要贡献到重链底部的节点,即可得到等同于重链长度的价值。

转移方程
dpi,j,k←j+max⁡v∈son(i)(dpv,max⁡(j,k+1),k+1+∑v′∈son(i)v′≠vdpv′,j,1)dp_{i,j,k}\gets j+\max_{v\in son(i)}(dp_{v,\max(j,k+1),k+1}+\sum_{v'\in son(i)}^{v'\ne v}dp_{v',j,1})

优化

存在一种使用指针与长链剖分的 O(nm)O(nm) 优化2,但我不会。所以考虑像通常的树链剖分那样用线性数据结构 O(log⁡n)O(\log n) 地维护数据。

设 fi,jf_{i,j} gi,jg_{i,j} 分别为点 ii 贡献的重链长度为 jj 时的最大价值,加上树状数组即可实现 O(log⁡n)O(\log n) 级别的转移。

具体转移见代码,码风优良,可清晰看出转移路线。

代码

笔者认为最易理解的代码3:

Accepted
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using ll = long long;
const int maxn = 8001, maxm = 801;
template <typename data = int>
class Binary_Index_Tree
{
private:
    std::vector<data> BIT;

public:
    void reset()
    {
        std::fill(BIT.begin(), BIT.end(), 0);
    }
    unsigned lowbit(const unsigned x) const
    {
        return x & (-x);
    }
    Binary_Index_Tree(int x = 0, data y = data())
    {
        BIT = std::vector<data>(x, y);
    }
    void kuorong(unsigned x)
    {
        if (x <= BIT.size())
            return;
        const auto old_size = BIT.size();
        BIT.resize(x);
        const auto new_size = BIT.size();
        for (unsigned i = old_size; i < new_size; i++)
        {
            BIT[i] = query(i - 1) - query(i - lowbit(i));
        }
        return;
    }
    data query(int x)
    {
        kuorong(x + 1);
        data wer(0);
        for (; x > 0; x -= lowbit(x))
        {
            wer += BIT[x];
        }
        return wer;
    }
    void add(unsigned x, const data d)
    {
        kuorong(x + 1);
        for (; x != 0 && x < BIT.size(); x += lowbit(x))
        {
            BIT[x] += d;
        }
    }
};
int n, m, p[maxn + 1], siz[maxn + 1], depth[maxn + 1];
vector<int> son[maxn + 1]; // 儿子
int dfn[maxn + 1], df_lst[maxn + 1], ni_dfn[maxn + 1], now_dfn;
void dfs_gets(int u) // 预处理
{
    siz[u] = 1;
    ni_dfn[dfn[u] = ++now_dfn] = u;
    for (const int &v : son[u])
    {
        depth[v] = depth[u] + 1;
        dfs_gets(v);
        siz[u] += siz[v];
    }
    df_lst[u] = now_dfn;
}
Binary_Index_Tree<> e[maxm + 1];
int f[maxn + 1][maxm + 1], g[maxn + 1][maxm + 1];
template <class T>
T &qvmax(T &x, T y)
{
    return x = max(x, y);
}
void dfs_dp(int u)
{
    for (int v : son[u])
        dfs_dp(v);
    for (int h = 1; h <= depth[u] + 1; h++)
    {
        f[u][h] = g[u][h] = h;
        for (int v : son[u])
            f[u][h] += f[v][h];
        for (int v : son[u])
        {
            qvmax(g[u][h], f[u][h] - f[v][h] + g[v][h + 1]);
            e[h].add(dfn[v], f[u][h] - f[v][h]);
            e[h].add(df_lst[v] + 1, f[v][h] - f[u][h]);
        }
    }
    for (int i = dfn[u] + 1; i <= df_lst[u]; i++)
    {
        const int &vv = ni_dfn[i];
        const int j = depth[vv] - depth[u];
        qvmax(f[u][j], g[vv][j + 1] + e[j].query(i));
    }
}
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        son[i].clear();
    }
    for (int j = 1; j <= m + 1; j++)
    {
        e[j].reset();
    }
    p[1] = 0;
    for (int i = 2; i <= n; i++)
    {
        cin >> p[i];
        son[p[i]].push_back(i); // 直属儿子
    }
    now_dfn = 0;
    depth[1] = 0;
    dfs_gets(1);
    dfs_dp(1);
    cout << f[1][1] << endl;
}
#define not_fio
int main()
{
#ifndef not_fio
    freopen("tree.in", "r", stdin);
    freopen("tree.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
// g++ tree.cpp -o tree -O2 -std=c++14 -static

基于树状数组上再封装一层的代码4:

Accepted
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using ll = long long;
const int maxn = 8001, maxm = 801;
template <typename data = int>
class Binary_Index_Tree
{
private:
    std::vector<data> BIT;

public:
    void reset()
    {
        std::fill(BIT.begin(), BIT.end(), 0);
    }
    unsigned lowbit(const unsigned x) const
    {
        return x & (-x);
    }
    Binary_Index_Tree(int x = 0, data y = data())
    {
        BIT = std::vector<data>(x, y);
    }
    void kuorong(unsigned x)
    {
        /*
        if (x <= BIT.size())
            return;
        const auto old_size = BIT.size();
        BIT.resize(x);
        const auto new_size = BIT.size();
        for (unsigned i = old_size; i < new_size; i++)
        {
            BIT[i] = query(i - 1) - query(i - lowbit(i));
        }
        return;
        */
    }
    data query(int x)
    {
        kuorong(x + 1);
        data wer(0);
        for (; x > 0; x -= lowbit(x))
        {
            wer += BIT[x];
        }
        return wer;
    }
    void add(unsigned x, const data d)
    {
        kuorong(x + 1);
        for (; x != 0 && x < BIT.size(); x += lowbit(x))
        {
            BIT[x] += d;
        }
    }
};
int n, m, p[maxn + 1], siz[maxn + 1], depth[maxn + 1];
vector<int> son[maxn + 1][maxm + 1]; // 与 i 距离 j 的儿子 0 为自身
int dfn[maxn + 1], now_dfn;
void dfs_gets(int u) // 预处理
{
    dfn[u] = ++now_dfn;
    siz[u] = 1;
    for (const int &v : son[u][1])
    {
        depth[v] = depth[u] + 1;
        dfs_gets(v);
        siz[u] += siz[v];
    }
}
struct tree_mao
{
    Binary_Index_Tree<> b0, b1;
    tree_mao() : b0(maxn + 1, 0), b1(maxn + 1, 0) {}
    void reset()
    {
        b0.reset();
        b1.reset();
    }
    void add_point(int index, int d)
    {
        b0.add(dfn[index], d);
        b0.add(dfn[index] + siz[index], -d);
        if (p[index] != 0)
        {
            b1.add(dfn[p[index]], d);
            b1.add(dfn[p[index]] + siz[p[index]], -d);
        }
    }
    int query_chain_0(int start_tr, int end_tr)
    {
        return b0.query(dfn[start_tr]) - b0.query(dfn[end_tr]);
    }
    int query_chain_1(int start_tr, int end_tr)
    {
        return b1.query(dfn[start_tr]) - b1.query(dfn[end_tr]);
    }
} mao[maxm + 1];
int f[maxn + 1][maxm + 1], g[maxn + 1][maxm + 1], subtree_h[maxn + 1];
void tree_dp(int u)
{
    subtree_h[u] = 0;
    son[u][0].push_back(u);
    for (int v : son[u][1])
    {
        tree_dp(v);
        subtree_h[u] = max(subtree_h[u], subtree_h[v] + 1);
        for (int j = 1; j <= subtree_h[v]; j++) // 至少往下一层吧
            for (int vv : son[v][j])
                son[u][j + 1].push_back(vv);
    }
    for (int v : son[u][1])
    {
        for (int i = 1; i <= depth[u] + 1; i++)
            mao[i].add_point(v, f[v][i]);
    }
    for (int i = 1; i <= depth[u] + 1; i++)
    {
        f[u][i] = siz[u] * i;
        for (int v : son[u][i])
        {
            int cur = g[v][i + 1];
            cur += mao[i].query_chain_1(p[v], 0);
            cur -= mao[i].query_chain_0(v, 0);
            f[u][i] = max(f[u][i], cur + i * i);
        }
    }
    for (int i = 1; i <= depth[u] + 1; i++)
    {
        int delta = 0;
        for (int v : son[u][1])
        {
            g[u][i] += f[v][i];
            delta = max(delta, g[v][i + 1] - f[v][i]);
        }
        g[u][i] += i;
        g[u][i] += delta;
    }
}

void solve()
{
    memset(f, 0, sizeof f);
    memset(g, 0, sizeof g);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= subtree_h[i]; j++)
            son[i][j].clear();
    }
    for (int i = 0; i <= m; i++)
        mao[i].reset();
    p[1] = 0;
    for (int i = 2; i <= n; i++)
    {
        cin >> p[i];
        son[p[i]][1].push_back(i); // 直属儿子
    }
    depth[1] = 0;
    now_dfn = 0;
    dfs_gets(1);
    tree_dp(1);
    cout << f[1][1] << endl;
}
#define not_fio
int main()
{
#ifndef not_fio
    freopen("tree.in", "r", stdin);
    freopen("tree.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
// g++ tree.cpp -o tree -O2 -std=c++14 -static

第一篇黑题题解,点个赞谢谢喵!

脚注

  1. https://www.luogu.com.cn/article/pm7c9dmt 回到正文↩

  2. https://www.luogu.com.cn/article/h8dbshy5 回到正文↩

  3. https://www.luogu.com.cn/article/wlclboc8 回到正文↩

  4. https://www.luogu.com.cn/article/0gur8cs2 回到正文↩