CF1654G Snowy Mountain 题解
CF1654G Snowy Mountain 题解
题意
给出一颗树,点分黑白色。一个点的高度
题解
(下面称一些相邻的等高点组成的连通块为一个“平台”。)
容易想到,我们每次直接走得尽可能低,然后在两个相同高度的点之间 brbrbrbrbr 然后再走到最下面。
所以答案其实就很明晰了:设
首先给出一个一点也不好想的暴力做法。
一个显然的事实:一个点
作为一个 OI 人,秉着有分就骗有常数就卡的原则(虽然这是 CF),我们发现平台的高度种类数再怎么说也比点数要少一些,尝试反着 bfs。
具体地,对于一个高度
这个类似于一个最短路,但要取
可以选择用 SPFA。
当然也可以选择使用 01bfs(我并不会)。
有意思的是,这样的时间复杂度是
于是我们就 A 了。
代码
(01bfs 写挂了不想调,直接 SPFA。)
using namespace std;
#include<bits/stdc++.h>
//#include<cstdio>
//#include<cstring>
//#include<string>
//#include<iostream>
//#include<utility>
//#include<algorithm>
#define MAX(x,y) (x<y?y:x)
#define MIN(x,y) (x<y?x:y)
#define ABS(x) (x<0?-x:x)
#define lb(x) ((x)&(-(x)))
#define N 200005
#define LL long long
#define LD long double
#define int LL
//#define double LD
#define P 1000000007
#ifdef _WIN32
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#elif _WINDOWS_
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#else
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
constexpr double EPS=(1e-9);
#ifdef int
constexpr int INF=1211081101201201140;
#else
constexpr int INF=1145141919;
#endif
template<typename T>T Max(T x,T y){return MAX(x,y);}
template<typename T>T Min(T x,T y){return MIN(x,y);}
template<typename T>T Abs(T x){return ABS(x);}
template<typename T>void Swap(T&x,T&y){x^=y^=x^=y;}
template<typename T>T Gcd(T x,T y){while(y^=x^=y^=x%=y);return x;}
template<typename T>
T qp(T a,T b){T sum=1;
while(b){if(b&1)sum=(sum*a)%P;a=(a*a)%P;b>>=1;}return sum;}
template<typename T>
void read(T&x){
x=0;char c=getchar();/*T fl=1;*/
while(c<'0'||c>'9'){/*if(c == '-')fl=-1;*/c=getchar();}
while('/'<c&&c<':'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}/*x*=fl;*/}
template<typename T>
void write(T x){if(x<0){x=-x;putchar('-');}
if(x>9){write(x/10);}putchar(x%10|48);}
int n;
int t[N<<1],las[N<<1],fir[N],num;
void add(int u,int v){
t[++num]=v;las[num]=fir[u];fir[u]=num;
t[++num]=u;las[num]=fir[v];fir[v]=num;}
int col[N];
int h[N];
void bfs(){
queue<int> q;
for(int i=1;i<=n;i++){
h[i]=INF;
if(col[i])h[i]=0,q.push(i);
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=fir[u];i;i=las[i])
if(h[u]+1<h[t[i]])q.push(t[i]),h[t[i]]=h[u]+1;
}
}
vector<int> v[N];
int val[N];
int vis[N];
int stp[N];
signed main(){
read(n);
for(int i=1;i<=n;i++)read(col[i]);
for(int i=1,x,y;i<n;i++)read(x),read(y),add(x,y);
bfs();
for(int u=1;u<=n;u++){
int fl=0;
for(int i=fir[u];i;i=las[i])fl|=(h[t[i]]==h[u]);
if(fl)v[h[u]].push_back(u);
}
for(int i=1;i<=n;i++)val[i]=h[i];
for(int k=n;k>=0;k--){
if(v[k].empty())continue;
for(int i=1;i<=n;i++)vis[i]=0,stp[i]=INF;
queue<int> q;
for(int u:v[k])stp[u]=0,q.push(u);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=fir[u];i;i=las[i]){
if(h[t[i]]==h[u]&&stp[u]+1<stp[t[i]]){
stp[t[i]]=Min(stp[u]+1,stp[t[i]]);
if(!vis[t[i]])q.push(t[i]);
vis[t[i]]=1;
}
else if(h[t[i]]>h[u]&&Max(stp[u]-1,(int)0)<stp[t[i]]){
stp[t[i]]=Min(stp[t[i]],Max(stp[u]-1,(int)0));
if(!vis[t[i]])q.push(t[i]);
vis[t[i]]=1;
}
}
}
for(int i=1;i<=n;i++)if(stp[i]<=0)val[i]=Min(val[i],k);
}
for(int i=1;i<=n;i++)write((h[i]<<1)-val[i]),putchar(' ');
return 0;
}