题目 › fcs-183

FrontierCS #183

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

查看原题跳到结果

题面

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

# Maximum Independent Set Challenge ## Context You are given an undirected graph G = (V, E) with |V| = N vertices and |E| = M edges. You must select a subset of vertices S ⊆ V to form an **Independent Set**. A subset S is a valid independent set if and only if: for every pair of distinct vertices u, v ∈ S, there is NO edge between them. (i.e., {u, v} ∉ E). This is a heuristic optimization problem. You are NOT required to find the theoretically optimal solution. Instead, you should try to **maximize** the size of the set S (denoted as |S|). Points depend on your set size relative to the stored reference; see Scoring. ## Input Format The first line contains two integers: N M where: 2 ≤ N ≤ 10,000 1 ≤ M ≤ 500,000 The next M lines each contain two integers: u v representing an undirected edge {u, v}, with 1 ≤ u, v ≤ N and u ≠ v. Multiple edges between the same pair of vertices may appear but imply the same constraint. ## Output Format Output exactly N lines. The i-th line must contain one integer x_i ∈ {0, 1}: - 1 indicates that vertex i is included in the independent set (i ∈ S). - 0 indicates that vertex i is excluded from the independent set (i ∉ S). Requirements: - For any pair of vertices u, v where x_u = 1 and x_v = 1, it must be true that {u, v} ∉ E. ## Scoring The physical objective is independent-set cardinality K; larger is better. The physical reference is `a = R`; matching a positive reference earns 60 points. R is the positive stored reference independent-set size. Define the historical low-score expression (in points): P_low = 60*K/R. If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award: r = K / 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. ## Example Input: 4 3 1 2 2 3 3 4 Output: 1 0 0 1 Explanation: The edges are {1,2}, {2,3}, {3,4}. The output selects vertices 1 and 4 (S={1, 4}). - Is {1,4} an edge? No. - Vertices 2 and 3 are not selected, so their connections don't violate independence. Total size K = 2. If the stored reference independent-set size is R=2, then r=2/2=1 and P=60 points. (Note: Selecting {1, 2} would be invalid because there is an edge between them.) ## Constraints - 2 ≤ N ≤ 10,000 - 1 ≤ M ≤ 500,000 - Time Limit: 2.0s - Memory Limit: 512MB

结果

Kimi K3.1 ProKimi

max

31.9

平均得分 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

31.6

平均得分 ↑

  • 31.6采样 1 · 部分分
  • 11.0k平均输出 token(含思考) ↓
  • 28.1输出速度 tokens/s ↑
  • 393 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

0.0

平均得分 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

0.0

平均得分 ↑

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

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

代码

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

struct FastInput {
    static const int SZ = 1 << 20;
    char buf[SZ];
    int len = 0, pos = 0;

    inline char getChar() {
        if (pos >= len) {
            len = (int)fread(buf, 1, SZ, stdin);
            pos = 0;
            if (len == 0) return 0;
        }
        return buf[pos++];
    }

    bool readInt(int &x) {
        char c;
        do {
            c = getChar();
            if (!c) return false;
        } while (c <= ' ');
        x = 0;
        while (c > ' ') {
            x = x * 10 + (c - '0');
            c = getChar();
        }
        return true;
    }
};

int N, W;
vector<vector<int>> G;
vector<uint64_t> adjBits;
mt19937 rng(0x9e3779b9U);
chrono::steady_clock::time_point startTime;
const double TIME_LIMIT = 1.86;

inline double elapsed() {
    return chrono::duration<double>(chrono::steady_clock::now() - startTime).count();
}

inline bool timeUp() {
    return elapsed() >= TIME_LIMIT;
}

inline bool hasEdge(int u, int v) {
    return (adjBits[(size_t)u * W + (v >> 6)] >> (v & 63)) & 1ULL;
}

void outputSet(const vector<char> &sol) {
    string out;
    out.reserve((size_t)N * 2);
    for (int i = 0; i < N; ++i) {
        out.push_back(sol[i] ? '1' : '0');
        out.push_back('\n');
    }
    fwrite(out.data(), 1, out.size(), stdout);
}

struct HopcroftKarp {
    const vector<int> &part;
    vector<int> matchL, matchR, dist, q;

    HopcroftKarp(const vector<int> &p)
        : part(p), matchL(N, -1), matchR(N, -1), dist(N, -1), q(N) {}

    bool bfs() {
        fill(dist.begin(), dist.end(), -1);
        int qh = 0, qt = 0;
        for (int u = 0; u < N; ++u) {
            if (part[u] == 0 && matchL[u] == -1) {
                dist[u] = 0;
                q[qt++] = u;
            }
        }

        bool found = false;
        while (qh < qt) {
            int u = q[qh++];
            for (int v : G[u]) {
                int nxt = matchR[v];
                if (nxt == -1) {
                    found = true;
                } else if (dist[nxt] == -1) {
                    dist[nxt] = dist[u] + 1;
                    q[qt++] = nxt;
                }
            }
        }
        return found;
    }

    bool dfs(int u) {
        for (int v : G[u]) {
            int nxt = matchR[v];
            if (nxt == -1 || (dist[nxt] == dist[u] + 1 && dfs(nxt))) {
                matchL[u] = v;
                matchR[v] = u;
                return true;
            }
        }
        dist[u] = -1;
        return false;
    }

    bool run() {
        while (true) {
            if (elapsed() > 1.68) return false;
            if (!bfs()) return true;
            for (int u = 0; u < N; ++u) {
                if (part[u] == 0 && matchL[u] == -1 && dist[u] == 0) {
                    dfs(u);
                }
            }
        }
    }
};

vector<char> staticGreedy() {
    vector<int> order(N);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int a, int b) {
        if (G[a].size() != G[b].size()) return G[a].size() < G[b].size();
        return a < b;
    });

    vector<char> blocked(N, 0), sol(N, 0);
    for (int v : order) {
        if (blocked[v]) continue;
        sol[v] = 1;
        blocked[v] = 1;
        for (int u : G[v]) blocked[u] = 1;
    }
    return sol;
}

vector<char> dynamicGreedy(int noise) {
    struct Node {
        uint64_t key;
        int d, v;
        bool operator<(const Node &other) const {
            return key > other.key;
        }
    };

    vector<int> deg(N);
    vector<char> active(N, 1);
    vector<uint32_t> randomValue(N);
    vector<char> sol(N, 0);
    priority_queue<Node> pq;

    for (int i = 0; i < N; ++i) {
        deg[i] = (int)G[i].size();
        randomValue[i] = rng();
    }

    auto makeKey = [&](int v, int d) -> uint64_t {
        uint64_t key = (uint64_t)d << 12;
        if (noise == 0) key += randomValue[v] & 4095U;
        else key += randomValue[v] % (noise * 4096U + 1U);
        return key;
    };

    for (int i = 0; i < N; ++i) {
        pq.push({makeKey(i, deg[i]), deg[i], i});
    }

    auto removeVertex = [&](int x) {
        active[x] = 0;
        for (int y : G[x]) {
            if (active[y]) {
                --deg[y];
                pq.push({makeKey(y, deg[y]), deg[y], y});
            }
        }
    };

    while (!pq.empty()) {
        Node node = pq.top();
        pq.pop();
        int v = node.v;
        if (!active[v] || node.d != deg[v]) continue;

        sol[v] = 1;
        removeVertex(v);
        for (int u : G[v]) {
            if (active[u]) removeVertex(u);
        }
    }
    return sol;
}

struct State {
    vector<char> inS;
    vector<int> tight;
    int sz = 0;

    vector<vector<int>> groups;
    vector<int> touched;
    vector<uint64_t> groupBits;

    State() : inS(N, 0), tight(N, 0), groups(N), groupBits(W) {}

    void build(const vector<char> &sol) {
        inS = sol;
        fill(tight.begin(), tight.end(), 0);
        sz = 0;
        for (int u = 0; u < N; ++u) {
            if (!inS[u]) continue;
            ++sz;
            for (int v : G[u]) ++tight[v];
        }
    }

    inline void add(int v) {
        inS[v] = 1;
        ++sz;
        for (int u : G[v]) ++tight[u];
    }

    inline void remove(int v) {
        inS[v] = 0;
        --sz;
        for (int u : G[v]) --tight[u];
    }

    void maximalize(bool randomize) {
        vector<int> candidates;
        for (int v = 0; v < N; ++v) {
            if (!inS[v] && tight[v] == 0) candidates.push_back(v);
        }
        if (candidates.empty()) return;

        if (randomize) {
            shuffle(candidates.begin(), candidates.end(), rng);
            stable_sort(candidates.begin(), candidates.end(), [&](int a, int b) {
                return G[a].size() < G[b].size();
            });
        } else {
            sort(candidates.begin(), candidates.end(), [&](int a, int b) {
                if (G[a].size() != G[b].size()) return G[a].size() < G[b].size();
                return a < b;
            });
        }

        for (int v : candidates) {
            if (!inS[v] && tight[v] == 0) add(v);
        }
    }

    bool findPair(const vector<int> &group, int &a, int &b) {
        fill(groupBits.begin(), groupBits.end(), 0ULL);
        for (int v : group) {
            groupBits[v >> 6] |= 1ULL << (v & 63);
        }

        for (int v : group) {
            uint64_t selfMask = 1ULL << (v & 63);
            groupBits[v >> 6] &= ~selfMask;
            const uint64_t *row = &adjBits[(size_t)v * W];

            bool found = false;
            for (int j = 0; j < W; ++j) {
                uint64_t available = groupBits[j] & ~row[j];
                if (available) {
                    a = v;
                    b = (j << 6) + __builtin_ctzll(available);
                    found = true;
                    break;
                }
            }
            groupBits[v >> 6] |= selfMask;
            if (found) return true;
        }
        return false;
    }

    void improve(bool randomMode) {
        vector<int> selected;
        selected.reserve(N);

        while (!timeUp()) {
            maximalize(randomMode);

            for (int u : touched) groups[u].clear();
            touched.clear();
            selected.clear();

            for (int u = 0; u < N; ++u) {
                if (inS[u]) selected.push_back(u);
            }
            if (randomMode) shuffle(selected.begin(), selected.end(), rng);

            for (size_t i = 0; i < selected.size(); ++i) {
                if ((i & 63) == 0 && timeUp()) return;
                int u = selected[i];
                for (int v : G[u]) {
                    if (!inS[v] && tight[v] == 1) groups[u].push_back(v);
                }
                if (!groups[u].empty()) touched.push_back(u);
            }

            bool swapped = false;
            for (int u : selected) {
                if (groups[u].size() < 2) continue;
                int a, b;
                if (findPair(groups[u], a, b)) {
                    remove(u);
                    add(a);
                    add(b);
                    swapped = true;
                    break;
                }
            }
            if (!swapped) break;
        }
    }

    void perturb(int strength) {
        vector<int> removed;
        for (int step = 0; step < strength; ++step) {
            int v = -1;

            if (rng() % 100 < 85) {
                int bestTight = INT_MAX;
                for (int t = 0; t < 24; ++t) {
                    int x = (int)(rng() % N);
                    if (inS[x]) continue;
                    if (tight[x] < bestTight || (tight[x] == bestTight && (rng() & 1))) {
                        bestTight = tight[x];
                        v = x;
                    }
                }
            }

            if (v == -1) {
                int start = (int)(rng() % N);
                for (int i = 0; i < N; ++i) {
                    int x = start + i;
                    if (x >= N) x -= N;
                    if (!inS[x]) {
                        v = x;
                        break;
                    }
                }
            }
            if (v == -1) return;

            removed.clear();
            for (int u : G[v]) {
                if (inS[u]) removed.push_back(u);
            }
            for (int u : removed) remove(u);
            if (!inS[v]) add(v);
        }
    }
};

int main() {
    startTime = chrono::steady_clock::now();
    static FastInput in;

    int M;
    if (!in.readInt(N)) return 0;
    in.readInt(M);

    W = (N + 63) >> 6;
    G.assign(N, {});
    adjBits.assign((size_t)N * W, 0ULL);

    for (int i = 0; i < M; ++i) {
        int u, v;
        in.readInt(u);
        in.readInt(v);
        --u;
        --v;

        size_t idx = (size_t)u * W + (v >> 6);
        uint64_t mask = 1ULL << (v & 63);
        if (adjBits[idx] & mask) continue;

        adjBits[idx] |= mask;
        adjBits[(size_t)v * W + (u >> 6)] |= 1ULL << (u & 63);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    vector<int> part(N, -1), q(N);
    bool bipartite = true;

    for (int s = 0; s < N && bipartite; ++s) {
        if (part[s] != -1) continue;
        part[s] = 0;
        int qh = 0, qt = 0;
        q[qt++] = s;

        while (qh < qt) {
            int u = q[qh++];
            for (int v : G[u]) {
                if (part[v] == -1) {
                    part[v] = part[u] ^ 1;
                    q[qt++] = v;
                } else if (part[v] == part[u]) {
                    bipartite = false;
                    break;
                }
            }
            if (!bipartite) break;
        }
    }

    if (bipartite) {
        HopcroftKarp hk(part);
        if (!hk.run()) {
            int leftCount = 0;
            for (int i = 0; i < N; ++i) leftCount += (part[i] == 0);
            vector<char> fallback(N, 0);
            int chosenPart = (leftCount * 2 >= N ? 0 : 1);
            for (int i = 0; i < N; ++i) fallback[i] = (part[i] == chosenPart);
            outputSet(fallback);
            return 0;
        }

        vector<char> reachable(N, 0);
        int qh = 0, qt = 0;
        for (int u = 0; u < N; ++u) {
            if (part[u] == 0 && hk.matchL[u] == -1) {
                reachable[u] = 1;
                q[qt++] = u;
            }
        }

        while (qh < qt) {
            int u = q[qh++];
            if (part[u] == 0) {
                for (int v : G[u]) {
                    if (hk.matchL[u] != v && !reachable[v]) {
                        reachable[v] = 1;
                        q[qt++] = v;
                    }
                }
            } else {
                int v = hk.matchR[u];
                if (v != -1 && !reachable[v]) {
                    reachable[v] = 1;
                    q[qt++] = v;
                }
            }
        }

        vector<char> exact(N, 0);
        for (int i = 0; i < N; ++i) {
            exact[i] = (part[i] == 0 ? reachable[i] : !reachable[i]);
        }
        outputSet(exact);
        return 0;
    }

    vector<char> best = staticGreedy();
    int bestSize = 0;
    for (char x : best) bestSize += x;

    auto runCandidate = [&](int noise) {
        vector<char> candidate = dynamicGreedy(noise);
        State state;
        state.build(candidate);
        state.improve(false);
        if (state.sz > bestSize) {
            bestSize = state.sz;
            best = state.inS;
        }
    };

    if (elapsed() < 1.45) runCandidate(0);

    int extraRuns = 0;
    while (extraRuns < 2 && elapsed() < 0.82) {
        runCandidate(1 + extraRuns * 2);
        ++extraRuns;
    }

    State current;
    current.build(best);

    int iteration = 0;
    int noImprovement = 0;

    while (!timeUp()) {
        int oldSize = current.sz;
        int r = (int)(rng() % 100);
        int strength;
        if (r < 70) strength = 1;
        else if (r < 93) strength = 2 + (int)(rng() % 2);
        else strength = 4 + (int)(rng() % 5);

        if (noImprovement > 60 && rng() % 12 == 0) {
            strength = 8 + (int)(rng() % 8);
        }

        current.perturb(strength);
        current.improve(true);

        if (current.sz > bestSize) {
            bestSize = current.sz;
            best = current.inS;
            noImprovement = 0;
        } else {
            ++noImprovement;
            if (current.sz == bestSize && rng() % 6 == 0) {
                best = current.inS;
            }
        }

        if (current.sz < bestSize - 3 ||
            current.sz + 3 < oldSize ||
            (iteration % 160 == 159 && current.sz < bestSize)) {
            current.build(best);
        }
        ++iteration;
    }

    outputSet(best);
    return 0;
}