题解:P17307 [ICPC 2026 Xi'an I] Zebra Crossing

· · 题解

前言

看了一圈题解,怎么没有力大砖飞的做法。

思路

首先我们往图论上想,我们将所有满足 dis(x,y)\leq k 的点对 x,y 连边,然后相当于求 1 到所有点的最短路,看到 dis(x,y)\leq k 的点对之间连边肯定想到点分治了,然后再套一个前缀优化建边,至于如何将点按照长度排序就是直接用广搜,然后连边就是 n\log (n) 的了,现在得到了一张 n,m 都是 n\log(n) 级别的图,直接跑最短路肯定死了,然后我们通过观察发现边权只有 0,1 所以直接 01 bfs 即可。

代码

由于拿自己的代码在洛谷卡常拼尽全力无法通过,所以给一份 ai 写的,和我自己的(能过 qoj)。

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define fire signed
#define il inline
int T=1;
const int N=5e5+10;
int n,k;
string s;
struct node{
    int x,y;
}edg[N*2],eg[N*80];
int head[N],tt;
int hd[N*20],tt1;
void add1(int x,int y) {
    edg[++tt]={y,head[x]};
    head[x]=tt;
}
int siz[N],mx[N],vis[N*20];
int rt,sum;
void dfs(int x,int fa) {
    siz[x]=1;
    mx[x]=false;
    for(int i=head[x];i;i=edg[i].y) {
        int to=edg[i].x;
        if(to==fa||vis[to]) continue;
        dfs(to,x);
        siz[x]+=siz[to];
        mx[x]=max(mx[x],siz[to]);
    }
    mx[x]=max(mx[x],sum-siz[x]);
    if(mx[x]<mx[rt]) rt=x;
}
pair<int,int>arr[N];
int tot;
int pr[N],idx;
void dfs1(int x,int fa,int d) {
    if(d<=k) {
        arr[++tot]={d,x};
    }else return;
    for(int i=head[x];i;i=edg[i].y) {
        int to=edg[i].x;
        if(to==fa||vis[to]) continue;
        dfs1(to,x,d+1);
    }
}
void add(int x,int y) {
    eg[++tt1]={y,hd[x]};
    hd[x]=tt1;
}
void bs(int x,int fa) {
    queue<array<int,3>>q;
    q.push({x,0,fa});
    while(q.size()) {
        int x=q.front()[0],d=q.front()[1],fa=q.front()[2];
        q.pop();
        arr[++tot]={d,x};
        for(int i=head[x];i;i=edg[i].y) {
            int to=edg[i].x;
            if(to==fa||vis[to]) continue;
            q.push({to,d+1,x});
        } 
    }
}
void calc(int x,int fa) {
    vis[x]=1;
    tot=0;
    bs(x,fa);
    rep(i,1,tot) {
        pr[i]=++idx;
        if(i!=1) add(pr[i],pr[i-1]);
        add(pr[i],arr[i].second);
    }
    int j=tot;
    rep(i,1,tot) {
        while(j>=1&&arr[j].first+arr[i].first>k) j--;
        if(j>=1) add(arr[i].second,pr[j]);
        else break;
    }
    for(int i=head[x];i;i=edg[i].y) {
        int to=edg[i].x;
        if(to==fa||vis[to]) continue;
        rt=0;
        sum=siz[to];
        dfs(to,0);
        calc(rt,0);
    }
}
int dis[N*20];
void bfs(int st) {
    deque<int>q;
    rep(i,1,idx) dis[i]=1e9,vis[i]=0;
    dis[st]=0;
    vis[st]=1;
    q.pb(st);
    while(q.size()) {
        int x=q.front();
        q.pop_front();
        vis[x]=false;
        for(int i=hd[x];i;i=eg[i].y){
            int to=eg[i].x;
            int pl=0;
            if(to<=n) {
                pl=s[to]-'0';
            }else pl=0;
            if(dis[to]>dis[x]+pl) {
                dis[to]=dis[x]+pl;
                if(!pl) {
                    q.push_front(to);
                    vis[to]=1;
                }else {
                    q.push_back(to);
                    vis[to]=1;
                }
            }
        }
    }
}
void solve() {
    cin>>n>>k>>s;
    s=" "+s;
    rep(i,1,n) {
        if(s[i]=='0') s[i]='1';
        else s[i]='0';
    }
    tt=tt1=0;
    rep(i,1,n)  head[i]=0,vis[i]=0;
    rep(i,1,idx) hd[i]=0;
    idx=n;
    rep(i,1,n-1) {
        int x,y;
        cin>>x>>y;
        add1(x,y);
        add1(y,x);
    }
    rt=0;
    mx[rt]=n+1;
    sum=n;
    dfs(1,0);
    calc(rt,0);
    bfs(1);
    rep(i,2,n) cout<<dis[i]<<' ';
}
fire main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    while(T--) {
        solve();
        if(T) cout<<endl;
    }
    return false;
}
/*
g++ jump.cpp -o j -std=c++14 -O2 "-Wl,--stack=1000000000"
*/

下面是 ai 写的代码,注意需要用 c++98 提交。

#include <bits/stdc++.h>
using namespace std;
namespace IO {
    const int BUF_SIZE = 1 << 20;
    char buf[BUF_SIZE], *p1 = buf, *p2 = buf;
    inline char gc() {
        if (p1 == p2) {
            p2 = (p1 = buf) + fread(buf, 1, BUF_SIZE, stdin);
            if (p1 == p2) return EOF;
        }
        return *p1++;
    }
    inline int read() {
        int x = 0, f = 1; char c = gc();
        while (c < '0' || c > '9') { if (c == '-') f = -1; c = gc(); }
        while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = gc(); }
        return x * f;
    }
    inline void read_str(char *s, int n) {
        char c = gc();
        while (c <= 32) c = gc();
        for (int i = 1; i <= n; ++i) { s[i] = c; c = gc(); }
    }
    char obuf[BUF_SIZE], *p3 = obuf;
    inline void pc(char c) {
        if (p3 - obuf == BUF_SIZE) {
            fwrite(obuf, 1, BUF_SIZE, stdout);
            p3 = obuf;
        }
        *p3++ = c;
    }
    inline void write(int x) {
        if (x < 0) { pc('-'); x = -x; }
        if (!x) { pc('0'); return; }
        static int tbl[20], t; t = 0;
        while (x) { tbl[++t] = x % 10; x /= 10; }
        while (t) pc(tbl[t--] + '0');
    }
    inline void flush() {
        fwrite(obuf, 1, p3 - obuf, stdout);
        p3 = obuf;
    }
}
const int N = 500005;
const int N_20 = 10000010;
const int N_80 = 40000010;
int n, k;
char s[N];
int head[N], edg_to[N * 2], edg_nxt[N * 2], tt;
inline void add1(int x, int y) {
    edg_to[++tt] = y;
    edg_nxt[tt] = head[x];
    head[x] = tt;
}
int hd[N_20], eg_to[N_80], eg_nxt[N_80], tt1;
inline void add(int x, int y) {
    eg_to[++tt1] = y;
    eg_nxt[tt1] = hd[x];
    hd[x] = tt1;
}
int siz[N], mx[N], vis[N_20];
int rt, sum;
void dfs(int x, int fa) {
    siz[x] = 1;
    mx[x] = 0;
    for (int i = head[x]; i; i = edg_nxt[i]) {
        int to = edg_to[i];
        if (to == fa || vis[to]) continue;
        dfs(to, x);
        siz[x] += siz[to];
        if (siz[to] > mx[x]) mx[x] = siz[to];
    }
    int rem = sum - siz[x];
    if (rem > mx[x]) mx[x] = rem;
    if (mx[x] < mx[rt]) rt = x;
}
int arr_d[N], arr_x[N], tot;
int pr[N], idx;
int q_x[N], q_d[N], q_fa[N];
inline void bs(int x, int fa) {
    int l = 1, r = 0;
    q_x[++r] = x; q_d[r] = 0; q_fa[r] = fa;
    while (l <= r) {
        int cur_x = q_x[l], cur_d = q_d[l], cur_fa = q_fa[l];
        l++;
        tot++;
        arr_d[tot] = cur_d;
        arr_x[tot] = cur_x;
        for (int i = head[cur_x]; i; i = edg_nxt[i]) {
            int to = edg_to[i];
            if (to == cur_fa || vis[to]) continue;
            q_x[++r] = to;
            q_d[r] = cur_d + 1;
            q_fa[r] = cur_x;
        }
    }
}
void calc(int x, int fa) {
    vis[x] = 1;
    tot = 0;
    bs(x, fa);
    for (int i = 1; i <= tot; ++i) {
        pr[i] = ++idx;
        if (i != 1) add(pr[i], pr[i - 1]);
        add(pr[i], arr_x[i]);
    }
    int j = tot;
    for (int i = 1; i <= tot; ++i) {
        while (j >= 1 && arr_d[j] + arr_d[i] > k) j--;
        if (j >= 1) add(arr_x[i], pr[j]);
        else break;
    }
    for (int i = head[x]; i; i = edg_nxt[i]) {
        int to = edg_to[i];
        if (to == fa || vis[to]) continue;
        rt = 0;
        sum = siz[to];
        dfs(to, 0);
        calc(rt, 0);
    }
}
int dis[N_20];
int q[1 << 24];
inline void bfs(int st) {
    int l = 0, r = 0;
    const int Q_MASK = (1 << 24) - 1;
    for (int i = 1; i <= idx; ++i) {
        dis[i] = 1e9;
        vis[i] = 0;
    }
    dis[st] = 0;
    vis[st] = 1;
    q[r] = st; r = (r + 1) & Q_MASK;
    while (l != r) {
        int x = q[l]; l = (l + 1) & Q_MASK;
        vis[x] = 0;
        for (int i = hd[x]; i; i = eg_nxt[i]) {
            int to = eg_to[i];
            int pl = 0;
            if (to <= n) {
                pl = (s[to] == '0' ? 1 : 0);
            }
            if (dis[to] > dis[x] + pl) {
                dis[to] = dis[x] + pl;
                if (!pl) {
                    if (!vis[to]) {
                        l = (l - 1) & Q_MASK;
                        q[l] = to;
                        vis[to] = 1;
                    }
                } else {
                    if (!vis[to]) {
                        q[r] = to; r = (r + 1) & Q_MASK;
                        vis[to] = 1;
                    }
                }
            }
        }
    }
}
void solve() {
    n = IO::read(); k = IO::read(); IO::read_str(s, n);
    tt = tt1 = 0;
    for (int i = 1; i <= n; ++i) { head[i] = 0; vis[i] = 0; }
    for (int i = 1; i <= idx; ++i) hd[i] = 0;
    idx = n;
    for (int i = 1; i < n; ++i) {
        int x = IO::read(), y = IO::read();
        add1(x, y);
        add1(y, x);
    }
    rt = 0;
    mx[rt] = n + 1;
    sum = n;
    dfs(1, 0);
    calc(rt, 0);
    bfs(1);
    for (int i = 2; i <= n; ++i) {
        IO::write(dis[i]);
        IO::pc(' ');
    }
}
int main() {
    int T = IO::read();
    while (T--) {
        solve();
        if (T) IO::pc('\n');
    }
    IO::flush();
    return 0;
}