题解:P10974 Accumulation Degree
ノテンシ
·
·
题解
题目描述
我们这么定义树的累计度 A(x):
- 树的每一条边都有一个正容量。
- 树中度为 1 的节点被称为终端节点。
- 每条边的流量不能超过其容量。
-
树的累积度是指其节点中最大累积度的值。你的任务是找到给定树的累积度。
输入输出样例
输入
1
5
1 2 11
1 4 13
3 4 5
4 5 10
输出
26
说明/提示
原题中没有提到的数据范围:T \le 4,\sum n \le 2\times 10 ^ 5。
本题所要用到的知识点
- 树形动态规划(树形 DP),建议先做一下 P1352 没有上司的舞会 再来看这篇题解;
- 二次扫描与换根法。
前言
讲一下蓝书的做法。。。
思路
通过读题,我们很容易发现这是一道“不定根”的树形动态规划题,我们采用“二次扫描与换根法”来做。
首先,我们考虑朴素解法:枚举源点,然后计算水系流量。把源点作为树根,整个水系就从一颗无根树(n 个点、n - 1 条边的无向连通图)变成了一颗有根树(请自行画图)。这样,子节点只能从父节点获得水源,然后流向自己的子节点。每个节点的“流域”都是以该节点为根的子树。我们发现,这样非常符合树形动态规划的应用场景(每棵树都是一个子问题)。
设 D_s[x] 表示在以 x 为根的子树中,把 x 作为源点,从 x 出发流向子树的流量最大是多少。
\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}
对于枚举的每个源点 x,可以用树形动态规划在 \Theta(n) 的时间内求出 D_s 数组,并用 D_s[x] 更新答案。所以,最终的时间复杂度为 \Theta(n^2)。下面的代码给出了一次树形动态规划的过程。在主函数中调用 dynamic\_programming(s),完成后 d[x] 就是所求的 D_s[s]。代码:
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;
}
考虑用“二次扫描与换根法”代替源点的枚举,可以在 \Theta(n) 的时间内解决整个问题。首先,我们任选一个源点作为根节点,记为 root,然后采用上面的代码进行一次树形动态规划,求出 D_{root} 数组,下文简写为:D 数组。
设 dp[x] 表示把 x 作为源点,流向整个水系,流量的最大值。那初始条件是什么?显然,对于根节点 root,显然有 dp[root]=D[root]。
假设 dp[x] 已经被正确求出了,我们考虑它的子节点 y,dp[y] 尚未计算。dp[y] 包含了两部分(请自行理解并画图):
- 从 y 流向以 y 为根的子树的流量,已经计算并保存在 D[y] 中;
- 从 y 沿着到父节点 x 的河道,进而流向水系中其它部分的流量。
因为把 x 作为源点的总流量为 dp[x],从 x 流向 y 的流量为 min(D[y], c(x,y)),所以从 x 流向除了 y 以外的其它部分的流量就是二者之差。于是,把 y 作为源点,先流到 x,再流向其它部分的流量就是把这个“差”再与 c(x,y) 取最小值后的结果。
当然,对于度数为一的节点 x,需要进行特判。
dp[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}
```cpp
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;
}
```
到此,整片题解就结束了,最后附上完整代码:
```cpp
#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;
}
```
# 后记
有问题留言或私信问我。