题目 › usaco-1574

Dynamic Instability

USACO 2026 Second · 铂金组 · 2026-01 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Farmer Nhoj has trapped Bessie on a rooted tree with $N$ ($2 \le N \le 2 \cdot 10^5$) nodes, where node $1$ is the root. Scared and alone, Bessie makes the following move each second:

  • If Bessie's current node has no children, then she will move to a random ancestor of the current node (excluding the node itself).
  • Otherwise, Bessie will move to a random child of the current node.

Initially, Bessie is at node $x$, and her only way out is the exit located at node $y$ ($1\le x,y\le N$). For $Q$ ($1 \le Q \le 2 \cdot 10^5$) independent queries of $x$ and $y$, compute the expected number of seconds it would take Bessie to reach node $y$ for the first time if she started at node $x$, modulo $10^9+7$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$ and $Q$.

The next line contains $N-1$ integers $p_2, \ldots p_N$ describing the tree ($1\le p_i<i$). For each $2 \le i \le N$, there is an edge between nodes $i$ and $p_i$.

Each of the next $Q$ lines contains integers $x$ and $y$ representing the nodes for that query.

OUTPUT FORMAT (print output to the terminal / stdout):

For each query, output the expected number of seconds for Bessie to reach node $y$ for the first time starting at node $x$, modulo $10^9+7$.

SAMPLE INPUT:

5 5
1 2 2 1
1 1
2 1
3 1
4 1
5 1

SAMPLE OUTPUT:

0
4
3
3
1
In the $1$st query, the expected time to reach node $1$ from itself is $0$.

In the $3$rd query, after $1$ second, Bessie will be at node $1$ with probability $\frac{1}{2}$ and at node $2$ with probability $\frac{1}{2}$. Since the expected time to reach node $1$ from node $2$ is $4$, the expected time for Bessie to reach node $1$ starting at node $3$ is $1 + \frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 4 = 3$.

SAMPLE INPUT:

5 5
1 2 2 1
1 1
1 2
1 3
1 4
1 5

SAMPLE OUTPUT:

0
3
500000011
500000011
6
In the $3$rd query, the expected time to reach node $3$ from node $1$ is $\frac{15}{2}$.

SAMPLE INPUT:

13 10
1 2 2 4 3 1 5 6 4 7 8 10
1 12
10 6
5 12
1 13
13 10
6 4
7 12
3 1
12 8
2 1

SAMPLE OUTPUT:

166666700
21
2
166666701
500000023
18
166666704
750000018
800000021
500000018

SCORING:

  • Inputs 4-8: For all queries, $y=1$.
  • Inputs 9-13: For all queries, $x=1$.
  • Inputs 14-18: For each $2 \le i \le N$, $p_i$ is uniformly randomly chosen from the range $[1, i-1]$.
  • Inputs 19-23: No additional constraints.

Problem credits: Avnith Vijayram

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 29.8k平均输出 token(含思考) ↓
  • 26.4输出速度 tokens/s ↑
  • 1090 秒延迟 ↓
  • 0.49×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 13.0采样 3 · 答案错误
  • 29.9k平均输出 token(含思考) ↓
  • 26.4输出速度 tokens/s ↑
  • 1090 秒延迟 ↓
  • 0.55×CPU 时间 / 官方解 ↓
  • 同阶 2时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 48.2k平均输出 token(含思考) ↓
  • 170.3输出速度 tokens/s ↑
  • 288 秒延迟 ↓
  • 0.43×CPU 时间 / 官方解 ↓
  • 不可测 1、同阶 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 30.4采样 2 · 答案错误
  • 30.4采样 3 · 答案错误
  • 99.7k平均输出 token(含思考) ↓
  • 162.3输出速度 tokens/s ↑
  • 501 秒延迟 ↓
  • 0.36×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 181 行
#include <bits/stdc++.h>
using namespace std;

static const long long MOD = 1000000007LL;

long long modpow(long long a, long long e) {
    long long r = 1;
    while (e) {
        if (e & 1) r = r * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

inline long long norm(long long x) {
    x %= MOD;
    if (x < 0) x += MOD;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;

    vector<int> parent(N + 1), depth(N + 1);
    vector<vector<int>> children(N + 1);

    for (int v = 2; v <= N; ++v) {
        cin >> parent[v];
        depth[v] = depth[parent[v]] + 1;
        children[parent[v]].push_back(v);
    }

    vector<long long> invInt(N + 1);
    invInt[1] = 1;
    for (int i = 2; i <= N; ++i) {
        invInt[i] = MOD - (MOD / i) * invInt[MOD % i] % MOD;
    }

    // If a subtree contains no special boundary node, its root value is
    // A[v] + B[v] * (sum of values of its proper ancestors).
    vector<long long> A(N + 1), B(N + 1);
    vector<long long> sumA(N + 1), sumB(N + 1);

    for (int v = N; v >= 2; --v) {
        if (children[v].empty()) {
            A[v] = 1;
            B[v] = invInt[depth[v]];
        } else {
            long long d = (long long)children[v].size();
            long long den = norm(d - sumB[v]);
            long long invDen = modpow(den, MOD - 2);
            A[v] = norm(d + sumA[v]) * invDen % MOD;
            B[v] = sumB[v] * invDen % MOD;
        }

        int p = parent[v];
        sumA[p] = norm(sumA[p] + A[v]);
        sumB[p] = norm(sumB[p] + B[v]);
    }

    vector<long long> F(N + 1), SF(N + 1);       // Hitting time of the root.
    vector<long long> U(N + 1), SU(N + 1);       // Homogeneous path solution.
    vector<long long> R(N + 1), SR(N + 1);
    vector<long long> T(N + 1), ST(N + 1);
    vector<long long> prod(N + 1);

    R[1] = SR[1] = 1;
    prod[1] = 1;

    for (int v = 2; v <= N; ++v) {
        int u = parent[v];
        long long d = (long long)children[u].size();
        long long offA = norm(sumA[u] - A[v]);
        long long offB = norm(sumB[u] - B[v]);

        F[v] = norm(A[v] + B[v] * SF[u]);
        SF[v] = norm(SF[u] + F[v]);

        prod[v] = prod[u] * norm(1 + B[v]) % MOD;

        if (u == 1) {
            U[v] = 1;
            SU[v] = 1;
        } else {
            U[v] = norm(d * U[u] - offB * SU[u]);
            SU[v] = norm(SU[u] + U[v]);
        }

        R[v] = norm(d * R[u] - offB * SR[u]);
        T[v] = norm(d * T[u] - d - offA - offB * ST[u]);
        SR[v] = norm(SR[u] + R[v]);
        ST[v] = norm(ST[u] + T[v]);
    }

    vector<long long> invProd(N + 1), invU(N + 1), targetC(N + 1);

    for (int v = 1; v <= N; ++v) {
        invProd[v] = modpow(prod[v], MOD - 2);
    }

    for (int v = 2; v <= N; ++v) {
        invU[v] = modpow(U[v], MOD - 2);

        // Expected time from the root to v is -T[v]/R[v].
        // targetC[v] = F[v] + that expected time.
        targetC[v] = norm(F[v] - T[v] * modpow(R[v], MOD - 2));
    }

    int LOG = 1;
    while ((1 << LOG) <= N) ++LOG;

    vector<vector<int>> up(LOG, vector<int>(N + 1));
    up[0][1] = 1;
    for (int v = 2; v <= N; ++v) up[0][v] = parent[v];

    for (int k = 1; k < LOG; ++k) {
        for (int v = 1; v <= N; ++v) {
            up[k][v] = up[k - 1][up[k - 1][v]];
        }
    }

    auto lca = [&](int a, int b) {
        if (depth[a] < depth[b]) swap(a, b);

        int diff = depth[a] - depth[b];
        for (int k = 0; k < LOG; ++k) {
            if (diff & (1 << k)) a = up[k][a];
        }

        if (a == b) return a;

        for (int k = LOG - 1; k >= 0; --k) {
            if (up[k][a] != up[k][b]) {
                a = up[k][a];
                b = up[k][b];
            }
        }
        return parent[a];
    };

    while (Q--) {
        int x, y;
        cin >> x >> y;

        if (x == y) {
            cout << 0 << '\n';
            continue;
        }

        if (y == 1) {
            cout << F[x] << '\n';
            continue;
        }

        int l = lca(x, y);
        long long r; // Probability of reaching y before the root.

        if (l == x) {
            // x is on the root-to-y path.
            r = U[x] * invU[y] % MOD;
        } else {
            long long k = B[x] * prod[parent[x]] % MOD;
            r = k * invProd[l] % MOD;
            r = r * SU[l] % MOD;
            r = r * invU[y] % MOD;
        }

        long long ans = norm(
            F[x] - F[y] + norm(1 - r) * targetC[y]
        );

        cout << ans << '\n';
    }

    return 0;
}