题解:P17307 [ICPC 2026 Xi'an I] Zebra Crossing
_LogicFish_qwq · · 题解
令人惊讶的是,这题竟然是暴力可解的。
思路
简单的画了一下,然后就能发现:如果一条路径
这启发我们从根节点(本题可当做节点
得到 \bm{dis} 数组
那么,怎么跑,可以最小化经过的黑色点呢?那就是尽量往路上的白点跳,或者,往别的地方跳。什么地方?
比如这张图,假设只有
说明,如果直接从
换句话说,我们利用
:::info[总结]{open}
通过跳到路径之外的其它白点(白中转点),可以刷新“当前可用的步数”。
:::
所以,我们可以开一个
为什么不记录“这个点是谁”呢?因为我们只关心能不能通过某个点来“刷新”当前的步数,而不关心具体是谁(除非要输出路径是什么之类的……),因此“距离”是我们唯一关心的,直接记距离当然比差分计算快得多。
扯远了,总之来看看这个距离怎么更新:
:::success[计算
//to 是存边数组,col表示颜色(1是白),dis见上
inline void DFS(int x,int pre){
if(col[x]) dis[x]=0;
for(int y:to[x]){
if(y==pre) continue;
DFS(y,x);
dis[x]=std::min(dis[x],dis[y]+1);
}
}
:::
计算答案
好了,然后呢,我们每个点的答案(记为
还记得我说“可以从根节点开始搜索”吗?我们要从
考虑我们前面得到的小技巧:
通过跳到路径之外的其它白点(白中转点),可以刷新“当前可用的步数”。
同时,我们发现,前面的推导过程,对于任意的
因此,如果一次性跳
考虑第二个搜索,我们需要记录哪些参数呢?
第一,一定是“当前节点
第二,就是“上一个中转点”是谁,因为我们搜搜到当前点的时候,虽然是“一步步”走过来的,但统计答案时,要当做“这几步是一次性跳过来的”,所以要用“上一个中转点”更新答案。在这里,我们记为
同时,既然是把“跳
于是答案就很简单了:
考虑当前点
显然,根据贪心的思路,为了使后面尽可能少踩黑点,这里的
接下来,从
- 先更新必须走到
\bm{y} 的答案,因为你必须到这里,所以就算这个点是黑的你也只能认了,ans_{y}=ans_{last}+[col_{y}=0] (记住,实际上是从last 跳到这来的)。接下来看经过y 的,其他点的答案。 - 如果
stp+1=k ,说明走到儿子之后不可能再去刷新什么了,那么就只能该踩哪是哪,更新stp=0 (因为要开始新的跳跃了),last=y (因为新的跳跃式从y 开始的)。 - 否则,那走到儿子之后还有可能尝试刷新,所以
stp+1 ,last 不变。
说到这估计也能写出个大概了吧? :::success[更新答案]
inline void Jump(int x,int pre,int stp,int last){
if(dis[x]<=k-stp) stp=std::min(stp,dis[x]);
//使用 min 取到最小的,注意判断这个点是不是可以在 k-stp 步之内走到的
for(int y:to[x]){
if(y==pre) continue;
ans[y]=ans[last]+(col[y]==0);
//更新“以 y 为终点”的答案
if(stp==k-1) Jump(y,x,0,y);//更新跳跃
else Jump(y,x,stp+1,last);//不用更新
}
}
::::info[关于“不用更新”的解释] 这里本应该再复杂一点的:
- 如果
x 是黑点,那么不能用x 刷新步数(否则答案变大了)。 - 否则
x 是白点,可以用来刷新。
| 但是,考虑到我们定义“ |
|---|
| ## 代码 |
| 作为一道紫题,代码量不大,但是贪心的想法和“把跳 |
| :::success[AC Code] |
| ```cpp |
| //Someone tell Vedal there is a problem with my AI. |
| #include<iostream> |
| #include<cstdio> |
| #include<vector> |
| #include<algorithm> |
| #define int long long |
| #define rep(v,s,e) for(int v=(s);v<=(e);v++) |
| #define dec(v,e,s) for(int v=(e);v>=(s);v--) |
| const int MAXN=5e5,INF=1e18+987; |
| int n,k; |
| std::vector<int>to[MAXN+5]; |
| int col[MAXN+5]; |
| int dis[MAXN+5],ans[MAXN+5]; |
| inline void DFS(int x,int pre); |
| inline void Jump(int x,int pre,int stp,int last); |
| void Init(),PreSolve(),Input(),Solve(),Answer(),AC(); |
| signed main(){ |
| AC(); |
| return 0; |
| } |
| void Init(){ |
| std::ios::sync_with_stdio(false), |
| std::cin.tie(0),std::cout.tie(0); |
| #ifndef ONLINE_JUDGE |
| std::freopen("FILE.in" ,"r",stdin ); |
| std::freopen("FILE.out","w",stdout); |
| #endif |
| } |
| void AC(){ |
| Init(); |
| int T=1; |
| std::cin>>T; |
| while(T--){ |
| PreSolve(); |
| Input(); |
| Solve(); |
| Answer(); |
| } |
| } |
| void PreSolve(){ |
| std::cin>>n>>k; |
| rep(i,1,n) to[i].clear(); |
| rep(i,1,n) dis[i]=INF,ans[i]=0; |
| } |
| void Input(){ |
| char ch; |
| rep(i,1,n){ |
| std::cin>>ch; |
| col[i]=ch-'0'; |
| } |
| int x,y; |
| rep(i,1,n-1){ |
| std::cin>>x>>y; |
| to[x].push_back(y),to[y].push_back(x); |
| } |
| } |
| void Solve(){ |
| DFS(1,0); |
| Jump(1,0,0,1); |
| } |
| void Answer(){ |
| rep(i,2,n) std::cout<<ans[i]<<" "; |
| std::cout<<"\n"; |
| } |
| inline void DFS(int x,int pre){ |
| if(col[x]) dis[x]=0; |
| for(int y:to[x]){ |
| if(y==pre) continue; |
| DFS(y,x); |
| dis[x]=std::min(dis[x],dis[y]+1); |
| } |
| } |
| inline void Jump(int x,int pre,int stp,int last){ |
| if(dis[x]<=k-stp) stp=std::min(stp,dis[x]); |
| for(int y:to[x]){ |
| if(y==pre) continue; |
| ans[y]=ans[last]+(col[y]==0); |
| if(stp==k-1) Jump(y,x,0,y); |
| else Jump(y,x,stp+1,last); |
| } |
| } |
| ``` |
| ::: |