2026/10/1 梦熊CSP-S二十连测 T3 车站巡查(inspection)题解
1 :题意
给定一棵
2 :思路
2.1 寻找最优解
首先我们发现
2.2 统计最优数量
根据上面所说,当
1. 直径点
先放上示意图
其中
2. 非直径点
还是先放示意图。
其中点
3. 汇总总贡献
对于直径点,贡献为
2.3 计算直径权值和
给出引理:一个树的所有直径会重叠,如果直径长度偶数,那么会重叠于一个点上,这个点是任意一条直径的中点;如果直径长度奇数,那么会重叠于一条边上,这条边是任意一条直径的中边。
证明略。
考虑在本题上的应用,需分类讨论直径的长度奇偶。直径的长度为
1.D 为偶数
随便找一条直径,找到那个中点,记为
考虑怎么构建出一个直径,我们发现是以
于是我们要计算一个子树内有多少条满足条件的长
2.D 为奇数
这时候我们会找到中边
这时我们发现如果将两侧的再算出来的话,可以任意两两匹配。所以直接计算出两边的贡献之和
3 :答案
直接计算出两部分的答案后,再乘上之前的
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.
*/