题目 › fcs-304

FrontierCS #304

FrontierCS 算法赛道 v1.1 · 2026-05 · L5 开放式 · C++17 · 时限 2 秒 · 0–100 连续分

查看原题跳到结果

题面

来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。

# Forest Mycelium Delay Tour (Optimization Challenge) ## Problem Vasya is mapping a forest trail network with **mushroom clearings**. The forest is modeled as an undirected connected graph with `N` clearings (vertices) and `M` trails (edges). At clearing `i`, mushrooms grow at a constant rate `r[i]` grams per minute, starting from **0 grams at time 0**. - Vasya starts at clearing `1` at time `t = 0`. - Each minute, Vasya **must** traverse exactly one trail to an adjacent clearing (no waiting). - When Vasya **first arrives** at a clearing `i` at time `t`, he instantly collects **all mushrooms grown there so far**, equal to `r[i] * t` grams. - After the first collection at clearing `i`, that clearing becomes barren and yields **0** on future visits. - Vasya will walk for exactly `K` minutes (i.e., traverse exactly `K` edges), ending anywhere. - To be feasible, his walk must visit **every clearing at least once** during times `0..K`. Your goal is to output a feasible walk that **maximizes the total collected mushroom weight**. This is an open-ended optimization task: many solutions are valid, and they receive different scores. ## Input ``` N M K r1 r2 ... rN u1 v1 u2 v2 ... uM vM ``` - `1 ≤ N ≤ 20000` - `N-1 ≤ M ≤ 60000` - `2·(N-1) ≤ K ≤ 200000` - `1 ≤ r[i] ≤ 10^6` - `1 ≤ uj, vj ≤ N`, `uj != vj` - The graph is connected. - Multiple edges may appear; self-loops do not appear. The constraints guarantee that a walk of length `K` visiting all vertices exists. ## Output Output exactly `K` integers: ``` x1 x2 ... xK ``` where `xt` is the clearing Vasya is at **after** minute `t` (after the `t`-th move). Feasibility requirements: - Vasya starts at clearing `1` at time `0`. - For each `t = 1..K`, the move from the previous clearing to `xt` must follow an existing trail (edge). - Every clearing `1..N` must appear at least once among the visited clearings at times `0..K` (including the start at time 0). If the output is infeasible, the solution receives **0 score for that test**. ## Objective Let `pos(t)` be Vasya’s clearing at time `t` (`pos(0)=1`, `pos(t)=xt` for `t≥1`). For each clearing `i`, define its first-visit time: - `T[i] = min { t ∈ [0..K] | pos(t) = i }` Total collected weight: \[ V = \sum_{i=1}^{N} r[i] \cdot T[i] \] (Notice `T[1]=0`, so clearing 1 contributes `0`.) You must **maximize** `V`. ## Scoring The physical objective is weighted walk reward V=computeValue(walk); larger is better. The physical reference is `a = 2.5*B`; matching a positive reference earns 60 points. Define the historical low-score expression (in points): P_low = 100*max(0,1-B/V) if V>0; 0 otherwise. If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award: r = V / a P = min(100, 60 + ln(r)). Here `r` is the improvement factor relative to the physical reference, and `ln` is the natural logarithm with coefficient 1. In the uncapped upper branch, `r = exp(P-60)`: each additional point means an `exp(1)`-fold improvement in the physical objective. The factor is computed from the physical objective and its reference, not from a transformed reward or the old point score. Invalid output retains its existing rejection or zero score; the zero-cost exception applies only after validation. Use one score per file. Both reported ratio channels use points divided by 100. Across official files, take the arithmetic mean. B is the reward of the deterministic baseline walk. If B=0 and V>0, no positive 60-point physical reference exists; retain the existing 100 branch. ## Constraints - Time limit: 2 seconds - Memory limit: 256 MB ## Example Sample input: ``` 4 4 6 5 1 4 10 1 2 2 3 3 4 1 4 ``` One valid sample output: ``` 2 3 2 1 4 3 ``` Explanation (high level): - Path: time 0 at 1, time 1 at 2, time 2 at 3, time 3 at 2, time 4 at 1, time 5 at 4, time 6 at 3. - First-visit times: `T[1]=0, T[2]=1, T[3]=2, T[4]=5`. - Objective value: `V = 5·0 + 1·1 + 4·2 + 10·5 = 59`. - The judge also computes the baseline value `B` for this test, then computes S=100*max(0,1-B/V) when V>0 and applies the piecewise scoring formula above.

结果

Kimi K3.1 ProKimi

max

58.5

平均得分 ↑

  • 58.5采样 1 · 部分分
  • 41.9k平均输出 token(含思考) ↓
  • 26.8输出速度 tokens/s ↑
  • 1567 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

58.5

平均得分 ↑

  • 58.5采样 1 · 部分分
  • 13.1k平均输出 token(含思考) ↓
  • 24.7输出速度 tokens/s ↑
  • 532 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

56.2

平均得分 ↑

  • 56.2采样 1 · 部分分
  • 41.8k平均输出 token(含思考) ↓
  • 177.1输出速度 tokens/s ↑
  • 237 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

0.0

平均得分 ↑

  • 0.0采样 1 · 编译错误
  • 83.1k平均输出 token(含思考) ↓
  • 147.7输出速度 tokens/s ↑
  • 564 秒延迟 ↓

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

代码

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

using ll = long long;
const ll NEG = numeric_limits<ll>::min() / 4;

struct Tree {
    vector<vector<int>> ch;
    vector<int> order;
};

struct DPResult {
    vector<array<ll, 2>> val;
    vector<array<int, 2>> len;
    vector<array<int, 2>> to;
    vector<array<int, 2>> toPar;
};

struct Task {
    int kind, v, p;
};

struct Best {
    ll value = -1;
    vector<int> base;
    int prefixKind = 0;
    int prefixLen = 0;
    int x = -1, y = -1;
};

struct Solver {
    int N, M, K;
    vector<ll> r;
    ll totalR = 0;
    vector<vector<int>> adj, adjSorted;
    vector<int> eu, ev;
    unordered_set<unsigned long long> edges;
    vector<pair<int, int>> triangles;
    Best best;

    static unsigned long long edgeKey(int a, int b) {
        if (a > b) swap(a, b);
        return (unsigned long long)(unsigned int)a << 32 | (unsigned int)b;
    }

    bool hasEdge(int a, int b) const {
        return edges.find(edgeKey(a, b)) != edges.end();
    }

    Tree buildTree(bool dfs, int mode) {
        Tree t;
        t.ch.resize(N + 1);
        t.order.reserve(N);
        vector<char> vis(N + 1, 0);
        vis[1] = 1;
        t.order.push_back(1);

        auto listSize = [&](int v) {
            return (int)(mode == 0 ? adj[v].size() : adjSorted[v].size());
        };
        auto getNeighbor = [&](int v, int i) {
            const vector<int> &lst = (mode == 0 ? adj[v] : adjSorted[v]);
            int idx = (mode == 2 ? (int)lst.size() - 1 - i : i);
            return lst[idx];
        };

        if (!dfs) {
            for (size_t head = 0; head < t.order.size(); ++head) {
                int v = t.order[head];
                int d = listSize(v);
                for (int i = 0; i < d; ++i) {
                    int u = getNeighbor(v, i);
                    if (!vis[u]) {
                        vis[u] = 1;
                        t.ch[v].push_back(u);
                        t.order.push_back(u);
                    }
                }
            }
        } else {
            vector<int> it(N + 1, 0), st = {1};
            while (!st.empty()) {
                int v = st.back();
                bool advanced = false;
                int d = listSize(v);
                while (it[v] < d) {
                    int u = getNeighbor(v, it[v]++);
                    if (!vis[u]) {
                        vis[u] = 1;
                        t.ch[v].push_back(u);
                        t.order.push_back(u);
                        st.push_back(u);
                        advanced = true;
                        break;
                    }
                }
                if (!advanced) st.pop_back();
            }
        }
        return t;
    }

    DPResult runDP(Tree &tree, const vector<ll> &w, ll lambda) {
        DPResult d;
        d.val.resize(N + 1);
        d.len.resize(N + 1);
        d.to.resize(N + 1);
        d.toPar.resize(N + 1);
        for (int i = 1; i <= N; ++i) {
            d.val[i] = {NEG, NEG};
            d.len[i] = {INT_MAX, INT_MAX};
            d.to[i] = {-1, -1};
            d.toPar[i] = {-1, -1};
        }

        vector<ll> subW(N + 1), closed(N + 1, 0);
        vector<int> sz(N + 1);
        int maxDeg = 0;
        for (int v = 1; v <= N; ++v)
            maxDeg = max(maxDeg, (int)tree.ch[v].size());

        vector<ll> prefV(maxDeg + 1), prefT(maxDeg + 1);
        vector<ll> suffV(maxDeg + 1), suffT(maxDeg + 1), suffW(maxDeg + 1);

        for (int oi = N - 1; oi >= 0; --oi) {
            int v = tree.order[oi];
            auto &ch = tree.ch[v];
            sz[v] = 1;
            subW[v] = w[v];
            for (int c : ch) {
                sz[v] += sz[c];
                subW[v] += subW[c];
            }

            sort(ch.begin(), ch.end(), [&](int a, int b) {
                __int128 lhs = (__int128)subW[a] * sz[b];
                __int128 rhs = (__int128)subW[b] * sz[a];
                if (lhs != rhs) return lhs < rhs;
                if (sz[a] != sz[b]) return sz[a] < sz[b];
                return a < b;
            });

            int deg = ch.size();
            prefV[0] = prefT[0] = 0;
            for (int i = 0; i < deg; ++i) {
                int c = ch[i];
                ll A = closed[c] - 2 * lambda + subW[c];
                prefV[i + 1] = prefV[i] + A + subW[c] * prefT[i];
                prefT[i + 1] = prefT[i] + 2LL * sz[c];
            }
            closed[v] = prefV[deg];

            suffV[deg] = suffT[deg] = suffW[deg] = 0;
            for (int i = deg - 1; i >= 0; --i) {
                int c = ch[i];
                ll A = closed[c] - 2 * lambda + subW[c];
                ll duration = 2LL * sz[c];
                suffV[i] = A + suffV[i + 1] + suffW[i + 1] * duration;
                suffT[i] = duration + suffT[i + 1];
                suffW[i] = subW[c] + suffW[i + 1];
            }

            if (deg == 0) {
                d.val[v][0] = 0;
                d.len[v][0] = 0;
                continue;
            }

            for (int i = 0; i < deg; ++i) {
                int f = ch[i];
                ll others = prefV[i] + suffV[i + 1]
                          + suffW[i + 1] * prefT[i];
                ll start = prefT[i] + suffT[i + 1];

                for (int q = 0; q < 2; ++q) {
                    if (d.val[f][q] == NEG) continue;
                    ll cand = others + d.val[f][q] - lambda
                            + subW[f] * (start + 1);
                    int p = q ^ 1;
                    int length = (int)start + 1 + d.len[f][q];
                    if (cand > d.val[v][p] ||
                        (cand == d.val[v][p] && length < d.len[v][p])) {
                        d.val[v][p] = cand;
                        d.len[v][p] = length;
                        d.to[v][p] = f;
                        d.toPar[v][p] = q;
                    }
                }
            }
        }
        return d;
    }

    vector<int> buildRoute(const Tree &tree, const DPResult &d, int par) {
        vector<int> base;
        base.reserve(2 * N);
        vector<Task> st;
        st.push_back({1, 1, par});

        while (!st.empty()) {
            Task task = st.back();
            st.pop_back();

            if (task.kind == 2) {
                base.push_back(task.v);
                continue;
            }

            int v = task.v;
            const auto &ch = tree.ch[v];

            if (task.kind == 0) {
                for (int i = (int)ch.size() - 1; i >= 0; --i) {
                    int c = ch[i];
                    st.push_back({2, v, 0});
                    st.push_back({0, c, 0});
                    st.push_back({2, c, 0});
                }
            } else {
                int p = task.p;
                int f = d.to[v][p];
                if (f == -1) continue;
                int q = d.toPar[v][p];

                st.push_back({1, f, q});
                st.push_back({2, f, 0});

                for (int i = (int)ch.size() - 1; i >= 0; --i) {
                    int c = ch[i];
                    if (c == f) continue;
                    st.push_back({2, v, 0});
                    st.push_back({0, c, 0});
                    st.push_back({2, c, 0});
                }
            }
        }
        return base;
    }

    vector<int> shortcut(const vector<int> &base) {
        int L = base.size();
        if (L < 2) return base;

        int S = L + 1;
        vector<int> seq(S);
        seq[0] = 1;
        for (int i = 0; i < L; ++i) seq[i + 1] = base[i];

        vector<int> pre(S), nxt(S), cnt(N + 1, 0);
        vector<char> alive(S, 1);
        for (int i = 0; i < S; ++i) {
            pre[i] = i - 1;
            nxt[i] = (i + 1 < S ? i + 1 : -1);
            ++cnt[seq[i]];
        }

        vector<int> q;
        q.reserve(4 * S);
        for (int i = 1; i < L; ++i) q.push_back(i);

        for (size_t head = 0; head < q.size(); ++head) {
            int b = q[head];
            if (!alive[b]) continue;
            int a = pre[b], c = nxt[b];
            if (a < 0 || c < 0 || !alive[a] || !alive[c]) continue;

            int va = seq[a], vb = seq[b], vc = seq[c];

            if (va == vc) {
                if (cnt[vb] <= 1) continue;
                int d = nxt[c];
                nxt[a] = d;
                if (d != -1) pre[d] = a;
                alive[b] = alive[c] = 0;
                --cnt[vb];
                --cnt[va];
                q.push_back(a);
                if (d != -1) q.push_back(d);
            } else {
                if (cnt[vb] <= 1 || !hasEdge(va, vc)) continue;
                nxt[a] = c;
                pre[c] = a;
                alive[b] = 0;
                --cnt[vb];
                q.push_back(a);
                q.push_back(c);
            }
        }

        vector<int> out;
        out.reserve(L);
        int node = 0;
        while (nxt[node] != -1) {
            node = nxt[node];
            out.push_back(seq[node]);
        }
        return out;
    }

    void consider(const vector<int> &base) {
        int L = base.size();
        if (L > K) return;

        vector<int> T(N + 1, -1);
        T[1] = 0;
        int seen = 1;
        ll baseV = 0;

        for (int i = 0; i < L; ++i) {
            int v = base[i];
            if (T[v] == -1) {
                T[v] = i + 1;
                baseV += r[v] * (i + 1LL);
                ++seen;
            }
        }
        if (seen != N) return;

        int D = K - L;

        auto update = [&](ll value, int kind, int plen, int x, int y) {
            if (value > best.value) {
                best.value = value;
                best.base = base;
                best.prefixKind = kind;
                best.prefixLen = plen;
                best.x = x;
                best.y = y;
            }
        };

        int p = D - (D & 1);
        if (p == 0) {
            update(baseV, 0, 0, -1, -1);
        } else {
            ll constant = baseV + (ll)p * (totalR - r[1]);
            ll localBest = NEG;
            int localA = -1;
            for (int a : adj[1]) {
                ll value = constant + r[a] * (1LL - p - T[a]);
                if (value > localBest) {
                    localBest = value;
                    localA = a;
                }
            }
            if (localA != -1) update(localBest, 0, p, localA, -1);
        }

        if ((D & 1) && D >= 3 && !triangles.empty()) {
            ll constant = baseV + (ll)D * (totalR - r[1]);
            ll localBest = NEG;
            int localA = -1, localB = -1;
            for (auto [a, b] : triangles) {
                ll value = constant
                         + r[a] * (1LL - D - T[a])
                         + r[b] * (-1LL - T[b]);
                if (value > localBest) {
                    localBest = value;
                    localA = a;
                    localB = b;
                }
            }
            if (localA != -1)
                update(localBest, 1, D, localA, localB);
        }
    }

    void considerWithShortcuts(const vector<int> &base) {
        consider(base);
        vector<int> shorter = shortcut(base);
        if (shorter.size() != base.size()) consider(shorter);
    }

    vector<int> fallbackDFS() {
        vector<int> base;
        base.reserve(2 * N);
        vector<char> vis(N + 1, 0);
        vector<int> it(N + 1, 0), st = {1};
        vis[1] = 1;

        while (!st.empty()) {
            int v = st.back();
            while (it[v] < (int)adj[v].size() && vis[adj[v][it[v]]])
                ++it[v];

            if (it[v] == (int)adj[v].size()) {
                st.pop_back();
                if (!st.empty()) base.push_back(st.back());
            } else {
                int u = adj[v][it[v]++];
                vis[u] = 1;
                base.push_back(u);
                st.push_back(u);
            }
        }
        return base;
    }

    void run() {
        cin >> N >> M >> K;
        r.assign(N + 1, 0);
        for (int i = 1; i <= N; ++i) {
            cin >> r[i];
            totalR += r[i];
        }

        adj.assign(N + 1, {});
        eu.resize(M);
        ev.resize(M);
        edges.reserve(2 * M + 1);

        for (int i = 0; i < M; ++i) {
            int u, v;
            cin >> u >> v;
            eu[i] = u;
            ev[i] = v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            edges.insert(edgeKey(u, v));
        }

        if (N == 1) return;

        adjSorted = adj;
        for (int v = 1; v <= N; ++v) {
            sort(adjSorted[v].begin(), adjSorted[v].end(),
                 [&](int a, int b) {
                     if (r[a] != r[b]) return r[a] < r[b];
                     return a < b;
                 });
        }

        vector<char> rootNeighbor(N + 1, 0);
        int minRoot = adj[1][0];
        for (int a : adj[1]) {
            rootNeighbor[a] = 1;
            if (r[a] < r[minRoot] || (r[a] == r[minRoot] && a < minRoot))
                minRoot = a;
        }

        for (int i = 0; i < M; ++i) {
            int u = eu[i], v = ev[i];
            if (u != 1 && v != 1 && rootNeighbor[u] && rootNeighbor[v]) {
                triangles.push_back({u, v});
                triangles.push_back({v, u});
            }
        }

        vector<int> fallback = fallbackDFS();
        considerWithShortcuts(fallback);

        for (int type = 0; type < 2; ++type) {
            for (int mode = 0; mode < 3; ++mode) {
                Tree tree = buildTree(type == 1, mode);

                for (int kind = 0; kind < 2; ++kind) {
                    vector<ll> w = r;
                    w[1] = 0;
                    ll lambda = 0;

                    if (kind == 0) {
                        w[minRoot] = 0;
                        lambda = totalR - r[1] - r[minRoot];
                    }

                    DPResult dp = runDP(tree, w, lambda);
                    for (int par = 0; par < 2; ++par) {
                        if (dp.val[1][par] == NEG) continue;
                        vector<int> base = buildRoute(tree, dp, par);
                        considerWithShortcuts(base);
                    }
                }
            }
        }

        if (best.value < 0) {
            best.base = fallback;
            best.prefixKind = 0;
            best.prefixLen = 0;
            best.x = best.y = -1;
        }

        vector<int> answer;
        answer.reserve(K);
        int cur = 1;
        auto push = [&](int v) {
            answer.push_back(v);
            cur = v;
        };

        if (best.prefixKind == 1) {
            int E = best.prefixLen - 3;
            for (int i = 0; i < E; ++i)
                push((i & 1) ? 1 : best.x);
            push(best.x);
            push(best.y);
            push(1);
        } else {
            for (int i = 0; i < best.prefixLen; ++i)
                push((i & 1) ? 1 : best.x);
        }

        for (int v : best.base) push(v);

        while ((int)answer.size() < K) {
            int nxt = adj[cur][0];
            push(nxt);
        }

        for (int i = 0; i < K; ++i) {
            if (i) cout << ' ';
            cout << answer[i];
        }
        cout << '\n';
    }
};

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

    Solver solver;
    solver.run();
    return 0;
}