题目 › usaco-1571

The Chase

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

查看原题跳到结果

题面

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

Bessie is trying to evade the farmers. The farmers own $N$ ($2 \le N \le 5 \cdot 10^5$) farms with a one way road between the $i$-th farm and the $a_i$-th farm ($1 \le i \le N$, $a_i \neq i$). There are $F$ ($1 \le F \le N$) farmers and the $i$-th farmer is initially stationed at farm $s_i$ ($1 \le s_i \le N$, all $s_i$ unique). At each time step, every farmer takes the road at their current farm and moves to the next. Bessie gets caught if she is ever located at the same farm as any farmer.

Suppose Bessie starts at some farm $b$. At each time step, she has two options: she can either choose to rest (stay at her current farm) or take the road and move to the next farm. If she chooses to move, she moves simultaneously with the farmers. Bessie moves so that she is never caught by any farmer in a finite number of timesteps.

For each starting farm $b$ ($1 \le b \le N$), find the maximum number of times Bessie can choose the rest option if she starts at farm $b$.

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

The first line contains $N$ and $F$, the number of farms and the number of farmers.

The second line contains $a_1 \ldots a_N$, the one-way roads going out of each farm.

The third line contains $s_1 \ldots s_F$, the starting locations of each farmer.

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

Output $N$ lines, the $b$-th of which must consist of a single integer denoting the maximum number of times Bessie can choose the rest option if she starts at farm $b$. If there is no way for Bessie to ensure she is never caught after a finite number of timesteps, then output $-1$. If Bessie can rest an infinite number of times, then output $-2$.

SAMPLE INPUT:

4 1
2 1 4 3
1

SAMPLE OUTPUT:

-1
0
-2
-2

  • Farm 1: If Bessie starts at a farm with a farmer, then she is caught immediately, and you should output $-1$.
  • Farm 2: Bessie must choose to move at every timestep to avoid being caught by the farmer who starts at farm $1$.
  • Farms 3-4: Bessie can rest an infinite number of times without being caught.

SCORING:

  • Input 2: $N \le 50$
  • Inputs 3-10: $N \le 2000$
  • Inputs 11-20: No additional constraints.

Problem credits: Alex Liang

结果

Kimi K3.1 ProKimi

auto(max)

1/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

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

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

代码

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

struct Component {
    int len = 0;
    vector<int> safeDist;
};

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

    int N, F;
    cin >> N >> F;

    vector<int> nxt(N);
    vector<vector<int>> radj(N);
    vector<int> indeg(N, 0);

    for (int i = 0; i < N; ++i) {
        cin >> nxt[i];
        --nxt[i];
        radj[nxt[i]].push_back(i);
        ++indeg[nxt[i]];
    }

    vector<int> farmers(F);
    for (int &s : farmers) {
        cin >> s;
        --s;
    }

    // Earliest time at which any farmer visits each farm.
    vector<int> dist(N, -1);
    vector<int> queue(N);
    int qh = 0, qt = 0;

    for (int s : farmers) {
        dist[s] = 0;
        queue[qt++] = s;
    }

    while (qh < qt) {
        int v = queue[qh++];
        int u = nxt[v];
        if (dist[u] == -1) {
            dist[u] = dist[v] + 1;
            queue[qt++] = u;
        }
    }

    // Remove all non-cycle vertices; remaining vertices are exactly cycle vertices.
    qh = qt = 0;
    for (int i = 0; i < N; ++i) {
        if (indeg[i] == 0) queue[qt++] = i;
    }

    while (qh < qt) {
        int v = queue[qh++];
        int u = nxt[v];
        if (--indeg[u] == 0) queue[qt++] = u;
    }

    vector<int> comp(N, -1), potential(N, 0);
    vector<Component> components;

    // Assign cycle positions. potential increases by 1 modulo the cycle length
    // whenever any vertex moves along its outgoing edge.
    for (int i = 0; i < N; ++i) {
        if (indeg[i] > 0 && comp[i] == -1) {
            int cid = (int)components.size();
            components.push_back(Component());

            int v = i;
            int pos = 0;
            do {
                comp[v] = cid;
                potential[v] = pos++;
                v = nxt[v];
            } while (v != i);

            components[cid].len = pos;
        }
    }

    // Propagate component IDs and potentials into the trees feeding the cycles.
    vector<int> st;
    for (int i = 0; i < N; ++i) {
        if (indeg[i] <= 0) continue;

        st.push_back(i);
        while (!st.empty()) {
            int v = st.back();
            st.pop_back();

            int cid = comp[v];
            int L = components[cid].len;

            for (int u : radj[v]) {
                if (comp[u] != -1) continue;

                comp[u] = cid;
                potential[u] = potential[v] - 1;
                if (potential[u] < 0) potential[u] += L;
                st.push_back(u);
            }
        }
    }

    // Phases occupied by farmers are unsafe for Bessie's eventual motion.
    for (auto &c : components) c.safeDist.assign(c.len, 0);

    for (int s : farmers) {
        Component &c = components[comp[s]];
        c.safeDist[potential[s]] = -1;
    }

    const int INF = 1e9;

    // For every phase, find the distance forward to the nearest safe phase.
    for (auto &c : components) {
        int safePhase = -1;
        for (int p = 0; p < c.len; ++p) {
            if (c.safeDist[p] == 0) {
                safePhase = p;
                break;
            }
        }

        if (safePhase == -1) {
            fill(c.safeDist.begin(), c.safeDist.end(), INF);
            continue;
        }

        int d = 0;
        for (int step = 1; step < c.len; ++step) {
            int p = safePhase - step;
            if (p < 0) p += c.len;

            if (c.safeDist[p] == 0) {
                d = 0;
            } else {
                c.safeDist[p] = ++d;
            }
        }
    }

    for (int b = 0; b < N; ++b) {
        if (dist[b] == -1) {
            cout << -2 << '\n';
            continue;
        }

        if (dist[b] == 0) {
            cout << -1 << '\n';
            continue;
        }

        int r = dist[b] - 1;
        Component &c = components[comp[b]];

        int phase = potential[b] - (r % c.len);
        if (phase < 0) phase += c.len;

        int delta = c.safeDist[phase];
        if (delta > r) {
            cout << -1 << '\n';
        } else {
            cout << r - delta << '\n';
        }
    }

    return 0;
}