ZR3775 归序

自评难度 \(5\)。

ZR3775 归序

给定一棵 \(n\) 个节点的无根树,边带正权。给出序列 \(a_1 \sim a_n\)。

称 \(1\sim n\) 的排列 \(p_i\) 是合法的,当且仅当对所有 \(k\),点集 \(\{p_1,p_2,\cdots,p_k\}\) 的直径 \(\le a_k\)。求合法的排列 \(p\) 数目。

\(n \le 700\),3 秒,512 MB。

点集直径是半群信息,考虑直接 DP:

  • 设 \(f_{i,x,y}\) 表示满足下述条件的 \(p_1 \sim p_i\) 选法数:
    • 点集 \(\{p_1,p_2,\cdots,p_i\}\) 的直径端点为 \(x\) 和 \(y\),且 \(x \le y\)。
    • \(\text{dis}(x,y) \le \min \limits_{j \ge i} a_j\)。
  • 转移枚举 \(z=p_i\) 分讨:
    • 若 \(\text{dis}(x,z)>\text{dis}(x,y)\) 或 \(\text{dis}(y,z) > \text{dis}(x,y)\),点 \(z\) 必然没有使用。
      • 若 \(\text{dis}(x,z)\ge \text{dis}(y,z)\),则 \(x \leadsto z\) 是新直径,转移 \(f_{i,x,y} \to f_{i+1,\min(x,z),\max(x,z)}\)。
      • 若 \(\text{dis}(y,z) > \text{dis}(x,z)\),则 \(y \leadsto z\) 是新直径,转移 \(f_{i,x,y} \to f_{i+1,\min(y,z),\max(y,z)}\)。
    • 若 \(\text{dis}(x,z)\le \text{dis}(x,y) \land \text{dis}(y,z) \le \text{dis}(x,y)\),对 \((x,y)\) 预处理对应 \(z\) 的数目为 \(q_{x,y}\)。这样的 \(z\) 恰好使用 \(i\) 个,转移 \((q_{x,y}-i)f_{i,x,y} \to f_{i+1,x,y}\)。
  • DP 初值为 \(f_{1,u,u}=1\)。
  • 答案为 \(\sum\limits_{x \le y} f_{n,x,y}\)。特判 \(n=1\) 的答案为 \(1\)。

对 \(i\) 维度滚动数组,复杂度 \(O(n^4)\)。

瓶颈在枚举 \(z\) 对满足 \(\text{dis}(x,y) < \text{dis}(x,z) \land \text{dis}(y,z) \le \text{dis}(x,z)\) 的 \((x,y)\) 求和 \(f_{i,x,y}\)。将原树以 \(x\) 定根,设深度 \(d_u=\text{dis}(x,u)\),令 \(x \leadsto u\) 根链的带权中点为 \(c_u\)。注意 \(c_u\) 可能是某点或某边中点。

结论 \(\text{dis}(x,y) < \text{dis}(x,z) \land \text{dis}(y,z) \le \text{dis}(x,z)\) 的充要条件是,\(c_y\) 是 \(c_z\) 的严格祖先。

证明 令 \(t=\text{lca}(y,z)\),将深度代入 \(\text{dis}\) 中

\[\text{dis}(y,z) \le \text{dis}(x,z) \iff d_y+d_z-2d_t\le d_z \iff d_t \ge \dfrac{d_y}{2} \]

从而 \(c_y\) 必须在 \(x \leadsto z\) 上。联立 \(d_y <d_z \Leftrightarrow d_{c_y}<d_{c_z}\) 得证。

取固定的 \(x\),将 \(f_{i,x,y}\) 挂到 \(c_y\) 的权值上,再对 \(c_z\) 求根链和贡献到 \(f_{i+1,\min(x,z),\max(x,z)}\)。只要做树上前缀和,注意处理 \(\text{dis}(x,z)= \text{dis}(y,z)\) 的情形。

可以在边内部插入节点,防止 \(c\) 落在边上。

复杂度 \(O(n^3)\),进行了大量常数优化才得以通过。

const int N = 705;
int n, q[N][N], c[N][N], fa[N], to[N][N], cnt[N], p[2*N];
i64 a[N], suf[N], d[N][N];
vector<pair<int, i64>> e[N];
void dfs(int u, i64* d, int* c) {
    int v = u;
    while (fa[v] && d[fa[v]] > d[u]/2) v = fa[v];
    c[u] = v;
    for (auto [v, w] : e[u]) if (v != fa[u])
        d[v] = d[u] + w, fa[v] = u, dfs(v, d, c);
}

vector<pair<i64, int>> s[N];
vector<int> t[N][2*N];
vector<pair<int, int>> seq[N];
bool vis[2*N];
void build(int x) {
    cnt[x] = n, fill(p+1, p+n+1, 0);
    for (int i = 1; i <= n; i++) s[i].clear();
    for (int y = 1; y <= n; y++) if (x != y) {
        i64 v = c[x][y], w = d[x][y];
        if (w % 2 == 0 && d[x][fa[v]] == w/2) c[x][y] = fa[v];
        else s[v].pb(w, y);
    }
    for (int v = 1; v <= n; v++) if (x != v) {
        sort(all(s[v]));
        int u = fa[v]; i64 ls = -1;
        for (auto [w, y] : s[v]) {
            if (w != ls) {
                int z = ++cnt[x];
                t[x][u].pb(z), p[z] = u;
                u = z, ls = w;
            }
            c[x][y] = u;
        }
        t[x][u].pb(v), p[v] = u;
    }
    for (int y = 1; y < x; y++) {
        int v = y;
        while (p[v] != c[x][y]) v = p[v];
        to[x][y] = v;
    }
    fill(vis+1, vis+cnt[x]+1, 0);
    for (int y = 1; y <= n; y++) vis[c[x][y]] = 1;
    auto dfs = [&](auto&& dfs, int u) -> bool {
        bool f = vis[u];
        for (int v : t[x][u]) f |= dfs(dfs, v);
        if (f && u != x) seq[x].pb(u, p[u]);
        return f;
    };
    dfs(dfs, x);
    reverse(all(seq[x]));
}

mint f[2][N][N]; u64 g[2*N], h[2*N], w[2*N];
void solve(int x) {
    h[x] = 0;
    for (auto [v, u] : seq[x]) h[v] = h[u] + g[u] + w[v];
}

void _main() {
    cin >> n;
    if (n == 1) return cout << 1, void();
    for (int i = 1; i <= n; i++) cin >> a[i];
    suf[n] = a[n];
    for (int i = n-1; i >= 1; i--) suf[i] = min(suf[i+1], a[i]);
    for (i64 i = 1, u, v, w; i < n; i++) {
        cin >> u >> v >> w;
        e[u].pb(v, w), e[v].pb(u, w);
    }
    for (int i = 1; i <= n; i++) 
        fill(fa+1, fa+n+1, 0), dfs(i, d[i], c[i]), build(i);
    for (int x = 1; x <= n; x++)
        for (int y = x; y <= n; y++) 
        for (int z = 1; z <= n; z++) 
        if (d[x][z] <= d[x][y] && d[y][z] <= d[x][y]) ++q[x][y];
    for (int u = 1; u <= n; u++) f[0][u][u] = 1;
    for (int i = 1, st = 0; i < n; i++, st ^= 1) {
        for (int x = 1; x <= n; x++) fill(f[st^1][x], f[st^1][x]+n+1, 0);
        for (int x = 1; x <= n; x++) {
            fill(g, g+cnt[x]+1, 0), fill(w, w+cnt[x]+1, 0);
            for (int y = x; y <= n; y++) {
                if (f[st][x][y] == 0) continue;
                f[st^1][x][y] += (q[x][y] - i) * f[st][x][y];
                g[c[x][y]] += f[st][x][y].val;
            }
            for (int y = 1; y < x; y++) w[to[x][y]] += f[st][x][y].val;
            solve(x);
            for (int z = 1; z <= n; z++) if (x != z) 
                f[st^1][x][z] += h[c[x][z]];
        }
        for (int x = 1; x <= n; x++)
            for (int y = x; y <= n; y++) {
                f[st^1][x][y] += f[st^1][y][x];
                if (d[x][y] > suf[i+1]) f[st^1][x][y] = 0;
                f[st^1][y][x] = f[st^1][x][y];
            }
    }
    mint res = 0;
    for (int x = 1; x <= n; x++)
        for (int y = x; y <= n; y++) 
            res += f[(n-1)&1][x][y];
    cout << res;
} 
posted @ 2026-11-18 18:20  stripe_python  阅读(7)  评论(0)    收藏  举报