题目 › fcs-174

FrontierCS #174

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

查看原题跳到结果

题面

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

# Graph 3-Coloring ## Problem You are given an undirected graph with n vertices and m edges. Your task is to assign each vertex one of three colors. The objective is to minimize the number of conflicting edges. An edge is conflicting if its two endpoints have the same color. ## Input Format - Line 1: two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ n(n−1)/2) - Next m lines: two integers u v (1 ≤ u, v ≤ n, u ≠ v) The graph may be disconnected. There are no multiple edges or self-loops. ## Output Format - Output exactly one line: - n integers c₁ c₂ … cₙ - each cᵢ ∈ {1, 2, 3} ## Scoring All feasibility and interaction limits apply before scoring; invalid output receives zero. Points are averaged over test files. For the logarithmic branch, write x for the original minimizing quantity and a for its physical 60-point reference. The improvement factor is r=a/x, and the upper-band award is P=min(100,60+ln(r)). Here ln is natural: r=1 corresponds to 60 points, and on the uncapped logarithmic upper band, r=exp(P-60). Each additional point multiplies the physical improvement factor by e (divides a minimizing objective by e or multiplies a maximizing objective by e, as applicable). The lower-band, zero-objective, and completion rules below still apply. E is the number of edges whose endpoints have the same color, and m is the number of graph edges. Define the objective x=E, its physical reference a=A=0.4*m, and the lower-band calculation L=100*(1-E/m) when m>0. If m=0, every valid coloring receives 100; the following rule otherwise applies. ```text P = 100, if x = 0 P = L, if L <= 60 and x > 0 P = min(100, 60 + ln(r)), otherwise ``` Zero is tested on the valid original count, cost, or error, before the lower-band test. A zero reference anchor has no positive improvement branch; positive objectives keep their lower-band score. The checker reports P/100.

结果

Kimi K3.1 ProKimi

max

3.3

平均得分 ↑

  • 3.3采样 1 · 部分分
  • 10.9k平均输出 token(含思考) ↓
  • 31.9输出速度 tokens/s ↑
  • 345 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

3.3

平均得分 ↑

  • 3.3采样 1 · 部分分
  • 5.6k平均输出 token(含思考) ↓
  • 26.3输出速度 tokens/s ↑
  • 213 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3.3

平均得分 ↑

  • 3.3采样 1 · 部分分
  • 22.1k平均输出 token(含思考) ↓
  • 200.8输出速度 tokens/s ↑
  • 111 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

29.8

平均得分 ↑

  • 29.8采样 1 · 部分分
  • 57.9k平均输出 token(含思考) ↓
  • 85.0输出速度 tokens/s ↑
  • 681 秒延迟 ↓

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

代码

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

struct FastRNG {
    uint64_t state;
    explicit FastRNG(uint64_t seed) : state(seed) {}
    uint64_t next() {
        uint64_t x = state;
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        state = x;
        return x * 2685821657736338717ULL;
    }
    int operator()(int bound) {
        return int(next() % uint64_t(bound));
    }
};

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

    auto startTime = chrono::steady_clock::now();
    auto deadline = startTime + chrono::milliseconds(1800);

    int n, m;
    if (!(cin >> n >> m)) return 0;

    vector<vector<int>> adj(n);
    vector<pair<int, int>> edges;
    edges.reserve(m);

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

    if (m == 0) {
        for (int i = 0; i < n; ++i) {
            if (i) cout << ' ';
            cout << 1;
        }
        cout << '\n';
        return 0;
    }

    FastRNG rng(0x9E3779B97F4A7C15ULL ^ uint64_t(n) * 1000003ULL ^ uint64_t(m));

    // Bipartite graphs already have an optimal two-coloring.
    vector<int> bipartiteColor(n, -1);
    bool bipartite = true;
    for (int s = 0; s < n && bipartite; ++s) {
        if (bipartiteColor[s] != -1) continue;
        queue<int> q;
        bipartiteColor[s] = 0;
        q.push(s);
        while (!q.empty() && bipartite) {
            int v = q.front();
            q.pop();
            for (int u : adj[v]) {
                if (bipartiteColor[u] == -1) {
                    bipartiteColor[u] = bipartiteColor[v] ^ 1;
                    q.push(u);
                } else if (bipartiteColor[u] == bipartiteColor[v]) {
                    bipartite = false;
                    break;
                }
            }
        }
    }

    if (bipartite) {
        for (int i = 0; i < n; ++i) {
            if (i) cout << ' ';
            cout << bipartiteColor[i] + 1;
        }
        cout << '\n';
        return 0;
    }

    vector<int> degree(n);
    for (int v = 0; v < n; ++v) degree[v] = int(adj[v].size());

    auto greedyColoring = [&](bool randomized) {
        vector<int> color(n, -1);
        vector<array<int, 3>> neighborColors(n);
        for (auto &a : neighborColors) a = {0, 0, 0};
        vector<int> saturation(n, 0);
        vector<int> tieRank(n);
        iota(tieRank.begin(), tieRank.end(), 0);
        if (randomized) {
            for (int i = n - 1; i > 0; --i) {
                int j = rng(i + 1);
                swap(tieRank[i], tieRank[j]);
            }
        }

        for (int step = 0; step < n; ++step) {
            int v = -1;
            for (int i = 0; i < n; ++i) {
                if (color[i] != -1) continue;
                if (v == -1 ||
                    saturation[i] > saturation[v] ||
                    (saturation[i] == saturation[v] && degree[i] > degree[v]) ||
                    (saturation[i] == saturation[v] && degree[i] == degree[v] &&
                     (randomized ? tieRank[i] > tieRank[v] : i < v))) {
                    v = i;
                }
            }

            int minConflict = INT_MAX;
            for (int c = 0; c < 3; ++c) {
                minConflict = min(minConflict, neighborColors[v][c]);
            }

            int chosen = -1;
            int bestImpact = INT_MAX;
            int ties = 0;
            for (int c = 0; c < 3; ++c) {
                if (neighborColors[v][c] != minConflict) continue;

                int impact = 0;
                for (int u : adj[v]) {
                    if (color[u] == -1 && neighborColors[u][c] == 0) ++impact;
                }

                if (chosen == -1 || impact < bestImpact) {
                    chosen = c;
                    bestImpact = impact;
                    ties = 1;
                } else if (impact == bestImpact) {
                    ++ties;
                    if (!randomized || rng(ties) == 0) chosen = c;
                }
            }

            color[v] = chosen;
            for (int u : adj[v]) {
                if (color[u] != -1) continue;
                if (neighborColors[u][chosen] == 0) ++saturation[u];
                ++neighborColors[u][chosen];
            }
        }
        return color;
    };

    int bestE = INT_MAX;
    vector<int> bestColor;

    auto conflictsOf = [&](const vector<int> &color) {
        int e = 0;
        for (auto [u, v] : edges) e += (color[u] == color[v]);
        return e;
    };

    auto record = [&](const vector<int> &color, int e) {
        if (e < bestE) {
            bestE = e;
            bestColor = color;
        }
    };

    auto tabuSearch = [&](vector<int> color, int maxStagnation) {
        vector<array<int, 3>> cnt(n);
        for (auto &a : cnt) a = {0, 0, 0};
        for (int v = 0; v < n; ++v) {
            for (int u : adj[v]) ++cnt[v][color[u]];
        }

        vector<array<int, 3>> tabu(n);
        for (auto &a : tabu) a = {0, 0, 0};

        int currentE = conflictsOf(color);
        record(color, currentE);
        if (bestE == 0) return;

        int runBest = currentE;
        int lastImprovement = 0;
        int iteration = 0;

        while (true) {
            ++iteration;
            if ((iteration & 63) == 0 && chrono::steady_clock::now() >= deadline) break;
            if (iteration - lastImprovement > maxStagnation) break;

            int bestDelta = INT_MAX;
            int bestVertex = -1;
            int bestNewColor = -1;
            int conflictingVertices = 0;
            int ties = 0;

            for (int v = 0; v < n; ++v) {
                int oldColor = color[v];
                if (cnt[v][oldColor] == 0) continue;
                ++conflictingVertices;

                for (int c = 0; c < 3; ++c) {
                    if (c == oldColor) continue;
                    int delta = cnt[v][c] - cnt[v][oldColor];
                    bool isTabu = tabu[v][c] > iteration;
                    if (isTabu && currentE + delta >= bestE) continue;

                    if (delta < bestDelta) {
                        bestDelta = delta;
                        bestVertex = v;
                        bestNewColor = c;
                        ties = 1;
                    } else if (delta == bestDelta) {
                        ++ties;
                        if (rng(ties) == 0) {
                            bestVertex = v;
                            bestNewColor = c;
                        }
                    }
                }
            }

            if (bestVertex == -1) {
                for (auto &a : tabu) a = {0, 0, 0};
                continue;
            }

            int v = bestVertex;
            int oldColor = color[v];
            int newColor = bestNewColor;

            for (int u : adj[v]) {
                --cnt[u][oldColor];
                ++cnt[u][newColor];
            }
            color[v] = newColor;
            currentE += bestDelta;

            int tenure = 5 + rng(10) + int(0.6 * conflictingVertices);
            tabu[v][oldColor] = iteration + tenure;

            if (currentE < runBest) {
                runBest = currentE;
                lastImprovement = iteration;
            }
            if (currentE < bestE) {
                bestE = currentE;
                bestColor = color;
                if (bestE == 0) return;
            }
        }
    };

    vector<int> initial = greedyColoring(false);
    record(initial, conflictsOf(initial));

    int run = 0;
    while (bestE > 0 && chrono::steady_clock::now() < deadline) {
        vector<int> current;

        if (run == 0) {
            current = initial;
        } else if (run % 4 == 1) {
            current = bestColor;
            int changes = max(2, n / 25);
            for (int k = 0; k < changes; ++k) {
                int v = rng(n);
                int c = rng(2);
                if (c >= current[v]) ++c;
                current[v] = c;
            }
        } else if (run % 4 == 2) {
            current = greedyColoring(true);
        } else if (run % 4 == 3) {
            current = bestColor;
        } else {
            current.resize(n);
            for (int v = 0; v < n; ++v) current[v] = rng(3);
        }

        record(current, conflictsOf(current));
        if (bestE == 0) break;

        tabuSearch(current, 100000);
        ++run;
    }

    for (int i = 0; i < n; ++i) {
        if (i) cout << ' ';
        cout << bestColor[i] + 1;
    }
    cout << '\n';
    return 0;
}