跳到正文

题解

题解:P10974 Accumulation Degree

ノテンシ约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年5月2日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 9 分钟
获赞
1
题目
P10974

版本

  1. v1当前

题目描述

我们这么定义树的累计度 A(x)A(x):

  • 树的每一条边都有一个正容量。
  • 树中度为 11 的节点被称为终端节点。
  • 每条边的流量不能超过其容量。
  • A(x)A(x) 是节点 xx 可以流向其他终端节点的最大流量。

树的累积度是指其节点中最大累积度的值。你的任务是找到给定树的累积度。

输入输出样例

输入

代码
1
5
1 2 11
1 4 13
3 4 5
4 5 10

输出

代码
26

说明/提示

原题中没有提到的数据范围:T≤4T \le 4,∑n≤2×105\sum n \le 2\times 10 ^ 5。

本题所要用到的知识点

  1. 树形动态规划(树形 DP),建议先做一下 P1352 没有上司的舞会 再来看这篇题解;
  2. 二次扫描与换根法。

前言

讲一下蓝书的做法。。。

思路

通过读题,我们很容易发现这是一道“不定根”的树形动态规划题,我们采用“二次扫描与换根法”来做。

首先,我们考虑朴素解法:枚举源点,然后计算水系流量。把源点作为树根,整个水系就从一颗无根树(nn 个点、n−1n - 1 条边的无向连通图)变成了一颗有根树(请自行画图)。这样,子节点只能从父节点获得水源,然后流向自己的子节点。每个节点的“流域”都是以该节点为根的子树。我们发现,这样非常符合树形动态规划的应用场景(每棵树都是一个子问题)。

设 Ds[x]D_s[x] 表示在以 xx 为根的子树中,把 xx 作为源点,从 xx 出发流向子树的流量最大是多少。

∑y∈Son(x){(Ds[y],c(x,y))⇔degy>1c(x,y)⇔degy=1\sum_{y\in Son(x) } \begin{cases} (D_s[y],c(x,y))\Leftrightarrow deg_y>1 \\ c(x,y)\Leftrightarrow deg_y=1 \end{cases}

对于枚举的每个源点 xx,可以用树形动态规划在 Θ(n)\Theta(n) 的时间内求出 DsD_s 数组,并用 Ds[x]D_s[x] 更新答案。所以,最终的时间复杂度为 Θ(n2)\Theta(n^2)。下面的代码给出了一次树形动态规划的过程。在主函数中调用 dynamic_programming(s)dynamic\_programming(s),完成后 d[x]d[x] 就是所求的 Ds[s]D_s[s]。代码:

C++
void dynamic_programming(int u)
{
    vis[u] = true; // 标记已访问
    for (int i = pre[u]; i; i = a[i].ne) // 链式前向星遍历
    {
        int to = a[i].to;
        if (vis[to]) continue; // 如果已访问,跳过
        dynamic_programming(to); // 递归
        // 回溯过程中计算d数组
        if (deg[to] == 1) d[u] += a[i].len;
        else d[u] += min(d[to], a[i].len);
    }
    return;
}

考虑用“二次扫描与换根法”代替源点的枚举,可以在 Θ(n)\Theta(n) 的时间内解决整个问题。首先,我们任选一个源点作为根节点,记为 rootroot,然后采用上面的代码进行一次树形动态规划,求出 DrootD_{root} 数组,下文简写为:DD 数组。

设 dp[x]dp[x] 表示把 xx 作为源点,流向整个水系,流量的最大值。那初始条件是什么?显然,对于根节点 rootroot,显然有 dp[root]=D[root]dp[root]=D[root]。

假设 dp[x]dp[x] 已经被正确求出了,我们考虑它的子节点 yy,dp[y]dp[y] 尚未计算。dp[y]dp[y] 包含了两部分(请自行理解并画图):

  1. 从 yy 流向以 yy 为根的子树的流量,已经计算并保存在 D[y]D[y] 中;
  2. 从 yy 沿着到父节点 xx 的河道,进而流向水系中其它部分的流量。

因为把 xx 作为源点的总流量为 dp[x]dp[x],从 xx 流向 yy 的流量为 min(D[y],c(x,y))min(D[y], c(x,y)),所以从 xx 流向除了 yy 以外的其它部分的流量就是二者之差。于是,把 yy 作为源点,先流到 xx,再流向其它部分的流量就是把这个“差”再与 c(x,y)c(x,y) 取最小值后的结果。

当然,对于度数为一的节点 xx,需要进行特判。

dp[y]=D[y]+{min(dp[x]−min(D[y],c(x,y)),c(x,y))⇔degx>1,degy>1min(dp[x]−c(x,y),c(x,y))⇔degx>1,degy=1c(x,y)⇔degx=1dp[y]=D[y]+\begin{cases} min(dp[x] - min(D[y],c(x,y)),c(x,y))\Leftrightarrow deg_x>1,deg_y>1 \\min(dp[x] - c(x,y),c(x,y))\Leftrightarrow deg_x>1,deg_y=1 \\c(x,y)\Leftrightarrow deg_x=1 \end{cases}

dp[y]dp[y] 就是把源点(树根)从 xx 换成 yy 后,流量的计算结果。这是一个自上而下的递推方程,通过一次深度优先遍历即可实现。代码:

C++
void dfs(int u)
{
    vis[u] = true; // 标记已访问
    for (int i = pre[u]; i; i = a[i].ne) // 链式前向星遍历
    {
        int to = a[i].to;
        if (vis[to]) continue; // 如果已访问过,跳过
        // 接下来按照方程里的去实现
        if (deg[u] == 1) dp[to] = d[to] + a[i].len;
        else if (deg[to] == 1) dp[to] = d[to] + min(dp[u] - a[i].len, a[i].len);
        else dp[to] = d[to] + min(dp[u] - min(d[to], a[i].len), a[i].len);
        dfs(to); // 递归
    }
    return;
}

到此,整片题解就结束了,最后附上完整代码:

C++
#include <bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int T, n, k = 0, pre[N], dp[N], d[N], deg[N]; bool vis[N];
struct Edge { int to, ne, len; } a[N << 1];
void add(int u, int v, int w) { a[++k] = (Edge){v, pre[u], w}; pre[u] = k; }
void dynamic_programming(int u)
{
    vis[u] = true;
    for (int i = pre[u]; i; i = a[i].ne)
    {
        int to = a[i].to;
        if (vis[to]) continue;
        dynamic_programming(to);
        if (deg[to] == 1) d[u] += a[i].len;
        else d[u] += min(d[to], a[i].len);
    }
    return;
}
void dfs(int u)
{
    vis[u] = true;
    for (int i = pre[u]; i; i = a[i].ne)
    {
        int to = a[i].to;
        if (vis[to]) continue;
        if (deg[u] == 1) dp[to] = d[to] + a[i].len;
        else if (deg[to] == 1) dp[to] = d[to] + min(dp[u] - a[i].len, a[i].len);
        else dp[to] = d[to] + min(dp[u] - min(d[to], a[i].len), a[i].len);
        dfs(to);
    }
    return;
}
signed main()
{
    cin >> T;
    while (T--)
    {
        k = 0;
        memset(vis, 0, sizeof vis); memset(dp, 0, sizeof dp);
        memset(d, 0, sizeof d); memset(deg, 0, sizeof deg);
        memset(pre, 0, sizeof pre);
        cin >> n;
        for (int i = 1, x, y, z; i < n; i++)
        {
            cin >> x >> y >> z;
            deg[x]++, deg[y]++;
            add(x, y, z); add(y, x, z);
        }
        int root = 1;
        dynamic_programming(root);
        memset(vis, 0, sizeof vis);
        dp[root] = d[root]; dfs(root);
        int ans = 0;
        for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
        cout << ans << endl;
    }
    return 0;
}

后记

有问题留言或私信问我。

原文