2026/10/1 梦熊CSP-S二十连测 T3 车站巡查(inspection)题解

· · 个人记录

1:题意

给定一棵 n 个节点的树,求一个排列 p_1, \dots, p_n 使得 \sum_{i=1}^{n-1} \text{dist}(p_i, p_{i+1}) 最小。输出最小值,以及达到最小值的排列个数(模 998244353)。n \le 5 \times 10^5。

2:思路

2.1 寻找最优解

首先我们发现 \sum_{i=1}^{n-1} \text{dist}(p_i, p_{i+1}) 并不好处理,但是如果加上一个 \text{dist}(p_n, p_1) 呢?我们发现这时候相当于寻找最短的回路,那么这时 DFS 序必然是最优的,因为一条边正好被遍历两次。那么问题就变成了让 \text{dist}(p_n, p_1) 尽可能的大,那不就是树的直径吗?因此我们的最优解就是 2(n-1) - D,其中 D 为直径长度。

2.2 统计最优数量

根据上面所说,当 p_1 和 p_n 分别时这棵树的直径两个端点的时候,才可能达到最优,因此就相当于统计一个 DFS 序的数量。我们分直径点和非直径点分类讨论贡献。

1. 直径点

先放上示意图

其中 A 和 B 是直径的端点,O 是直径上一点,s_{1,2,...,m} 为点 O 在直径上分出来的一些儿子,我们考虑点 O 的贡献。因为 DFS 序的先后是不影响结果的,所以所有的 m 个儿子可以以任意顺序遍历,贡献 m!;又因为点 O 一共被遍历了 m+1 次,可以在任意一次被加入,所以还有一个 m+1 的贡献,总贡献 = m! \times (m+1) = (m+1)!。因为 m=deg_o-2,所以贡献也可以写为 (deg_o-1)!。

2. 非直径点

还是先放示意图。

其中点 s 是一个非直径点,其儿子为 t_{1,2,...,k}。根据上面的推法,我们可以推出儿子重排的贡献为 k!,s 单个点的贡献为 k+1,所以总贡献为 (k+1)!。因为 k=deg_s - 1,所以贡献也可以写作 deg_s!。

3. 汇总总贡献

对于直径点,贡献为 (deg-1)!;对于非直径点,贡献为 deg!,总贡献就是两部分相乘再相加。我们发现这个贡献并不好统计,不妨先乘上所有的 deg,在单独出去直径点上的 deg。即将总贡献改写为 \frac{\prod_{i=1}^{n}deg_i!}{\prod_{i是直径点}deg_i}。我们发现 \prod_{i=1}^{n}deg_i! 不变,将他记为 S。将一条直径的权值记为它的所有直径点的度数的倒数的乘积,即 \prod_{i是直径点}\frac{1}{deg_i}。答案就变为了所有直径的权值和再乘上 S。

2.3 计算直径权值和

给出引理:一个树的所有直径会重叠,如果直径长度偶数,那么会重叠于一个点上,这个点是任意一条直径的中点;如果直径长度奇数,那么会重叠于一条边上,这条边是任意一条直径的中边。

证明略。

考虑在本题上的应用,需分类讨论直径的长度奇偶。直径的长度为 D。

1.D 为偶数

随便找一条直径,找到那个中点,记为 s,建立以 s 为根的有根树。

考虑怎么构建出一个直径,我们发现是以 s 的子节点 t_{1,2,...,m} 为根的子树里找出两个子树,并各选择出来一条到 s 的长 \frac{D}{2} 的路径,记这个路径的长度为 R = \frac{D}{2},我们用如下方式表示:

\underbrace{u_1, \dots, u_R}_{\text{左侧内部}}, s, \underbrace{v_1, \dots, v_R}_{\text{右侧内部}}

于是我们要计算一个子树内有多少条满足条件的长 R 的路径,并将答案累积到 c_{1,2,...,k} 中。我们要算的答案就是 (\sum_{1 \le i < j \le m}c_{t_i} \cdot c_{t_j}) \times \frac{1}{deg_c}。前面一部分可以转化为 \frac{(\sum_{i=1}^{m}c_{t_i})^2 - \sum_{i=1}^{m}c_{t_i}^2}{2}。

2.D 为奇数

这时候我们会找到中边 (s_1,s_2),R=\frac{D-1}{2}。我们先将这条边断开,之后分别以 s_1 和 s_2 建立有根树。这时候我们还是表示出来:

\underbrace{u_1, \dots, u_R}_{\text{左侧内部}}, s_1, s_2, \underbrace{v_1, \dots, v_R}_{\text{右侧内部}}

这时我们发现如果将两侧的再算出来的话,可以任意两两匹配。所以直接计算出两边的贡献之和 c_1 和 c_2,总贡献直接就是 c_1 \cdot c_2 \cdot \frac{1}{deg_{s_1}} \cdot \frac{1}{deg_{s_2}}。

3:答案

直接计算出两部分的答案后,再乘上之前的 S,再乘上 2(可以从两个方向走直径),就是答案了。这道题就做完了。注意特判 D=1 的情况即可。

4:Code

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define lll __int128
#define db double
#define ldb long db
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define endl '\n'
#define ft first
#define sd second
using namespace std;
const ll Mod = 998244353;
int n, D;
vector<int> edge[500005];
int d[500005];
ll fac[500005];
ll deg[500005], inv[500005];
ll c[500005];
void dfs(int x, int fa) {
    d[x] = d[fa] + 1;
    for(int y : edge[x]) {
        if(y == fa) {
            continue;
        }
        dfs(y, x);
    }
}
void _dfs(int x, int fa, ll sum, ll& res) {
    //cout << "x = " << x << ", fa = " << fa << endl;
    (sum *= inv[x]) %= Mod;
    //cout << "sum = " << sum << endl;
    if(d[x] == D / 2) {
        (res += sum) %= Mod;
        //cout << "gongxian" << endl;
    }
    for(int y : edge[x]) {
        if(y == fa) {
            continue;
        }
        _dfs(y, x, sum, res);
    }
}
ll qpw(ll x, ll y) {
    ll res = 1;
    while(y) {
        if(y & 1ll) {
            (res *= x) %= Mod;
        }
        (x *= x) %= Mod;
        y >>= 1ll;
    }
    return res;
}
int get(int x, int y, int dis) {
    dfs(x, 0);
    vector<int> vec;
    for(int i = 1; i <= n; i++) {
        if(d[i] == dis) {
            vec.push_back(i);
        }
    }
    dfs(y, 0);
    for(int i : vec) {
        if(d[i] == D - dis) {
            return i;
        }
    }
}
int main() {
    freopen("inspection.in", "r", stdin);
    freopen("inspection.out", "w", stdout);
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    fac[0] = 1;
    for(int i = 1; i <= 500000; i++) {
        fac[i] = fac[i - 1] * i % Mod;
    }
    cin >> n;
    for(int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    d[0] = -1;
    dfs(1, 0);
    //cout << "dfs1" << endl;
    int a = 0, b = 0;
    for(int i = 1; i <= n; i++) {
        if(d[i] > d[a]) {
            a = i;
        }//cout << "d[" << i << "] = " << d[i] << endl;
    }
    //cout << "a = " << a << endl;
    dfs(a, 0);
    //cout << "dfs" << a << endl;
    for(int i = 1; i <= n; i++) {
        if(d[i] > d[b]) {
            b = i;
        }//cout << "d[" << i << "] = " << d[i] << endl;
    }
    //cout << "b = " << b << endl;
    D = d[b];
    //cout << "D = " << D << endl;
    cout << 2 * (n - 1) - D << endl;
    if(D == 1) {
        cout << 2 << endl;
        return 0;
    }
    for(int i = 1; i <= n; i++) {
        deg[i] = edge[i].size();
        inv[i] = qpw(deg[i], Mod - 2);
        //cout << "inv[" << i << "] = " << inv[i] << endl;
    }
    ll S = 1;
    for(int i = 1; i <= n; i++) {
        (S *= fac[deg[i]]) %= Mod;
    }
    if(D % 2 == 0) {
        int s = get(a, b, D / 2);
        dfs(s, 0);
        for(int i : edge[s]) {
            _dfs(i, s, 1, c[i]);
        }
        ll ans = 0;
        for(int i : edge[s]) {
            (ans += c[i]) %= Mod;
        }
        ans = ans * ans % Mod;
        for(int i : edge[s]) {
            (ans += Mod - c[i] * c[i] % Mod) %= Mod;
        }
        cout << ans * inv[s] % Mod * S % Mod << endl;
    } else {
        int s1 = get(a, b, (D - 1) / 2), s2 = get(a, b, (D + 1) / 2);
        //cout << "a = " << a << ", b = " << b << ", s1 = " << s1 << ", s2 = " << s2 << endl; 
        ll c1 = 0, c2 = 0;
        dfs(s1, 0);
        _dfs(s1, s2, deg[s1], c1);
        dfs(s2, 0);
        _dfs(s2, s1, deg[s2], c2);
        //cout << "c1 = " << c1 << ", c2 = " << c2 << endl;
        cout << c1 * c2 % Mod * inv[s1] % Mod * inv[s2] % Mod * S % Mod * 2ll % Mod << endl;
    }
    return 0;
}

/*
Progress is impossible without change, and those who cannot change their minds cannot change anything.
NEVER GIVE UP.
*/