题面
来源: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; }
Kimi K3.1 Pro(high) · 采样 1 · 207 行
#include <bits/stdc++.h> using namespace std; vector<vector<int>> create_map(int N, int M, vector<int> A, vector<int> B) { if (N == 1) return {{1}}; vector<vector<int>> g(N + 1); for (int i = 0; i < M; ++i) { g[A[i]].push_back(B[i]); g[B[i]].push_back(A[i]); } auto normalize = [](int a, int b) { if (a > b) swap(a, b); return make_pair(a, b); }; auto make_walk = [&](int root) { vector<int> walk; vector<bool> vis(N + 1, false); int seen = 0; function<void(int)> dfs = [&](int v) { vis[v] = true; ++seen; walk.push_back(v); if (seen == N) return; for (int to : g[v]) { if (!vis[to]) { dfs(to); if (seen == N) return; walk.push_back(v); } } }; dfs(root); return walk; }; auto build_from_walk = [&](const vector<int>& walk) { int L = (int)walk.size(); vector<int> first(N + 1, -1); for (int i = 0; i < L; ++i) { if (first[walk[i]] == -1) first[walk[i]] = i; } set<pair<int,int>> baseEdges; for (int i = 1; i < L; ++i) { baseEdges.insert(normalize(walk[i - 1], walk[i])); } vector<vector<int>> targets(N + 1); for (int i = 0; i < M; ++i) { int u = A[i], v = B[i]; if (baseEdges.count(normalize(u, v))) continue; if (first[u] < first[v]) targets[u].push_back(v); else targets[v].push_back(u); } auto generate = [&](vector<int> h, vector<int> w) { int sh = accumulate(h.begin(), h.end(), 0); int sw = accumulate(w.begin(), w.end(), 0); if (sh != sw) return vector<vector<int>>{}; vector<int> ro(L + 1, 0), co(L + 1, 0); for (int i = 0; i < L; ++i) { ro[i + 1] = ro[i] + h[i]; co[i + 1] = co[i] + w[i]; } int K = sh; vector<vector<int>> C(K, vector<int>(K)); for (int i = 0; i < L; ++i) { for (int j = 0; j < L; ++j) { int color = walk[min(i, j)]; for (int di = 0; di < h[i]; ++di) { for (int dj = 0; dj < w[j]; ++dj) { C[ro[i] + di][co[j] + dj] = color; } } } } vector<vector<pair<int,int>>> slots(N + 1); for (int v = 1; v <= N; ++v) { int p = first[v]; if (h[p] == 3) { for (int j = p + 1; j < L; j += 2) { slots[v].push_back({ro[p] + 1, co[j]}); } } if (w[p] == 3) { for (int i = p + 1; i < L; i += 2) { slots[v].push_back({ro[i], co[p] + 1}); } } } for (int v = 1; v <= N; ++v) { if (slots[v].size() < targets[v].size()) { return vector<vector<int>>{}; } for (int i = 0; i < (int)targets[v].size(); ++i) { auto [r, c] = slots[v][i]; C[r][c] = targets[v][i]; } } return C; }; vector<int> fullH(L, 3), fullW(L, 3); auto fullMap = generate(fullH, fullW); vector<int> h(L, 1), w(L, 1); for (int v = 1; v <= N; ++v) { int need = (int)targets[v].size(); if (need == 0) continue; int p = first[v]; int q = L - p - 1; if (need <= (q + 1) / 2) { h[p] = 3; } else { h[p] = w[p] = 3; } } int sh = accumulate(h.begin(), h.end(), 0); int sw = accumulate(w.begin(), w.end(), 0); while (sh < sw) { int p = 0; while (p < L && h[p] == 3) ++p; if (p == L) break; h[p] = 3; sh += 2; } while (sw < sh) { int p = 0; while (p < L && w[p] == 3) ++p; if (p == L) break; w[p] = 3; sw += 2; } auto selectiveMap = generate(h, w); if (!selectiveMap.empty() && (fullMap.empty() || selectiveMap.size() < fullMap.size())) { return selectiveMap; } return fullMap; }; vector<vector<int>> answer; for (int root = 1; root <= N; ++root) { vector<int> walk = make_walk(root); if ((int)walk.size() < N) continue; auto candidate = build_from_walk(walk); if (!candidate.empty() && (answer.empty() || candidate.size() < answer.size())) { answer = move(candidate); } } return answer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; 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 K = (int)C.size(); cout << K << '\n'; for (int i = 0; i < K; ++i) { if (i) cout << ' '; cout << C[i].size(); } cout << '\n'; for (int i = 0; i < K; ++i) { for (int j = 0; j < K; ++j) { if (j) cout << ' '; cout << C[i][j]; } cout << '\n'; } } return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 134 行
#include <bits/stdc++.h> using namespace std; vector<vector<int>> create_map(int N, int M, vector<int> A, vector<int> B) { vector<pair<int, int>> edges; vector<vector<int>> adj(N); for (int i = 0; i < M; i++) { int u = A[i] - 1; int v = B[i] - 1; edges.push_back({u, v}); adj[u].push_back(i); adj[v].push_back(i); } vector<char> isTree(M, false); vector<vector<int>> tree(N); vector<int> parent(N, -1); queue<int> q; parent[0] = 0; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int ei : adj[u]) { int v = (edges[ei].first == u ? edges[ei].second : edges[ei].first); if (parent[v] == -1) { parent[v] = u; isTree[ei] = true; tree[u].push_back(v); tree[v].push_back(u); q.push(v); } } } vector<int> seq; function<void(int, int)> dfs = [&](int u, int p) { seq.push_back(u); for (int v : tree[u]) { if (v == p) continue; dfs(v, u); seq.push_back(u); } }; dfs(0, -1); int L = (int)seq.size(); int K = 3 * L; vector<vector<int>> C(K, vector<int>(K)); for (int r = 0; r < K; r++) { for (int p = 0; p < L; p++) { int color = seq[p] + 1; for (int d = 0; d < 3; d++) { C[r][3 * p + d] = color; } } } vector<int> firstCol(N, -1); for (int p = 0; p < L; p++) { int v = seq[p]; if (firstCol[v] == -1) { firstCol[v] = 3 * p + 1; } } vector<int> used(N, 0); for (int i = 0; i < M; i++) { if (!isTree[i]) { int u = edges[i].first; int v = edges[i].second; int host = v; int island = u; int r = 2 * used[host] + 1; int c = firstCol[host]; C[r][c] = island + 1; used[host]++; } } return C; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; 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]; } vector<vector<int>> 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 << (int)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; }
MiniMax M3.1 Flash · 采样 1 · 846 行
#include <bits/stdc++.h> using namespace std; using Mask = uint64_t; class MaxCliqueSolver { int n; vector<Mask> adj; int bestSize = 0; Mask bestSet = 0; void colorSort(Mask candidates, vector<int>& order, vector<int>& colors) const { order.clear(); colors.clear(); Mask uncolored = candidates; int color = 0; while (uncolored) { ++color; Mask available = uncolored; while (available) { int v = __builtin_ctzll(available); Mask bit = Mask(1) << v; order.push_back(v); colors.push_back(color); uncolored &= ~bit; available &= ~bit; available &= ~adj[v]; } } } void expand(Mask candidates, int size, Mask chosen) { if (size > bestSize) { bestSize = size; bestSet = chosen; } if (!candidates) return; vector<int> order, colors; colorSort(candidates, order, colors); for (int i = static_cast<int>(order.size()) - 1; i >= 0; --i) { if (size + colors[i] <= bestSize) return; int v = order[i]; Mask bit = Mask(1) << v; Mask next = candidates & adj[v]; expand(next, size + 1, chosen | bit); candidates &= ~bit; } } public: explicit MaxCliqueSolver(vector<Mask> graph) : n(static_cast<int>(graph.size())), adj(std::move(graph)) {} Mask solve() { bestSize = 0; bestSet = 0; Mask all = (Mask(1) << n) - 1; expand(all, 0, 0); return bestSet; } }; static vector<int> minimumVertexCover(const vector<Mask>& graph) { int n = static_cast<int>(graph.size()); if (n == 0) return {}; Mask all = (Mask(1) << n) - 1; Mask edgeCount = 0; for (Mask neighbors : graph) { edgeCount += __builtin_popcountll(neighbors); } if ((edgeCount & 1ULL) == 0 && edgeCount == 0) return {}; vector<Mask> complement(n); for (int v = 0; v < n; ++v) { Mask bit = Mask(1) << v; complement[v] = all & ~(graph[v] | bit); } MaxCliqueSolver clique(std::move(complement)); Mask independentSet = clique.solve(); Mask cover = all & ~independentSet; vector<int> result; for (int v = 0; v < n; ++v) { if (cover & (Mask(1) << v)) result.push_back(v); } return result; } class GeneralMapSolver { using Tree = vector<vector<int>>; int n; vector<Mask> graph; vector<vector<int>> adjacency; struct Orientation { vector<vector<int>> targets; vector<int> load; }; static int treeDiameter(const Tree& tree, int start) { int n = static_cast<int>(tree.size()); vector<int> distance(n, -1), parent(n, -1); queue<int> q; distance[start] = 0; q.push(start); int farthest = start; while (!q.empty()) { int v = q.front(); q.pop(); if (distance[v] > distance[farthest]) farthest = v; for (int u : tree[v]) { if (distance[u] == -1) { distance[u] = distance[v] + 1; parent[u] = v; q.push(u); } } } int other = farthest; while (parent[other] != -1) other = parent[other]; return distance[farthest]; } static vector<int> diameterPath(const Tree& tree) { int n = static_cast<int>(tree.size()); auto bfs = [&](int source) { vector<int> distance(n, -1), parent(n, -1); queue<int> q; distance[source] = 0; q.push(source); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : tree[v]) { if (distance[u] == -1) { distance[u] = distance[v] + 1; parent[u] = v; q.push(u); } } } int farthest = source; for (int v = 0; v < n; ++v) { if (distance[v] > distance[farthest]) farthest = v; } return make_pair(farthest, parent); }; int endpoint = bfs(0).first; auto result = bfs(endpoint); int other = result.first; const auto& parent = result.second; vector<int> path; while (other != -1) { path.push_back(other); other = parent[other]; } reverse(path.begin(), path.end()); return path; } static vector<int> buildTreeWalk(const Tree& tree) { vector<int> path = diameterPath(tree); vector<char> onPath(tree.size(), false); for (int v : path) onPath[v] = true; vector<int> walk; walk.push_back(path[0]); function<void(int, int)> tour = [&](int v, int parent) { walk.push_back(v); for (int u : tree[v]) { if (u == parent || onPath[u]) continue; tour(u, v); walk.push_back(v); } }; for (int i = 0; i < static_cast<int>(path.size()); ++i) { int v = path[i]; for (int u : tree[v]) { if (onPath[u]) continue; tour(u, v); walk.push_back(v); } if (i + 1 < static_cast<int>(path.size())) { walk.push_back(path[i + 1]); } } return walk; } vector<Mask> nonTreeEdges(const Tree& tree) const { vector<Mask> result(n, 0); for (int v = 0; v < n; ++v) { for (int u : tree[v]) { result[v] |= Mask(1) << u; } } for (int v = 0; v < n; ++v) { result[v] = graph[v] & ~result[v]; } return result; } Orientation orientEdges(const vector<Mask>& edges, const vector<int>& cover) const { Mask coverMask = 0; for (int v : cover) coverMask |= Mask(1) << v; struct Edge { int u, v; int options; }; vector<Edge> edgeList; for (int u = 0; u < n; ++u) { for (int v = u + 1; v < n; ++v) { if (!(edges[u] & (Mask(1) << v))) continue; int options = 0; if (coverMask & (Mask(1) << u)) ++options; if (coverMask & (Mask(1) << v)) ++options; if (options > 0) edgeList.push_back({u, v, options}); } } sort(edgeList.begin(), edgeList.end(), [](const Edge& a, const Edge& b) { return a.options < b.options; }); vector<int> load(n, 0), assigned(edgeList.size(), -1); for (int i = 0; i < static_cast<int>(edgeList.size()); ++i) { int u = edgeList[i].u; int v = edgeList[i].v; bool canU = coverMask & (Mask(1) << u); bool canV = coverMask & (Mask(1) << v); if (!canU && !canV) return {}; int chosen; if (!canU) { chosen = v; } else if (!canV) { chosen = u; } else if (load[u] != load[v]) { chosen = load[u] < load[v] ? u : v; } else { int du = __builtin_popcountll(edges[u]); int dv = __builtin_popcountll(edges[v]); chosen = du >= dv ? u : v; } assigned[i] = chosen; ++load[chosen]; } bool changed = true; while (changed) { changed = false; for (int i = 0; i < static_cast<int>(edgeList.size()); ++i) { int a = assigned[i]; int b = edgeList[i].u ^ edgeList[i].v ^ a; if ((coverMask & (Mask(1) << b)) && load[a] > load[b] + 1) { --load[a]; ++load[b]; assigned[i] = b; changed = true; } } } Orientation result; result.load.assign(n, 0); result.targets.resize(n); for (int i = 0; i < static_cast<int>(edgeList.size()); ++i) { int a = assigned[i]; int b = edgeList[i].u ^ edgeList[i].v ^ a; result.targets[a].push_back(b); ++result.load[a]; } return result; } int estimateSize(const Tree& tree, const vector<int>& cover) const { vector<int> walk = buildTreeWalk(tree); vector<Mask> edges = nonTreeEdges(tree); Orientation orientation = orientEdges(edges, cover); int length = static_cast<int>(walk.size()); int capacity = max(1, length / 2); int result = length; for (int v = 0; v < n; ++v) { int count = orientation.load[v]; if (count == 0) continue; int interiorColumns = (count + capacity - 1) / capacity; result += interiorColumns + 1; } return result; } vector<vector<int>> construct(const Tree& tree, const vector<int>& cover) const { vector<int> walk = buildTreeWalk(tree); vector<Mask> edges = nonTreeEdges(tree); Orientation orientation = orientEdges(edges, cover); int length = static_cast<int>(walk.size()); int capacity = max(1, length / 2); vector<int> firstPosition(n, -1); for (int i = 0; i < length; ++i) { if (firstPosition[walk[i]] == -1) firstPosition[walk[i]] = i; } vector<int> width(length, 1); for (int v = 0; v < n; ++v) { int count = orientation.load[v]; if (count == 0) continue; int position = firstPosition[v]; if (position == -1) return {}; int interiorColumns = (count + capacity - 1) / capacity; width[position] = interiorColumns + 2; } int side = accumulate(width.begin(), width.end(), 0); if (side > 240) return {}; vector<int> blockStart(length, 0); int currentStart = 0; for (int i = 0; i < length; ++i) { blockStart[i] = currentStart; currentStart += width[i]; } vector<int> base; base.reserve(side); for (int i = 0; i < length; ++i) { for (int j = 0; j < width[i]; ++j) { base.push_back(walk[i]); } } vector<vector<int>> result(side, base); for (int v = 0; v < n; ++v) { if (orientation.targets[v].empty()) continue; int position = firstPosition[v]; int count = orientation.load[v]; int interiorColumns = (count + capacity - 1) / capacity; int start = blockStart[position]; int targetIndex = 0; for (int column = start + 1; column <= start + interiorColumns && targetIndex < static_cast<int>(orientation.targets[v].size()); ++column) { for (int row = 0; row < side && targetIndex < static_cast<int>(orientation.targets[v].size()); ++row) { if (((row + column) & 1) == 0) { result[row][column] = orientation.targets[v][targetIndex++]; } } } } return result; } bool validMap(const vector<vector<int>>& map) const { if (map.empty()) return false; int side = static_cast<int>(map.size()); for (const auto& row : map) { if (static_cast<int>(row.size()) != side) return false; } vector<char> present(n, false); vector<Mask> seen(n, 0); for (int r = 0; r < side; ++r) { for (int c = 0; c < side; ++c) { int a = map[r][c]; if (a < 0 || a >= n) return false; present[a] = true; if (r + 1 < side) { int b = map[r + 1][c]; if (a != b) { if (!(graph[a] & (Mask(1) << b))) return false; seen[a] |= Mask(1) << b; } } if (c + 1 < side) { int b = map[r][c + 1]; if (a != b) { if (!(graph[a] & (Mask(1) << b))) return false; seen[a] |= Mask(1) << b; } } } } for (int v = 0; v < n; ++v) { if (!present[v]) return false; } for (int u = 0; u < n; ++u) { for (int v = u + 1; v < n; ++v) { if ((graph[u] & (Mask(1) << v)) && !(seen[u] & (Mask(1) << v))) { return false; } } } return true; } Tree breadthFirstTree() const { Tree tree(n); vector<char> visited(n, false); queue<int> q; visited[0] = true; q.push(0); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : adjacency[v]) { if (!visited[u]) { visited[u] = true; tree[v].push_back(u); tree[u].push_back(v); q.push(u); } } } return tree; } Tree randomDepthFirstTree(mt19937& rng, int variant) const { Tree tree(n); vector<char> visited(n, false); function<void(int)> dfs = [&](int v) { visited[v] = true; vector<int> order = adjacency[v]; if (variant == 0) { sort(order.begin(), order.end(), [&](int a, int b) { return adjacency[a].size() < adjacency[b].size(); }); } else if (variant == 1) { sort(order.begin(), order.end(), [&](int a, int b) { return adjacency[a].size() > adjacency[b].size(); }); } else { shuffle(order.begin(), order.end(), rng); } for (int u : order) { if (!visited[u]) { tree[v].push_back(u); tree[u].push_back(v); dfs(u); } } }; dfs(0); return tree; } Tree randomPrimTree(mt19937& rng) const { vector<vector<int>> weight(n, vector<int>(n, -1)); for (int u = 0; u < n; ++u) { for (int v = u + 1; v < n; ++v) { if (graph[u] & (Mask(1) << v)) { weight[u][v] = weight[v][u] = static_cast<int>(rng()); } } } const int INF = numeric_limits<int>::max(); vector<int> key(n, INF); vector<char> used(n, false); Tree tree(n); int root = static_cast<int>(rng() % n); key[root] = 0; for (int iteration = 0; iteration < n; ++iteration) { int v = -1; for (int u = 0; u < n; ++u) { if (!used[u] && (v == -1 || key[u] < key[v])) v = u; } if (v == -1) break; used[v] = true; for (int u = 0; u < n; ++u) { if (!used[u] && weight[v][u] >= 0 && weight[v][u] < key[u]) { key[u] = weight[v][u]; tree[v].push_back(u); tree[u].push_back(v); } } } return tree; } bool findHamiltonianPath(mt19937& rng, vector<int>& path) const { int leaves = 0; for (int v = 0; v < n; ++v) { if (adjacency[v].size() == 1) ++leaves; } if (leaves > 2) return false; vector<int> starts(n); iota(starts.begin(), starts.end(), 0); shuffle(starts.begin(), starts.end(), rng); for (int start : starts) { vector<char> visited(n, false); path.clear(); path.push_back(start); visited[start] = true; int nodes = 0; const int LIMIT = 300000; function<bool(int)> dfs = [&](int v) { if (++nodes > LIMIT) return false; if (static_cast<int>(path.size()) == n) return true; vector<pair<int, int>> candidates; for (int u : adjacency[v]) { if (visited[u]) continue; int remaining = 0; for (int w : adjacency[u]) { if (!visited[w]) ++remaining; } candidates.push_back({remaining, u}); } sort(candidates.begin(), candidates.end()); for (auto [remaining, u] : candidates) { if (nodes > LIMIT) break; visited[u] = true; path.push_back(u); if (dfs(u)) return true; path.pop_back(); visited[u] = false; } return false; }; if (dfs(start)) return true; } return false; } public: GeneralMapSolver(int vertexCount, const vector<Mask>& adjacencyMask) : n(vertexCount), graph(adjacencyMask), adjacency(n) { for (int v = 0; v < n; ++v) { for (int u = 0; u < n; ++u) { if (graph[v] & (Mask(1) << u)) adjacency[v].push_back(u); } } } vector<vector<int>> solve() { mt19937 rng(712367821U + static_cast<unsigned>(n)); Tree bestTree; vector<int> bestCover; int bestSize = numeric_limits<int>::max(); auto consider = [&](const Tree& tree) { if (static_cast<int>(tree.size()) != n) return; vector<int> walk = buildTreeWalk(tree); if (walk.empty()) return; vector<Mask> edges = nonTreeEdges(tree); vector<int> cover = minimumVertexCover(edges); int size = estimateSize(tree, cover); if (size < bestSize) { bestSize = size; bestTree = tree; bestCover = std::move(cover); } }; consider(breadthFirstTree()); for (int variant = 0; variant < 14; ++variant) { consider(randomDepthFirstTree(rng, variant)); } for (int i = 0; i < 6; ++i) { consider(randomPrimTree(rng)); } vector<int> path; if (findHamiltonianPath(rng, path)) { Tree pathTree(n); for (int i = 0; i + 1 < static_cast<int>(path.size()); ++i) { int u = path[i], v = path[i + 1]; pathTree[u].push_back(v); pathTree[v].push_back(u); } consider(pathTree); } vector<vector<int>> answer = construct(bestTree, bestCover); if (!validMap(answer)) { vector<int> allVertices(n); iota(allVertices.begin(), allVertices.end(), 0); answer = construct(bestTree, allVertices); } return answer; } }; static vector<pair<int, int>> roundRobinMatching(int size, int round) { vector<int> arrangement(size - 1); iota(arrangement.begin(), arrangement.end(), 0); int fixed = size - 1; vector<pair<int, int>> matching; matching.push_back({fixed, arrangement[round]}); for (int i = 1; i <= (size - 2) / 2; ++i) { int a = arrangement[(round + i) % (size - 1)]; int b = arrangement[(round - i + size - 1) % (size - 1)]; matching.push_back({a, b}); } return matching; } static vector<vector<int>> completeGraphMap(int n) { vector<vector<int>> result(n, vector<int>(n)); if (n % 2 == 0) { for (int round = 0; round < n - 1; ++round) { auto matching = roundRobinMatching(n, round); int column = 0; for (auto [a, b] : matching) { result[round][column++] = a; result[round][column++] = b; } } for (int column = 0; column < n; ++column) { result[n - 1][column] = 0; } } else { for (int round = 0; round < n; ++round) { auto matching = roundRobinMatching(n + 1, round); int column = 0; int singleton = -1; for (auto [a, b] : matching) { if (a == n) { singleton = b; } else if (b == n) { singleton = a; } else { result[round][column++] = a; result[round][column++] = b; } } if (singleton != -1) result[round][column++] = singleton; } } return result; } static vector<vector<int>> starMap(int n) { int side = 2; while ((side * side) / 2 < n - 1) ++side; vector<vector<int>> result(side, vector<int>(side, 0)); int nextLeaf = 1; for (int r = 0; r < side; ++r) { for (int c = 0; c < side; ++c) { if ((r + c) & 1) { result[r][c] = nextLeaf; if (++nextLeaf == n) nextLeaf = 1; } } } return result; } std::vector<std::vector<int>> create_map(int N, int M, std::vector<int> A, std::vector<int> B) { vector<Mask> adjacency(N, 0); for (int i = 0; i < M; ++i) { int u = A[i] - 1; int v = B[i] - 1; adjacency[u] |= Mask(1) << v; adjacency[v] |= Mask(1) << u; } bool complete = (M == N * (N - 1) / 2); if (complete) { for (int u = 0; u < N && complete; ++u) { if (__builtin_popcountll(adjacency[u]) != N - 1) complete = false; } } if (complete) { vector<vector<int>> answer = completeGraphMap(N); for (auto& row : answer) { for (int& color : row) ++color; } return answer; } int maximumDegree = 0; for (int v = 0; v < N; ++v) { maximumDegree = max(maximumDegree, __builtin_popcountll(adjacency[v])); } if (N >= 3 && M == N - 1 && maximumDegree == N - 1) { vector<vector<int>> answer = starMap(N); for (auto& row : answer) { for (int& color : row) ++color; } return answer; } GeneralMapSolver solver(N, adjacency); vector<vector<int>> answer = solver.solve(); for (auto& row : answer) { for (int& color : row) ++color; } return answer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; 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]; } vector<vector<int>> C = create_map(N, M, A, B); int K = static_cast<int>(C.size()); cout << K << "\n\n"; for (int r = 0; r < K; ++r) { for (int c = 0; c < K; ++c) { if (c) cout << ' '; cout << C[r][c]; } cout << '\n'; } } return 0; }