题解:P14666 [KenOI 2025] 游走题
2025.12.9 update:
感谢 haoertiansuo 的指正!现在本篇逆元题解已经修复!让逆元再次辉煌!
具体内容详见 此工单 。
前言
一道基础的树上dp题,考验了选手是否掌握树上dp。
由于题目较基础,面向的多为新手,所以笔者会写的详细一些。对于进阶选手如果觉得废话多可以不用看这么细。
碎碎念:今天脑子昏,读错三次题+调试几个小时,我是金钩的耻辱。
正文
题目说到,当 u 没有相邻节点时,游走停止。树是联通的,只有断边才会导致没有相邻节点。而题目又说到,只有向上行走时才会断边,所以游走时会向上行走一直到树根。样例解释也印证了这一点(所有路径终点都为
树上问题+统计方案数,考虑树上 dp。
先尝试设计一个经典的dp状态:
如图所示,假设起点为
那我们考虑换个状态:
这时候转移就非常方便了。考虑儿子节点
所以再维护一个
如果选了
所以有 dp 转移方程:
这么写太恶心了,每次还得重新乘起来一遍。我们令:
此时 dp 转移方程为:
现问题解决完了,我们再回来考虑原问题。唯一区别是钦定了一个起始节点。我们会观察到两个性质:
- 起始节点及其祖先的子树在转移时必被选。统计方案的时候不可以
+1 乘一起。 - 起始节点回到根节点路径上的边只会经过一次(因为走完就被断掉力)。
转移的时候特判一下就好啦,转移的时候统计总方案的时候,如果子节点是
最后的最后,(或者你可以看我的代码)。
由于涉及到取模除法,需要用到费马小定理求逆元:
总时间复杂度:
2025.12.9 update
没想到被 hack 了。但是其实做题的时候考虑过这种情况,只不过 AC 了没去多想。看来以后还是得谨慎啊。
求逆元的时候会出问题,因为是求方案数取模,所以可能 子树贡献为 0,导致逆元出错。
如何避免这种情况呢?考虑求逆元本质是在求除了该子树意外的方案乘积,那让我们进行分类讨论:
- 如果全部子树的方案数都不为
0 ,那么正常做即可。 - 如果存在
>1 个子树的方案数为0 ,那么去除这个子树方案积仍然为0 。此时不做任何贡献。 - 如果存在
=1 个子树的方案数为0 ,那么进行特判,只统计这个子树的贡献即可。
具体实现为:维护一个变量
- 如果
cnt_0 = 0 ,则不需要管。 - 如果
cnt_0 > 1 ,则直接返回。 - 如果
cnt_0 = 1 ,则统计所有子树方案数 除 0 外 的乘积,然后统计答案即可。
代码细节很多,我也不清楚我是否有遗漏的点。欢迎大家来 hack!
:::success[AC Code]
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lf;
const int INF = 1e9+7;
const ll LLINF = (ll)1e18+7;
const lf LFINF = (lf)1e18+7;
const int maxn = 1e6+5;
const int maxnode = maxn;
const int maxedge = maxnode*2;
const ll mod = 1e9+7;
int N, s;
ll dp[maxn], dp2[maxn];
bool vis[maxn];
struct Edge{
int v, next;
}edge[maxedge];
int head[maxnode];
int cntE = 0;
void addedge(int u, int v){
edge[++cntE] = {v, head[u]};
head[u] = cntE;
}
bool inline isdig(const char &ch){
return ch >= '0' && ch <= '9';
}
template <typename type>
void inline read(type &x){
x = 0; type f = 1;
char ch = getchar();
while (!isdig(ch)){
if (ch == '-') f = -1;
ch = getchar();
}
while (isdig(ch)) x = x*10+ch-'0', ch = getchar();
x = x*f;
}
// 快速幂
ll quick_pow(ll a, ll x){
ll res = 1;
while (x > 0){
if (x & 1) res = res*a%mod;
x >>= 1;
a = a*a%mod;
}
return res;
}
// 费马小定理求逆元
ll inv(ll x){
return quick_pow(x, mod-2);
}
// tree dp
void dfs(int u, int fa){
dp2[u] = 1ll;
int cnt0 = 0;
for (int i = head[u]; i; i = edge[i].next){
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
ll ex = (dp2[v] + !vis[v])%mod;
// 先记录除0外的方案积。如果 cnt0 > 0 则实际方案积 = 0。
if (ex) dp2[u] = dp2[u]*ex%mod;
else cnt0++;
// vis标记是否为 (s, 1) 路径上的一条边。用来特判。
vis[u] |= vis[v];
}
if (cnt0 > 1){
dp2[u] = 0;
return;
}
for (int i = head[u]; i; i = edge[i].next){
int v = edge[i].v;
if (v == fa) continue;
ll ex = (dp2[v] + !vis[v])%mod;
// 当前仅当只存在一个子树方案为0时,并且就是该子树方案=0时,才会有贡献。
if (cnt0 && ex) continue;
ll cnt = dp2[u] * (ex ? inv(ex) : 1ll)%mod;
ll res = cnt * dp[v]%mod;
// 如果实际方案乘积 = 0就当0用
if (vis[v]) res = (res + (cnt0 ? 0ll : dp2[u]))%mod;
else res = (res + cnt*dp2[v]%mod*2%mod)%mod;
dp[u] = (dp[u] + res)%mod;
}
// 别忘了修正 dp2[u]!
if (cnt0) dp2[u] = 0;
}
int main(){
read(N); read(s);
for (int i = 1; i <= N-1; i++){
int u, v;
read(u); read(v);
addedge(u, v);
addedge(v, u);
}
vis[s] = 1;
dfs(1, 0);
// for (int i = 1; i <= N; i++) printf("f=%lld g=%lld\n", dp[i], dp2[i]);
// 来看看求答案的代码吧!
ll res = 0, ttl = 1ll;
ll cnt0 = 0;
for (int i = head[1]; i; i = edge[i].next){
int v = edge[i].v;
if (dp2[v]) ttl = ttl*dp2[v]%mod;
else cnt0++;
}
if (cnt0 > 1){
printf("0\n");
return 0;
}
for (int i = head[1]; i; i = edge[i].next){
int v = edge[i].v;
if (cnt0 && dp2[v]) continue;
res = (res + ttl*(dp2[v] ? inv(dp2[v]) : 1ll)%mod*dp[v]%mod) % mod;
// 如果ttl = 0则直接退出
if (cnt0) continue;
if (vis[v]) res = (res + ttl)%mod;
else res = (res + ttl*2)%mod;
}
printf("%lld\n", res);
return 0;
}
:::