跳到正文

题解

题解:P10962 Computer

P2441M约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月10日
篇幅
约 6 分钟
获赞
1
题目
P10962

版本

  1. v1当前

经典题,来写写题解。

原题链接

题意简述

给定一棵树,对于每个节点,输出它能到达的最远的距离。

题解

显然是换根 DP,但并不是那么好想。

钦定 11 号节点为根。一个节点 uu 走到最远的距离只有两种情况:

  1. 一直向下走,即路径在 subtree⁡(u)\operatorname{subtree}(u) 内;
  2. 先向上走,再向下走到 subtree⁡(u)\operatorname{subtree}(u) 之外的地方(下面直接称为向上走)。

对于第一种情况,我们容易通过一次 DFS 树形 DP 求出。

对于第二种情况,考虑使用换根 DP 求出每个节点向上走的最远距离。当从 uu 转移到它的某个子节点 vv 时,设它们间的边权为 ww,我们对 vv 向上向下走的拐点进行讨论:

  1. 若拐点为 uu 的祖先,则这种情况已经在计算 uu 时被覆盖了,直接从 vv 走到 uu,再累加上 uu 的向上走的最远距离;
  2. 若拐点就是 uu,考虑令 uu 取到向下的最远距离的是哪个子树。
    • 如果不是 vv 所在的子树,那么最长的路径必然是从 vv 走到 uu 后,在从 uu 向下走最长路;
    • 如果恰好是 vv 所在的子树,那么走次长路即可。次长路可以在第一次树形 DP 时被求出。

根据上述讨论,我们可以设计出这样的状态:设 fu,0/1f_{u,0/1} 表示从节点 uu 向下走能达到的最长路/次长路的长度。gug_{u} 表示令 uu 取到向下的最远距离的子节点编号。fu,2f_{u,2} 表示节点 uu 向上走能达到的最长路的长度。

对于第一次 DFS,转移是简单的:

∀v∈son⁡(u), {fu,1←fu,0, fu,0←fv,0+w, gu=vif fu,0<fv,0+wfu,1←max⁡(fu,1,fv,0+w)otherwise\forall v\in\operatorname{son}(u),\ \begin{cases} f_{u,1}\leftarrow f_{u,0},\ f_{u,0}\leftarrow f_{v,0}+w,\ g_{u}=v &{\text{if}}\ f_{u,0}<f_{v,0}+w \\ f_{u,1}\leftarrow \max(f_{u,1},f_{v,0}+w) &{\text{otherwise}} \end{cases}

对于第二次 DFS,我们按照上述讨论进行转移即可:

∀v∈son⁡(u), fv,2←max⁡(fu,2,fu,[gu=v])+w\forall v\in\operatorname{son}(u),\ f_{v,2}\leftarrow \max(f_{u,2},f_{u,[g_u=v]})+w

统计答案时,显然有 ansu=max⁡(fu,0,fu,2)ans_u=\max(f_{u,0},f_{u,2})。

于是我们就轻松解决了本题,时间复杂度为 O(n+m)O(n+m)。

代码

C++
// Problem: Computer
// Contest: HDOJ
// URL: https://acm.hdu.edu.cn/showproblem.php?pid=2196
// Memory Limit: 32 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstring>

using namespace std;

#define lowbit(x) ((x) & -(x))
#define add_mod(x, v) (x) = ((ll)(x) + (v)) % MOD
#define mul_mod(x, v) (x) = (1ll * (x) * (v)) % MOD
#define sub_mod(x, v) (x) = (((ll)(x) - (v)) % MOD + MOD) % MOD
typedef long long ll;
typedef pair<int, int> pii;
const int MAX_N = 1e4 + 5, MAX_M = 1e4 + 5;

int n, f[MAX_N][3], g[MAX_N];

struct AdjList {
    int tot, head[MAX_N], nxt[MAX_M << 1], to[MAX_M << 1], edge[MAX_M << 1];

    void init() {
        tot = 0;
        for (int i = 1; i <= n; ++i) head[i] = -1;
    }

    void insert(int x, int y, int z) {
        to[++tot] = y;
        edge[tot] = z;
        nxt[tot] = head[x];
        head[x] = tot;
    }
} t;

void dfs1(int x, int fx) {
    for (int i = t.head[x]; ~i; i = t.nxt[i]) {
        int y = t.to[i], z = t.edge[i];
        if (y == fx) continue;
        dfs1(y, x);
        if (f[x][0] < f[y][0] + z) {
            f[x][1] = f[x][0];
            f[x][0] = f[y][0] + z;
            g[x] = y;
        } else f[x][1] = max(f[x][1], f[y][0] + z);
    }
}

void dfs2(int x, int fx) {
    for (int i = t.head[x]; ~i; i = t.nxt[i]) {
        int y = t.to[i], z = t.edge[i];
        if (y == fx) continue;
        f[y][2] = max(f[x][2], f[x][y == g[x]]) + z;
        dfs2(y, x);
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while (cin >> n) {
        t.init();
        for (int i = 2; i <= n; ++i) {
            int j, w;
            cin >> j >> w;
            t.insert(i, j, w); t.insert(j, i, w);
        }
        memset(f, 0, sizeof(f));
        memset(g, 0, sizeof(g));
        dfs1(1, 0);
        dfs2(1, 0);
        for (int i = 1; i <= n; ++i) cout << max(f[i][0], f[i][2]) << '\n';
    }
    return 0;
}
原文