跳到正文

题解

题解:P10070 [CCO 2023] Travelling Trader

modfish_约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年2月7日
收录
2026年6月11日
更新
2026年10月14日
篇幅
约 20 分钟
题目
P10070

版本

  1. v1当前

简化题意

给定一棵树,从点 11 开始,每次可以选择一个到当前点距离不超过 kk 的点,并跳到那个点上,不能重复经过某个点,求遍历的点权和最大值。

k=1k=1

大家想必都会了,选出最长链即可。

k=3k=3

看看分值分布,不难猜到 k=3k=3 比 k=2k=2 简单——k=3k=3 时一定可以遍历所有点。

方案如何构造?看下图:

我们证明:

  • 结论 A:从一个点 xx 出发,一定能够遍历 xx 的子树内所有点,并停止到 xx 的某个儿子。
  • 结论 B:从一个点 xx 的儿子出发,一定能够遍历 xx 的子树内所有点,并停止到 xx。

使用归纳法,假设其成立,并证明自洽性。

如图,如果结论 22 成立,那么从点 11 跳到 22 的某个儿子后,一定可以遍历完 22 的子树并跳到 22,之后再跳到 33 的儿子,重复此操作。最后一定停在 11 的儿子上,于是结论 22 可以推出结论 11。

如图,如果结论 11 成立,那么从 22 开始可以遍历 22 的子树并跳到 22 的儿子,从 22 的儿子跳到 33,重复此操作,最后可以停在 11 上。于是结论 11 可以推出结论 22。

于是显然这两个结论都是成立的。方案就如上构造即可。

k=2k=2

这才是最困难的。不过 k=3k=3 给了我们些许启示。

观察在 k=3k=3 中的两种跳法:

  • 遍历点的子树时,从它自己出发的,并在它们的某个儿子停止。
  • 遍历点的子树时,从子树外直接跳到它们的某个儿子,并在它们自己停止。

这暗示我们,这两种结点应该分别讨论。设计以下状态:

  • fx,0f_{x,0},表示从 xx 出发,在 xx 的子树内跳跃,不限制终点,所能得到的最大点权和(不含 xx)。
  • fx,1f_{x,1},表示从 xx 出发,在 xx 的子树内跳跃,最后停在 xx 的某个儿子上,所能得到的最大点权和(不含 xx)。
  • gx,0g_{x,0},表示从 xx 的儿子出发,在 xx 的子树内跳跃,不限制终点(但要经过 xx),所能得到的最大点权和(不含 xx)。
  • gx,1g_{x,1},表示从 xx 的儿子出发在 xx 的子树内跳跃,最后停在 xx 上,所能得到的最大点权和(不含 xx)。

答案即为 f1,0f_{1,0}。现在考虑转移:


fx,1f_{x,1} 的转移

为何不先讲 fx,0f_{x,0}?因为 fx,1f_{x,1} 更简单。

显然只有一种最优跳法:

转移式为:

fx,1=max⁡i∈son(x)gi,1+∑i∈son(x)aif_{x,1}=\max_{i\in \text{son}(x)}g_{i,1}+\sum_{i\in \text{son}(x)} a_i

其中 son(x)\text{son}(x) 表示 xx 的儿子组成的集合。

fx,0f_{x,0} 的转移

也大差不差,只是最后不用停在儿子上:

注意特判只有一个儿子的情况。

其实,另外还有一种可能更优的走法:

是很容易被忽略的。这种走法在某一个儿子的 gj,0g_{j,0} 特别大时是优的。注意这种走法是会经过点 33 的,详情请见 gx,0g_{x,0} 的定义。

转移式为:

fx,0=max⁡(max⁡i,j∈son(x)(gi,1+fi,0)+∑i∈son(x)ai,max⁡i∈son(x)(gi,0+ai))f_{x,0}=\max(\max_{i,j\in \text{son}(x)}(g_{i,1}+f_{i,0})+\sum_{i\in \text{son}(x)}a_i,\max_{i\in \text{son(x)}}(g_{i,0}+a_i))

gx,1g_{x,1} 的转移

与 fx,1f_{x,1} 类似(其实就是反过来走):

转移式:

gx,1=max⁡i∈son(x)fi,1+∑i∈son(x)aig_{x,1}=\max_{i\in \text{son}(x)}f_{i,1}+\sum_{i\in \text{son}(x)}a_i

gx,0g_{x,0} 的转移

首先有一种朴素的走法:

(注意,路径会经过 55 点,详情请见 gx,0g_{x,0} 的定义)。

但是,还有另一种跳法:

转移式:

gx,0=max⁡(max⁡i,j∈son(x)(fi,1+gj,0)+∑i∈son(x)aimax⁡i,j,k∈son(x)(fi,1+gj,1+fk,0)+∑i∈son(x)ai)\begin{aligned} g_{x,0}=\max(&\max_{i,j\in \text{son}(x)}(f_{i,1}+g_{j,0})+\sum_{i\in \text{son}(x)}a_i\\ &\max_{i,j,k\in \text{son}(x)}(f_{i,1}+g_{j,1}+f_{k,0})+\sum_{i\in \text{son}(x)}a_i) \end{aligned}

注意特判儿子数量为 11 和 22 的情况。


好了,现在转移也做完了,就只剩下构造方案了。这部分没什么好说的,记录转移时最优的 i,j,ki,j,k,按照图片的跳法跳就行……虽然非常难写。

代码

本人的代码写得十分丑陋,仅供参考或对拍。

C++
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int maxn = 2e5 + 5;

int head[maxn], nxt[maxn << 1], to[maxn << 1], cnt = 0;
ll a[maxn], ans = 0;
int go[maxn], lnk[maxn], m = 0, siz[maxn];

static inline void match(int u, int v){
    nxt[++ cnt] = head[u], to[cnt] = v, head[u] = cnt;
}
namespace solve1{
static inline ll dfs(int x, int f){
    ll res = 0;
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == f) continue;
        ll u = dfs(j, x);
        if(u > res) res = u, go[x] = j;
    }
    if(x == 1) ans = res + a[x];
    return res + a[x];
}}
namespace solve2{
ll F[2][maxn], G[2][maxn];
int fx[2][maxn], fy[2][maxn], gx[2][maxn], gy[2][maxn], gz[maxn];
int tf0[maxn];
priority_queue<pair<ll, int> > f1, f0, g0, g1;
pair<ll, int> AA[3], BB[3], CC[3];
static inline void dfs(int x, int f){
    siz[x] = 1;
    ll sum = 0;
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == f) continue;
        dfs(j, x);
        sum += a[j], siz[x] += siz[j];
    }
    while(!f1.empty()) f1.pop();
    while(!f0.empty()) f0.pop();
    while(!g0.empty()) g0.pop();
    while(!g1.empty()) g1.pop();
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == f) continue;
        f1.push(make_pair(F[1][j], j)), f0.push(make_pair(F[0][j], j));
        g0.push(make_pair(G[0][j], j)), g1.push(make_pair(G[1][j], j));
    }
    if(f1.empty()){
        F[0][x] = F[1][x] = G[0][x] = G[1][x] = 0;
        fx[0][x] = fy[0][x] = fx[1][x] = fy[1][x] = x;
        gx[0][x] = gx[1][x] = x;
        return;
    }
    pair<ll, int> A, B;
    // 1
    A = g1.top(), g1.pop();
    F[1][x] = sum + A.first, fx[1][x] = A.second;
    // 2
    B = f0.top(), f0.pop();
    if(A.second == B.second){
        if(g1.empty()) F[0][x] = sum + B.first, fy[0][x] = B.second;
        else{
            if(g1.top().first - A.first > f0.top().first - B.first) A = g1.top();
            else B = f0.top();
        }
    }
    if(!g1.empty()) F[0][x] = sum + A.first + B.first, fx[0][x] = A.second, fy[0][x] = B.second;
    if(g0.top().first + a[g0.top().second] > F[0][x]) F[0][x] = g0.top().first + a[g0.top().second], fx[0][x] = g0.top().second, tf0[x] = 1;
    // 3
    A = f1.top(), f1.pop();
    G[1][x] = sum + A.first, gx[1][x] = A.second;
    // 4
    B = g0.top(), g0.pop();
    if(A.second == B.second){
        if(g0.empty()) G[0][x] = sum + B.first, gy[0][x] = B.second;
        else{
            if(f1.top().first - A.first > g0.top().first - B.first) A = f1.top();
            else B = g0.top();
        }
    }
    if(!f1.empty()) G[0][x] = sum + A.first + B.first, gx[0][x] = A.second, gy[0][x] = B.second;
    while(!f1.empty()) f1.pop();
    while(!f0.empty()) f0.pop();
    while(!g0.empty()) g0.pop();
    while(!g1.empty()) g1.pop();
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == f) continue;
        f1.push(make_pair(F[1][j], j)), f0.push(make_pair(F[0][j], j));
        g1.push(make_pair(G[1][j], j));
    }
    if(f1.size() < 3) return;
    for(int i = 0; i < 3; i ++){
        AA[i] = f1.top(), f1.pop();
        BB[i] = g1.top(), g1.pop();
        CC[i] = f0.top(), f0.pop();
    }
    pair<ll, int> C;
    A = B = C = make_pair(0, 0);
    for(int i = 0; i < 3; i ++){
        for(int j = 0; j < 3; j ++){
            for(int k = 0; k < 3; k ++){
                if(AA[i].second != BB[j].second && BB[j].second != CC[k].second && CC[k].second != AA[i].second){
                    if(AA[i].first + BB[j].first + CC[k].first >= A.first + B.first + C.first){
                        A = AA[i], B = BB[j], C = CC[k];
                    }
                }
            }
        }
    }
    if(sum + A.first + B.first + C.first > G[0][x]){
        G[0][x] = sum + A.first + B.first + C.first;
        gx[0][x] = A.second, gy[0][x] = B.second, gz[x] = C.second;
    }
}
static inline pair<int, int> calc(int x, int f, int tp){
    if(x == 1) ans = F[0][x] + a[1], tp = 0;
    if(siz[x] == 1) return make_pair(x, x);
    pair<int, int> res;
    int r = 0, s = 0;
    if(tp == 0){
        r = x;
        if(tf0[x]){
            res = calc(fx[0][x], x, 2);
            go[r] = res.first;
        }else{
            if(fx[0][x]){
                res = calc(fx[0][x], x, 3);
                go[r] = res.first, r = res.second;
            }
            for(int i = head[x]; i; i = nxt[i]){
                int j = to[i];
                if(j == f || j == fx[0][x] || j == fy[0][x]) continue;
                r = go[r] = j;
            }
            res = calc(fy[0][x], x, 0);
            go[r] = res.first, r = res.second;
        }
        return make_pair(x, 0);
    }else if(tp == 1){
        r = x;
        res = calc(fx[1][x], x, 3);
        go[r] = res.first, r = res.second;
        for(int i = head[x]; i; i = nxt[i]){
            int j = to[i];
            if(j == f || j == fx[1][x]) continue;
            r = go[r] = j;
        }
        return make_pair(x, r);
    }else if(tp == 2){
        if(gz[x]){
            res = calc(gx[0][x], x, 1);
            s = res.first, r = res.second;
            r = go[r] = x;
            res = calc(gy[0][x], x, 3);
            go[r] = res.first, r = res.second;
            for(int i = head[x]; i; i = nxt[i]){
                int j = to[i];
                if(j == f || j == gx[0][x] || j == gy[0][x] || j == gz[x]) continue;
                r = go[r] = j;
            }
            res = calc(gz[x], x, 0);
            go[r] = res.first;
        }else{
            for(int i = head[x]; i; i = nxt[i]){
                int j = to[i];
                if(j == f || j == gx[0][x] || j == gy[0][x]) continue;
                if(!s) s = r = j;
                else r = go[r] = j;
            }
            if(gx[0][x]){
                res = calc(gx[0][x], x, 1);
                go[r] = res.first, r = res.second;
                if(!s) s = res.first;
            }
            r = go[r] = x;
            res = calc(gy[0][x], x, 2);
            go[r] = res.first;
        }
        return make_pair(s, 0);
    }else{
        for(int i = head[x]; i; i = nxt[i]){
            int j = to[i];
            if(j == f || j == gx[1][x]) continue;
            if(!s) s = r = j;
            else r = go[r] = j;
        }
        res = calc(gx[1][x], x, 1);
        go[r] = res.first, r = res.second;
        go[r] = x;
        if(!s) s = res.first;
        return make_pair(s, x);
    }
    return make_pair(0, 0);
}}
namespace solve3{
static inline int dfs(int x, int f, int u, int op){
    ans += a[x];
    int v = x;
    if(!op){
        v = x;
        for(int i = head[x]; i; i = nxt[i]){
            int j = to[i];
            if(j == f) continue;
            v = go[dfs(j, x, v, 1)] = j;
        }
    }else{
        v = u;
        for(int i = head[x]; i; i = nxt[i]){
            int j = to[i];
            if(j == f) continue;
            go[v] = j;
            v = dfs(j, x, 0, 0);
        }
    }
    return v;
}}
static inline void solve(){
    printf("%lld\n", ans);
    int x = 1;
    while(x) lnk[++ m] = x, x = go[x];
    printf("%d\n", m);
    for(int i = 1; i <= m; i ++) printf("%d ", lnk[i]);
    printf("\n");
}

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    for(int i = 1; i < n; i ++){
        int u, v;
        scanf("%d %d", &u, &v);
        match(u, v), match(v, u);
    }
    for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
    if(k == 1) solve1::dfs(1, 0);
    if(k == 2) solve2::dfs(1, 0), solve2::calc(1, 0, 0);
    if(k == 3) solve3::dfs(1, 0, 0, 0);
    solve();
    return 0;
}
原文