跳到正文

题解

题解:P17141 [NOI 2026] 传送

_Z_F_R_约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年7月26日
收录
2026年8月1日
更新
2026年11月10日
篇幅
约 12 分钟
题目
P17141

版本

  1. v1当前

首先,你走至少一步到一个传送门一定不优。所以对于 (x,y)(x, y),最终的通行方式一定形如,选择一个包含 yy 的连通块 SS,以 yy 为根,SS 内的点向根移动,非 SS 内的点使用传送门移动。

令 dis⁡(u,v)\operatorname{dis}(u, v) 为 u⇝vu \rightsquigarrow v 路径经过的边数,答案为:

{dis⁡(x,y)x∈Sn+∑z∈Sdis⁡(y,z)∣S∣x∉S\begin{cases} \operatorname{dis}(x, y) & x \in S \\ \dfrac{n + \sum_{z \in S} \operatorname{dis}(y, z)}{|S|} & x \not \in S \end{cases}

考虑贪心,每次加入当前距离 yy 最近的点,可以做到 O(nm)O(nm)。

继续观察到一个显然的性质是,每次总是会加入一个深度的所有点。

进一步,实际上答案是 O(n)O(\sqrt n) 的,直接做是 O((n+m)n)O((n + m)\sqrt n),但是没啥用。

继续考虑,下面的式子与 xx 无关,如果加入到深度为 dd 的所有点之后,式子取到最小值,那么下面式子的值 tt 满足 t∈[d,d+1]t \in [d, d + 1],那么:

  • 若 d<dis⁡(x,y)d < \operatorname{dis}(x, y),那么直接取 tt 作为答案;
  • 否则直接取 dis⁡(x,y)\operatorname{dis}(x, y) 作为答案。

做到 O(nn+m)O(n \sqrt n + m),精细实现已经可以通过。

瓶颈是对于每个 uu 求出 tut_u,进一步的,考虑相邻结点 u,vu, v,则 ∣tu−tv∣≤1|t_u - t_v| \le 1,证明直接考虑实际意义,令 au←va_u \gets v,av←−1a_v \gets -1 直接得到一组合法解 tv′t'_v,且 tv≤tv′≤tu+1t_v \le t'_v \le t_u + 1,反之亦然。

使用点分树查询邻域信息即可,就是 O(nlog⁡n+m)O(n \log n + m)。

写完之后咋感觉跑不过根号。

C++
#include <bits/stdc++.h>
#include "teleport.h"
#define ll long long
using namespace std;
namespace qwq {
    const int N = 500005;
    int n, pos[N], rk[N], dep[N], siz[N], tfa[N], cnto, o[N << 1];
    bool vis[N];
    vector<int> e[N];
    namespace ST {
        int lg[N << 1], f[20][N << 1];
        void Init(int n, int *a) {
            int i, j;
            for(i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
            for(i = 1; i <= n; i++) f[0][i] = a[i];
            for(i = 1; i <= lg[n]; i++) for(j = 1; j + (1 << i) - 1 <= n; j++) {
                f[i][j] = min(f[i - 1][j], f[i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    inline int Dis(int u, int v) {
        int l = pos[u], r = pos[v]; if(l > r) swap(l, r);
        int x = ST::lg[r - l + 1]; return dep[u] + dep[v] - min(ST::f[x][l], ST::f[x][r - (1 << x) + 1]) * 2;
    }
    void Dfs(int u, int fa) {
        dep[u] = dep[fa] + 1, o[++cnto] = dep[u], pos[u] = cnto;
        for(auto v : e[u]) if(v != fa) Dfs(v, u), o[++cnto] = dep[u];
    }
    void Count(int &tot, int u, int fa) { tot++; for(auto v : e[u]) if(v != fa && !vis[v]) Count(tot, v, u); }
    void Find_Center(int tot, int &c, int u, int fa) {
        int maxsiz = 0; siz[u] = 1;
        for(auto v : e[u]) if(v != fa && !vis[v]) {
            Find_Center(tot, c, v, u), siz[u] += siz[v], maxsiz = max(maxsiz, siz[v]);
        }
        if(max(maxsiz, tot - siz[u]) <= tot / 2) c = u;
    }
    vector<int> a[N], c[N];
    vector<ll> b[N], d[N];
    inline void Add(vector<int> &vec, int x, int d) {
        while(vec.size() <= x) vec.emplace_back(0);
        vec[x] += d;
    }
    void DfsAdd(int u, int fa, vector<int> &f, int cdep) {
        Add(f, cdep, 1);
        for(auto v : e[u]) if(v != fa && !vis[v]) DfsAdd(v, u, f, cdep + 1); 
    }
    int Build(int u) {
        int tot = 0; Count(tot, u, 0), Find_Center(tot, u, u, 0), Add(a[u], 0, 1), vis[u] = true;
        for(auto v : e[u]) if(!vis[v]) {
            vector<int> tmp; DfsAdd(v, u, tmp, 1);
            int w = Build(v), i; tfa[w] = u, c[w].resize(tmp.size() - 1);
            for(i = tmp.size() - 1; i >= 0; i--) {
                if(i) c[w][i - 1] = tmp[i];
                Add(a[u], i, tmp[i]);
            }
        }
        return u;
    }
    struct Frac {
        ll a, b;
        friend bool operator <=(Frac x, Frac y) {
            return x.a * y.b <= y.a * x.b;
        }
        friend bool operator <(Frac x, Frac y) {
            return x.a * y.b < y.a * x.b;
        }
    };
    inline int Get(vector<int> &vec, int x) { return x < 0 || vec.empty() ? 0ll : (vec.size() <= x ? vec.back() : vec[x]); }
    inline ll Get(vector<ll> &vec, int x) { return x < 0 || vec.empty() ? 0ll : (vec.size() <= x ? vec.back() : vec[x]); }
    unordered_map<ll, Frac> mp;
    Frac Query(int s, int t) {
        if(mp.count(1ll * s * N + t)) return mp[1ll * s * N + t];
        Frac res = {n, 0};
        int u = s, v = 0;
        while(u) {
            int x = Dis(s, u), y;
            y = Get(a[u], t - x), res.b += y, res.a += Get(b[u], t - x) + 1ll * x * y;
            y = Get(c[v], t - x - 1), res.b -= y, res.a -= Get(d[v], t - x - 1) + 1ll * (x + 1) * y;
            v = u, u = tfa[u];
        }
        // cerr << s << ' ' << t << ' ' << res.a << ' ' << res.b << endl;
        return mp[1ll * s * N + t] = res;
    }
    bool Check(int s, int x) { return Query(s, x) <= Query(s, x + 1); }
    int f[N];
    Frac g[N];
    void Dfs2(int u, int fa) {
        g[u] = Query(u, f[u]);
        for(auto v : e[u]) if(v != fa) {
            f[v] = max(f[u] - 1, 0);
            while(f[v] < n && !Check(v, f[v])) f[v]++;
            Dfs2(v, u);
        }
    }
    inline void Set(vector<ll> &x, vector<int> &y) {
        int i, siz = y.size(); x.resize(siz);
        for(i = 1; i < siz; i++) x[i] = x[i - 1] + 1ll * y[i] * i, y[i] += y[i - 1];
    }
    void Solve() {
        int i; Dfs(1, 0), ST::Init(cnto, o), Build(1), f[1] = 0;
        for(i = 1; i <= n; i++) Set(b[i], a[i]), Set(d[i], c[i]);
        // for(i = 1; i <= n; i++) {
        //     cerr << "node " << i << ' ' << tfa[i] << endl;
        //     cerr << "a : "; for(auto v : a[i]) cerr << v << ' '; cerr << endl;
        //     cerr << "b : "; for(auto v : b[i]) cerr << v << ' '; cerr << endl;
        //     cerr << "c : "; for(auto v : c[i]) cerr << v << ' '; cerr << endl;
        //     cerr << "d : "; for(auto v : d[i]) cerr << v << ' '; cerr << endl;
        // }
        while(f[1] < n && !Check(1, f[1])) f[1]++;
        Dfs2(1, 0);
        // for(i = 1; i <= n; i++) cerr << i << " : " << f[i] << " | " << g[i].a << '/' << g[i].b << endl;
    }
    pair<ll, int> Report(int x, int y) { Frac res = min(Frac({Dis(x, y), 1}), g[y]); return make_pair(res.a, int(res.b)); }
}
vector<pair<ll, int> > teleport(int c, int n, int m, vector<int> u, vector<int> v, vector<int> x, vector<int> y) {
    int i; vector<pair<ll, int> > vec; qwq::n = n;
    for(i = 0; i < n - 1; i++) qwq::e[u[i] + 1].emplace_back(v[i] + 1), qwq::e[v[i] + 1].emplace_back(u[i] + 1);
    qwq::Solve();
    for(i = 0; i < m; i++) vec.emplace_back(qwq::Report(x[i] + 1, y[i] + 1));
    for(auto &v : vec) { ll g = __gcd(v.first, (ll)v.second); v.first /= g, v.second /= g; }
    return vec;
}
原文
存档评论1 条抓取于