题目 › usaco-1595

Random Tree Generation

USACO 2026 Third · 金组 · 2026-02 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Suppose the function $\text{randint}(l,r)$ returns an integer independently and uniformly at random from the range $[l, r]$.

Bessie generates a random labeled tree on $N$ vertices ($2 \le N \le 2\cdot10^5$) using the following two-step process:

  1. Start with vertices labeled $1$ through $N$. For each $i$ from $2$ to $N$, add an edge between vertex $i$ and $\text{randint}(1, i-1)$.
  2. Choose a permutation $p_1,p_2,\dots,p_N$ of $\{1, 2, \ldots, N\}$ uniformly at random. Relabel every vertex $v$ as $p_v$.

Now, Farmer John is looking at the edge set of the final tree and wants to know the probability that the two-step process above produces a tree with exactly this edge set. Can you determine this probability modulo $10^9+7$?

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

The input consists of $T$ ($1\le T\le 10$) independent inputs. Each input is specified as follows:

The first line contains $N$.

The next $N-1$ lines contain the edges of the tree specified by two space-separated integers $u$ and $v$ ($1\le u, v\le N$). It is guaranteed that these edges induce a tree.

It is guaranteed that the sum of $N$ across all tests does not exceed $5\cdot 10^5$.

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

For each test, output the probability modulo $10^9+7$ on a new line (note that the output probability is a ratio of integers, so you will want to print the result of this division when working modulo $10^9+7$).

SAMPLE INPUT:

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

SAMPLE OUTPUT:

1
333333336
83333334
55555556

The probabilities are $1$, $1/3$, $1/12$, $1/18$.

First test: There is only one tree on $N=2$ vertices, so the probability of generating it is just $1$.

Second test: there are three trees on $N=3$ vertices, and each of them is equally likely to have been generated by the process above. And $1/3\equiv 333333336\pmod{10^9+7}$.

SCORING:

  • Input 2-3: $N\le 8$
  • Inputs 4-9: $N\le 2000$
  • Inputs 10-21: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 3.6k平均输出 token(含思考) ↓
  • 27.9输出速度 tokens/s ↑
  • 117 秒延迟 ↓
  • 0.88×CPU 时间 / 官方解 ↓
  • 不确定 2、同阶 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 11.5k平均输出 token(含思考) ↓
  • 212.2输出速度 tokens/s ↑
  • 53 秒延迟 ↓
  • 0.86×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 13.5k平均输出 token(含思考) ↓
  • 178.4输出速度 tokens/s ↑
  • 70 秒延迟 ↓
  • 0.87×CPU 时间 / 官方解 ↓
  • 同阶 2、不确定 1时间复杂度(相对官方解)

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

代码

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

static const int MOD = 1000000007;
static const int MAXN = 200000;

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;
}

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

    vector<long long> inv(MAXN + 1), fact(MAXN + 1), invfact(MAXN + 1);
    inv[1] = 1;
    for (int i = 2; i <= MAXN; i++) {
        inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD;
    }

    fact[0] = 1;
    for (int i = 1; i <= MAXN; i++) {
        fact[i] = fact[i - 1] * i % MOD;
    }
    invfact[MAXN] = modpow(fact[MAXN], MOD - 2);
    for (int i = MAXN; i >= 1; i--) {
        invfact[i - 1] = invfact[i] * i % MOD;
    }

    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;

        vector<vector<int>> adj(N);
        for (int i = 0; i < N - 1; i++) {
            int u, v;
            cin >> u >> v;
            --u;
            --v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        vector<int> parent(N, -1), order;
        order.reserve(N);
        parent[0] = 0;
        order.push_back(0);

        for (int i = 0; i < (int)order.size(); i++) {
            int u = order[i];
            for (int v : adj[u]) {
                if (v == parent[u]) continue;
                parent[v] = u;
                order.push_back(v);
            }
        }

        vector<int> sub(N, 1);
        for (int i = N - 1; i >= 1; i--) {
            int u = order[i];
            sub[parent[u]] += sub[u];
        }

        // invProd[r] is the inverse of the product of subtree sizes
        // when the tree is rooted at r.
        vector<long long> invProd(N);
        invProd[0] = 1;
        for (int u = 0; u < N; u++) {
            invProd[0] = invProd[0] * inv[sub[u]] % MOD;
        }

        long long sum = invProd[0];

        for (int i = 0; i < N; i++) {
            int u = order[i];
            for (int v : adj[u]) {
                if (v == parent[u]) continue;

                int s = sub[v];
                invProd[v] = invProd[u] * s % MOD * inv[N - s] % MOD;
                sum += invProd[v];
                if (sum >= MOD) sum -= MOD;
            }
        }

        cout << sum * invfact[N - 1] % MOD << '\n';
    }

    return 0;
}