题目 › fcs-6

FrontierCS #6

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

查看原题跳到结果

题面

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

worldmap Day 1 Tasks English (ISC) World Map Mr. Pacha, a Bolivian archeologist, discovered an ancient document near Tiwanaku that describes the world during the Tiwanaku Period (300-1000 CE). At that time, there were N countries, numbered from 1 to N. In the document, there is a list of M different pairs of adjacent countries: (A[0],B[0]),(A[1],B[1]),…,(A[M−1],B[M−1]). For each i (0≤i<M), the document states that country A[i] was adjacent to country B[i] and vice versa. Pairs of countries not listed were not adjacent. Mr. Pacha wants to create a map of the world such that all adjacencies between countries are exactly as they were during the Tiwanaku Period. For this purpose, he first chooses a positive integer K. Then, he draws the map as a grid of K×K square cells, with rows numbered from 0 to K−1 (top to bottom) and columns numbered from 0 to K−1 (left to right). He wants to color each cell of the map using one of N colors. The colors are numbered from 1 to N, and country j (1≤j≤N) is represented by color j. The coloring must satisfy all of the following conditions: For each j (1≤j≤N), there is at least one cell with color j. For each pair of adjacent countries (A[i],B[i]), there is at least one pair of adjacent cells such that one of them is colored A[i] and the other is colored B[i]. Two cells are adjacent if they share a side. For each pair of adjacent cells with different colors, the countries represented by these two colors were adjacent during the Tiwanaku Period. For example, if N=3, M=2 and the pairs of adjacent countries are (1,2) and (2,3), then the pair (1,3) was not adjacent, and the following map of dimension K=3 satisfies all the conditions. worldmap (1 of 5)In particular, a country does not need to form a connected region on the map. In the map above, country 3 forms a connected region, while countries 1 and 2 form disconnected regions. Your task is to help Mr. Pacha choose a value of K and create a map. The document guarantees that such a map exists. Since Mr. Pacha prefers smaller maps, in the last subtask your score depends on the value of K, and lower values of K may result in a better score. However, finding the minimum possible value of K is not required. Implementation Details You should implement the following procedure as well as a main function: std::vector<std::vector<int>> create_map(int N, int M, std::vector<int> A, std::vector<int> B) N: the number of countries. M: the number of pairs of adjacent countries. A and B: arrays of length M describing adjacent countries. The procedure should return an array C that represents the map. Let K be the length of C. Each element of C must be an array of length K, containing integers between 1 and N inclusive. C[i][j] is the color of the cell at row i and column j (for each i and j such that 0≤i,j<K). K must be less than or equal to 240. Constraints 1≤N≤40 0≤M≤ 1≤A[i]<B[i]≤N for each i such that 0≤i<M. 2 N⋅(N−1) worldmap (2 of 5)The pairs (A[0],B[0]),…,(A[M−1],B[M−1]) are distinct. There exists at least one map which satisfies all the conditions. Example In CMS, both of the following scenarios are included as part of a single test case. Example 1 Consider the following call: 2 N⋅(N−1) worldmap (3 of 5)create_map(3, 2, [1, 2], [2, 3]) This is the example from the task description, so the procedure can return the following map. [ [2, 3, 3], [2, 3, 2], [1, 2, 1] ] Example 2 Consider the following call: create_map(4, 4, [1, 1, 2, 3], [2, 3, 4, 4]) In this example, N=4, M=4 and the country pairs (1,2), (1,3), (2,4), and (3,4) are adjacent. Consequently, the pairs (1,4) and (2,3) are not adjacent. The procedure can return the following map of dimension K=7, which satisfies all the conditions. [ [2, 1, 3, 3, 4, 3, 4], [2, 1, 3, 3, 3, 3, 3], [2, 1, 1, 1, 3, 4, 4], [2, 2, 2, 1, 3, 4, 3], [1, 1, 1, 2, 4, 4, 4], [2, 2, 1, 2, 2, 4, 3], [2, 2, 1, 2, 2, 4, 4] ] The map could be smaller; for example, the procedure can return the following map of dimension K=2. [ [3, 1], [4, 2] ] Note that both maps satisfy K/N≤2. worldmap (4 of 5)Sample Grader The first line of the input should contain a single integer T, the number of scenarios. A description of T scenarios should follow, each in the format specified below. Input Format: N M A[0] B[0] : A[M-1] B[M-1] Output Format: P Q[0] Q[1] ... Q[P-1] C[0][0] ... C[0][Q[0]-1] : C[P-1][0] ... C[P-1][Q[P-1]-1] Here, P is the length of the array C returned by create_map , and Q[i] (0≤i<P) is the length of C[i]. Note that line 3 in the output format is intentionally left blank. worldmap (5 of 5) ## Scoring Let x be K, the number of rows (and columns) in the constructed map. N is the number of countries. The physical reference (the 60-point level) is a=1.5*N. Define the lower-band continuation: L(x) = 60*max(0,(6-x/N)/4.5). For positive x and positive physical reference a, define the improvement factor r=a/x. The physical reference has r=1 and earns 60 points. For a valid output on this branch, the points P are: P = 100, if x=0; P = L(x), if L(x)<=60; P = min(100,60+ln(r)), otherwise. Here ln is the natural logarithm. In the upper branch before the 100-point cap, r=exp(P-60): each additional point requires an e-fold improvement in the physical objective (division by e). All feasibility, correctness, and hard resource-limit checks remain required; invalid output receives 0. Across test files, use the arithmetic mean of the file scores.

结果

Kimi K3.1 ProKimi

max

59.5

平均得分 ↑

  • 59.5采样 1 · 部分分
  • 47.9k平均输出 token(含思考) ↓
  • 26.1输出速度 tokens/s ↑
  • 1837 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

48.6

平均得分 ↑

  • 48.6采样 1 · 部分分
  • 25.7k平均输出 token(含思考) ↓
  • 21.5输出速度 tokens/s ↑
  • 1198 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2.6

平均得分 ↑

  • 2.6采样 1 · 部分分
  • 18.4k平均输出 token(含思考) ↓
  • 155.2输出速度 tokens/s ↑
  • 119 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

0.0

平均得分 ↑

  • 0.0采样 1 · 答案错误
  • 80.7k平均输出 token(含思考) ↓
  • 71.4输出速度 tokens/s ↑
  • 1132 秒延迟 ↓

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

代码

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

namespace WM {

struct Item {
    int w, h;
    int child;  // -1 for a single-cell pocket
    int color;  // used only for pockets
};

struct Placement {
    int x, y, idx;
};

struct Layout {
    int w = INT_MAX, h = INT_MAX;
    vector<Placement> p;
};

struct Region {
    int w = 1, h = 1;
    vector<Item> items;
    vector<Placement> p;
};

static Layout packShelf(const vector<Item>& items, const vector<int>& order,
                        int limit) {
    Layout out;
    out.w = out.h = 0;
    out.p.clear();

    int x = 1, y = 1;
    int rowWidth = 0, rowHeight = 0;
    int maxWidth = 0, totalHeight = 0;

    for (int idx : order) {
        int pw = items[idx].w + 1;
        int ph = items[idx].h + 1;

        if (rowWidth > 0 && rowWidth + pw > limit) {
            maxWidth = max(maxWidth, rowWidth);
            totalHeight += rowHeight;
            y += rowHeight;
            x = 1;
            rowWidth = rowHeight = 0;
        }

        out.p.push_back({x, y, idx});
        x += pw;
        rowWidth += pw;
        rowHeight = max(rowHeight, ph);
    }

    maxWidth = max(maxWidth, rowWidth);
    totalHeight += rowHeight;
    out.w = maxWidth + 1;
    out.h = totalHeight + 1;
    return out;
}

static bool betterLayout(const Layout& a, const Layout& b) {
    auto keyA = tuple<int, int, long long>(
        max(a.w, a.h), abs(a.w - a.h), 1LL * a.w * a.h);
    auto keyB = tuple<int, int, long long>(
        max(b.w, b.h), abs(b.w - b.h), 1LL * b.w * b.h);
    return keyA < keyB;
}

static Region makeRegion(vector<Item> items) {
    Region r;
    r.items = move(items);

    if (r.items.empty()) {
        r.w = r.h = 1;
        return r;
    }

    if (r.items.size() == 1) {
        r.w = r.items[0].w + 2;
        r.h = r.items[0].h + 2;
        r.p.push_back({1, 1, 0});
        return r;
    }

    int n = (int)r.items.size();
    int maxPW = 0, sumPW = 0;
    long long paddedArea = 0;

    for (const auto& it : r.items) {
        int pw = it.w + 1;
        int ph = it.h + 1;
        maxPW = max(maxPW, pw);
        sumPW += pw;
        paddedArea += 1LL * pw * ph;
    }

    vector<int> limits;
    auto addLimit = [&](int x) {
        if (x >= maxPW && x <= sumPW) limits.push_back(x);
    };

    int range = sumPW - maxPW + 1;
    if (range <= 600) {
        for (int x = maxPW; x <= sumPW; ++x) addLimit(x);
    } else {
        int step = max(1, range / 300);
        for (int x = maxPW; x < sumPW; x += step) addLimit(x);
        addLimit(sumPW);
        int guess = max(1, (int)sqrt((long double)paddedArea));
        for (int d = -20; d <= 20; ++d) addLimit(guess + d);
    }

    sort(limits.begin(), limits.end());
    limits.erase(unique(limits.begin(), limits.end()), limits.end());

    vector<vector<int>> orders(4);
    for (int t = 0; t < 4; ++t) {
        orders[t].resize(n);
        iota(orders[t].begin(), orders[t].end(), 0);
    }

    sort(orders[1].begin(), orders[1].end(), [&](int a, int b) {
        const auto& A = r.items[a];
        const auto& B = r.items[b];
        int ma = max(A.w, A.h), mb = max(B.w, B.h);
        if (ma != mb) return ma > mb;
        return 1LL * A.w * A.h > 1LL * B.w * B.h;
    });

    sort(orders[2].begin(), orders[2].end(), [&](int a, int b) {
        const auto& A = r.items[a];
        const auto& B = r.items[b];
        if (A.h != B.h) return A.h > B.h;
        return A.w > B.w;
    });

    sort(orders[3].begin(), orders[3].end(), [&](int a, int b) {
        const auto& A = r.items[a];
        const auto& B = r.items[b];
        if (A.w != B.w) return A.w > B.w;
        return A.h > B.h;
    });

    Layout best;
    for (const auto& ord : orders) {
        for (int lim : limits) {
            Layout cur = packShelf(r.items, ord, lim);
            if (best.w == INT_MAX || betterLayout(cur, best)) {
                best = move(cur);
            }
        }
    }

    r.w = best.w;
    r.h = best.h;
    r.p = move(best.p);
    return r;
}

struct Builder {
    int n, root;
    const vector<vector<int>>& children;
    const vector<vector<int>>& pockets;
    const vector<int>& order;
    vector<Region> regions;

    Builder(int n_, int root_, const vector<vector<int>>& children_,
            const vector<vector<int>>& pockets_, const vector<int>& order_)
        : n(n_), root(root_), children(children_), pockets(pockets_),
          order(order_) {
        regions.resize(n);
    }

    void draw(int v, int r0, int c0, vector<vector<int>>& C) {
        const Region& r = regions[v];

        for (int i = 0; i < r.h; ++i) {
            for (int j = 0; j < r.w; ++j) {
                C[r0 + i][c0 + j] = v + 1;
            }
        }

        for (const auto& pl : r.p) {
            const Item& it = r.items[pl.idx];
            if (it.child >= 0) {
                draw(it.child, r0 + pl.y, c0 + pl.x, C);
            } else {
                C[r0 + pl.y][c0 + pl.x] = it.color + 1;
            }
        }
    }

    vector<vector<int>> run() {
        for (int oi = (int)order.size() - 1; oi >= 0; --oi) {
            int v = order[oi];
            vector<Item> items;

            for (int c : children[v]) {
                items.push_back({regions[c].w, regions[c].h, c, -1});
            }
            for (int c : pockets[v]) {
                items.push_back({1, 1, -1, c});
            }

            regions[v] = makeRegion(move(items));
        }

        int K = max(regions[root].w, regions[root].h);
        vector<vector<int>> C(K, vector<int>(K, root + 1));
        draw(root, 0, 0, C);
        return C;
    }
};

}  // namespace WM

vector<vector<int>> create_map(int N, int M, vector<int> A, vector<int> B) {
    if (N == 1) return {{1}};

    for (int& x : A) --x;
    for (int& x : B) --x;

    vector<vector<char>> adj(N, vector<char>(N, false));
    vector<vector<pair<int, int>>> g(N);

    for (int i = 0; i < M; ++i) {
        int u = A[i], v = B[i];
        adj[u][v] = adj[v][u] = true;
        g[u].push_back({v, i});
        g[v].push_back({u, i});
    }

    auto valid = [&](const vector<vector<int>>& C) {
        int K = (int)C.size();
        if (K <= 0 || K > 240) return false;

        for (const auto& row : C) {
            if ((int)row.size() != K) return false;
            for (int x : row) {
                if (x < 1 || x > N) return false;
            }
        }

        vector<char> present(N, false);
        vector<vector<char>> seen(N, vector<char>(N, false));

        for (int i = 0; i < K; ++i) {
            for (int j = 0; j < K; ++j) {
                present[C[i][j] - 1] = true;
            }
        }

        for (int i = 0; i < K; ++i) {
            for (int j = 0; j < K; ++j) {
                if (i + 1 < K) {
                    int u = C[i][j] - 1, v = C[i + 1][j] - 1;
                    if (u != v) {
                        if (!adj[u][v]) return false;
                        seen[u][v] = seen[v][u] = true;
                    }
                }
                if (j + 1 < K) {
                    int u = C[i][j] - 1, v = C[i][j + 1] - 1;
                    if (u != v) {
                        if (!adj[u][v]) return false;
                        seen[u][v] = seen[v][u] = true;
                    }
                }
            }
        }

        for (int v = 0; v < N; ++v) {
            if (!present[v]) return false;
        }
        for (int i = 0; i < M; ++i) {
            if (!seen[A[i]][B[i]]) return false;
        }
        return true;
    };

    vector<vector<int>> best;
    auto consider = [&](const vector<vector<int>>& C) {
        if ((best.empty() || C.size() < best.size()) && valid(C)) {
            best = C;
        }
    };

    int lowerBound = 1;
    while (lowerBound * lowerBound < N ||
           2 * lowerBound * (lowerBound - 1) < M) {
        ++lowerBound;
    }

    // Short walks covering all edges give diagonal-stripe maps.
    vector<vector<int>> dist(N, vector<int>(N, -1));
    vector<vector<int>> nxt(N, vector<int>(N, -1));

    for (int t = 0; t < N; ++t) {
        queue<int> q;
        q.push(t);
        dist[t][t] = 0;
        nxt[t][t] = t;

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (auto [v, id] : g[u]) {
                if (dist[v][t] == -1) {
                    dist[v][t] = dist[u][t] + 1;
                    nxt[v][t] = u;
                    q.push(v);
                }
            }
        }
    }

    vector<int> bestWalk;

    for (int start = 0; start < N; ++start) {
        vector<char> used(M, false);
        int covered = 0;
        int cur = start;
        vector<int> walk = {start};

        auto hasUnused = [&](int v) {
            for (auto [to, id] : g[v]) {
                if (!used[id]) return true;
            }
            return false;
        };

        while (covered < M) {
            int chosen = -1, to = -1;
            for (auto [v, id] : g[cur]) {
                if (!used[id]) {
                    chosen = id;
                    to = v;
                    break;
                }
            }

            if (chosen != -1) {
                used[chosen] = true;
                ++covered;
                cur = to;
                walk.push_back(cur);
                continue;
            }

            int target = -1;
            for (int v = 0; v < N; ++v) {
                if (!hasUnused(v)) continue;
                if (target == -1 ||
                    dist[cur][v] < dist[cur][target]) {
                    target = v;
                }
            }

            while (cur != target) {
                cur = nxt[cur][target];
                walk.push_back(cur);
            }
        }

        if (bestWalk.empty() || walk.size() < bestWalk.size()) {
            bestWalk = move(walk);
        }
    }

    if (!bestWalk.empty()) {
        int L = (int)bestWalk.size();
        int K = (L + 2) / 2;

        if (K <= 240 && (best.empty() || K < (int)best.size())) {
            vector<vector<int>> C(K, vector<int>(K));
            for (int i = 0; i < K; ++i) {
                for (int j = 0; j < K; ++j) {
                    C[i][j] = bestWalk[min(i + j, L - 1)] + 1;
                }
            }
            consider(C);
        }
    }

    if (!best.empty() && (int)best.size() == lowerBound) return best;

    // A particularly compact construction for complete graphs.
    if (M == N * (N - 1) / 2) {
        auto prime = [](int x) {
            if (x < 2) return false;
            for (int d = 2; d * d <= x; ++d) {
                if (x % d == 0) return false;
            }
            return true;
        };

        int p = N;
        while (!prime(p)) ++p;

        if (p <= 240) {
            vector<vector<int>> C(p, vector<int>(p));
            for (int i = 0; i < p; ++i) {
                for (int j = 0; j < p; ++j) {
                    int c = (i * j) % p;
                    if (c >= N) c = 0;
                    C[i][j] = c + 1;
                }
            }
            consider(C);
        }
    }

    if (!best.empty() && (int)best.size() == lowerBound) return best;

    auto nestedMap = [&](int root, int mode) {
        vector<int> parent(N, -1), depth(N, 0), order;
        vector<vector<int>> children(N);
        queue<int> q;

        parent[root] = root;
        q.push(root);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            order.push_back(u);

            for (auto [v, id] : g[u]) {
                if (parent[v] == -1) {
                    parent[v] = u;
                    depth[v] = depth[u] + 1;
                    children[u].push_back(v);
                    q.push(v);
                }
            }
        }

        vector<vector<int>> pockets(N);
        vector<int> load(N, 0);

        for (int i = 0; i < M; ++i) {
            int u = A[i], v = B[i];
            if (parent[u] == v || parent[v] == u) continue;

            int host, other;

            auto balancedChoice = [&]() {
                if (load[u] < load[v]) {
                    host = u;
                    other = v;
                } else if (load[v] < load[u]) {
                    host = v;
                    other = u;
                } else {
                    host = u;
                    other = v;
                }
            };

            if (mode == 0 && depth[u] != depth[v]) {
                if (depth[u] > depth[v]) {
                    host = u;
                    other = v;
                } else {
                    host = v;
                    other = u;
                }
            } else if (mode == 1 && depth[u] != depth[v]) {
                if (depth[u] < depth[v]) {
                    host = u;
                    other = v;
                } else {
                    host = v;
                    other = u;
                }
            } else {
                balancedChoice();
            }

            pockets[host].push_back(other);
            ++load[host];
        }

        WM::Builder builder(N, root, children, pockets, order);
        return builder.run();
    };

    int root = 0;
    for (int v = 1; v < N; ++v) {
        if (g[v].size() > g[root].size()) root = v;
    }

    vector<vector<int>> fallback;

    for (int mode = 0; mode < 3; ++mode) {
        auto C = nestedMap(root, mode);
        if (fallback.empty() || C.size() < fallback.size()) fallback = C;
        consider(C);

        if (!best.empty() && (int)best.size() == lowerBound) return best;
        if (M == N - 1) break;
    }

    // This is only needed for unusually unfavourable rectangle layouts.
    if (best.empty()) {
        for (int r = 0; r < N; ++r) {
            for (int mode = 0; mode < 3; ++mode) {
                auto C = nestedMap(r, mode);
                if (fallback.empty() || C.size() < fallback.size()) {
                    fallback = C;
                }
                consider(C);
                if (!best.empty()) break;
            }
            if (!best.empty()) break;
        }
    }

    if (!best.empty()) return best;
    return fallback;
}

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

    int T;
    if (!(cin >> T)) return 0;

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

        vector<int> A(M), B(M);
        for (int i = 0; i < M; ++i) {
            cin >> A[i] >> B[i];
        }

        auto C = create_map(N, M, A, B);
        int P = (int)C.size();

        cout << P << '\n';
        for (int i = 0; i < P; ++i) {
            if (i) cout << ' ';
            cout << C[i].size();
        }
        cout << "\n\n";

        for (int i = 0; i < P; ++i) {
            for (int j = 0; j < (int)C[i].size(); ++j) {
                if (j) cout << ' ';
                cout << C[i][j];
            }
            cout << '\n';
        }
    }

    return 0;
}