题面
来源: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; }
Kimi K3.1 Pro(high) · 采样 1 · 237 行
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto startTime = chrono::steady_clock::now(); const double TIME_LIMIT = 1.80; int n, m; cin >> n >> m; vector<vector<int>> adj(n); 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); } if (m == 0) { for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << 1; } cout << '\n'; return 0; } mt19937 rng(712367821); vector<int> bestColor(n, 0); int bestE = INT_MAX; auto elapsed = [&]() { return chrono::duration<double>(chrono::steady_clock::now() - startTime).count(); }; auto dsatur = [&](bool randomized) { vector<int> col(n, -1), sat(n, 0), used(3, 0); vector<array<int, 3>> cnt(n); for (auto &a : cnt) a = {0, 0, 0}; for (int step = 0; step < n; ++step) { int v = -1; for (int i = 0; i < n; ++i) { if (col[i] != -1) continue; if (v == -1) { v = i; continue; } int si = __builtin_popcount((unsigned)sat[i]); int sv = __builtin_popcount((unsigned)sat[v]); if (si > sv || (si == sv && adj[i].size() > adj[v].size())) { v = i; } else if (randomized && si == sv && adj[i].size() == adj[v].size() && (rng() & 1)) { v = i; } } int c = 0, ties = 1; for (int k = 1; k < 3; ++k) { if (cnt[v][k] < cnt[v][c] || (cnt[v][k] == cnt[v][c] && used[k] < used[c])) { c = k; ties = 1; } else if (randomized && cnt[v][k] == cnt[v][c] && used[k] == used[c]) { ++ties; if (rng() % ties == 0) c = k; } } col[v] = c; ++used[c]; for (int u : adj[v]) { if (col[u] == -1) { ++cnt[u][c]; sat[u] |= 1 << c; } } } return col; }; vector<array<int, 3>> cnt(n); vector<array<int, 3>> tabu(n); int restart = 0; while (true) { vector<int> col; if (restart == 0) { col = dsatur(false); } else if (restart % 4 == 1) { col = dsatur(true); } else if (restart % 4 == 2) { col = bestColor; int changes = max(2, n / 12); for (int i = 0; i < changes; ++i) { int v = rng() % n; col[v] = rng() % 3; } } else if (restart % 4 == 3) { col.resize(n); for (int &c : col) c = rng() % 3; } else { vector<int> order(n); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), rng); col.assign(n, -1); for (int v : order) { int cc[3] = {0, 0, 0}; for (int u : adj[v]) if (col[u] != -1) ++cc[col[u]]; int c = 0, ties = 1; for (int k = 1; k < 3; ++k) { if (cc[k] < cc[c]) { c = k; ties = 1; } else if (cc[k] == cc[c]) { ++ties; if (rng() % ties == 0) c = k; } } col[v] = c; } } int currentE = 0; for (int v = 0; v < n; ++v) { cnt[v] = {0, 0, 0}; tabu[v] = {0, 0, 0}; } for (int v = 0; v < n; ++v) { for (int u : adj[v]) ++cnt[v][col[u]]; currentE += cnt[v][col[v]]; } currentE /= 2; if (currentE < bestE) { bestE = currentE; bestColor = col; if (bestE == 0) break; } int noImprove = 0; int iteration = 0; const int restartLimit = 12000 + 8 * n; while (noImprove < restartLimit) { if ((iteration & 127) == 0 && elapsed() > TIME_LIMIT) break; ++iteration; int moveV = -1, moveC = -1, moveDelta = INT_MAX; int ties = 0, conflicting = 0; for (int v = 0; v < n; ++v) { int old = col[v]; if (cnt[v][old] == 0) continue; ++conflicting; for (int c = 0; c < 3; ++c) { if (c == old) continue; int delta = cnt[v][c] - cnt[v][old]; if (tabu[v][c] > iteration && currentE + delta >= bestE) continue; if (delta < moveDelta) { moveDelta = delta; moveV = v; moveC = c; ties = 1; } else if (delta == moveDelta) { ++ties; if (rng() % ties == 0) { moveV = v; moveC = c; } } } } if (moveV == -1) { moveDelta = INT_MAX; ties = 0; for (int v = 0; v < n; ++v) { int old = col[v]; if (cnt[v][old] == 0) continue; for (int c = 0; c < 3; ++c) { if (c == old) continue; int delta = cnt[v][c] - cnt[v][old]; if (delta < moveDelta) { moveDelta = delta; moveV = v; moveC = c; ties = 1; } else if (delta == moveDelta) { ++ties; if (rng() % ties == 0) { moveV = v; moveC = c; } } } } } if (moveV == -1) break; int old = col[moveV]; col[moveV] = moveC; currentE += moveDelta; for (int u : adj[moveV]) { --cnt[u][old]; ++cnt[u][moveC]; } int tenure = 5 + (int)(rng() % 10) + (int)(0.6 * conflicting); tabu[moveV][old] = iteration + tenure; if (currentE < bestE) { bestE = currentE; bestColor = col; noImprove = 0; if (bestE == 0) break; } else { ++noImprove; } } if (bestE == 0 || elapsed() > TIME_LIMIT) break; ++restart; } for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << bestColor[i] + 1; } cout << '\n'; return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 303 行
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; int n, m; vector<int> adj[MAXN]; int colorArr[MAXN], bestColor[MAXN]; int cnt[MAXN][3]; int tabuArr[MAXN][3]; int E, bestE; int initCountsForColor() { memset(cnt, 0, sizeof(cnt)); for (int v = 0; v < n; ++v) { for (int u : adj[v]) { ++cnt[v][colorArr[u]]; } } int e = 0; for (int v = 0; v < n; ++v) { e += cnt[v][colorArr[v]]; } return e / 2; } vector<int> greedyColoring(bool randomTie) { vector<int> col(n, -1); vector<array<int, 3>> gcnt(n); for (int i = 0; i < n; ++i) gcnt[i] = {0, 0, 0}; vector<unsigned char> mask(n, 0); vector<int> deg(n); for (int v = 0; v < n; ++v) deg[v] = (int)adj[v].size(); for (int step = 0; step < n; ++step) { int best = -1; int bestSat = -1; int bestDeg = -1; for (int v = 0; v < n; ++v) { if (col[v] != -1) continue; int sat = __builtin_popcount((unsigned)mask[v]); if (sat > bestSat || (sat == bestSat && (deg[v] > bestDeg || (randomTie && deg[v] == bestDeg && (rand() & 1))))) { bestSat = sat; bestDeg = deg[v]; best = v; } } int minConf = 1000000000; int choices[3]; int num = 0; for (int c = 0; c < 3; ++c) { if (gcnt[best][c] < minConf) { minConf = gcnt[best][c]; num = 0; choices[num++] = c; } else if (gcnt[best][c] == minConf) { choices[num++] = c; } } int chosen; if (randomTie) chosen = choices[rand() % num]; else chosen = choices[0]; col[best] = chosen; for (int v : adj[best]) { if (col[v] == -1) { if (gcnt[v][chosen] == 0) { mask[v] |= (1 << chosen); } ++gcnt[v][chosen]; } } } return col; } int evaluate(const vector<int>& col) { int e = 0; for (int v = 0; v < n; ++v) { for (int u : adj[v]) { if (v < u && col[v] == col[u]) ++e; } } return e; } bool checkBipartite(vector<int>& res) { res.assign(n, -1); queue<int> q; for (int s = 0; s < n; ++s) { if (res[s] != -1) continue; res[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : adj[v]) { if (res[u] == -1) { res[u] = res[v] ^ 1; q.push(u); } else if (res[u] == res[v]) { return false; } } } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); srand((unsigned)chrono::steady_clock::now().time_since_epoch().count()); cin >> n >> 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); } if (m == 0) { for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << 1; } cout << '\n'; return 0; } vector<int> bip; if (checkBipartite(bip)) { for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << (bip[i] + 1); } cout << '\n'; return 0; } bestE = 1000000000; vector<int> bestVec; const int greedyRuns = 30; for (int r = 0; r < greedyRuns; ++r) { vector<int> col = greedyColoring(r > 0); int e = evaluate(col); if (e < bestE) { bestE = e; bestVec = col; } if (bestE == 0) break; } if (bestE == 0) { for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << bestVec[i] + 1; } cout << '\n'; return 0; } for (int v = 0; v < n; ++v) colorArr[v] = bestVec[v]; E = initCountsForColor(); bestE = E; for (int v = 0; v < n; ++v) bestColor[v] = colorArr[v]; memset(tabuArr, 0, sizeof(tabuArr)); int iter = 0; int noImprove = 0; const int maxNoImprove = 1000 + n; auto start = chrono::steady_clock::now(); const double timeLimit = 1.5; while (true) { if ((iter & 127) == 0) { auto now = chrono::steady_clock::now(); double elapsed = chrono::duration<double>(now - start).count(); if (elapsed > timeLimit) break; } int bestDelta = 1000000000; int bestV = -1; int bestC = -1; for (int v = 0; v < n; ++v) { int old = colorArr[v]; int base = cnt[v][old]; for (int c = 0; c < 3; ++c) { if (c == old) continue; int delta = cnt[v][c] - base; if (tabuArr[v][c] > iter && E + delta >= bestE) continue; if (delta < bestDelta || (delta == bestDelta && (rand() & 1))) { bestDelta = delta; bestV = v; bestC = c; } } } if (bestV == -1) { bestV = rand() % n; bestC = (colorArr[bestV] + 1 + rand() % 2) % 3; bestDelta = cnt[bestV][bestC] - cnt[bestV][colorArr[bestV]]; } int oldc = colorArr[bestV]; E += bestDelta; colorArr[bestV] = bestC; for (int u : adj[bestV]) { --cnt[u][oldc]; ++cnt[u][bestC]; } int tenure = 7 + rand() % 10; tenure += noImprove / 500; if (tenure > 50) tenure = 50; tabuArr[bestV][oldc] = iter + tenure; ++iter; if (E < bestE) { bestE = E; for (int v = 0; v < n; ++v) bestColor[v] = colorArr[v]; noImprove = 0; if (bestE == 0) break; } else { ++noImprove; } if (noImprove > maxNoImprove) { for (int v = 0; v < n; ++v) colorArr[v] = bestColor[v]; E = initCountsForColor(); memset(tabuArr, 0, sizeof(tabuArr)); int K = max(1, n / 10); for (int k = 0; k < K; ++k) { int v = rand() % n; int oldc2 = colorArr[v]; int newc = rand() % 3; if (newc == oldc2) newc = (newc + 1 + rand() % 2) % 3; int delta = cnt[v][newc] - cnt[v][oldc2]; E += delta; colorArr[v] = newc; for (int u : adj[v]) { --cnt[u][oldc2]; ++cnt[u][newc]; } } noImprove = 0; if (rand() % 5 == 0) { for (int v = 0; v < n; ++v) colorArr[v] = rand() % 3; E = initCountsForColor(); memset(tabuArr, 0, sizeof(tabuArr)); } } } for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << bestColor[i] + 1; } cout << '\n'; return 0; }
MiniMax M3.1 Flash · 采样 1 · 906 行
#include <bits/stdc++.h> using namespace std; struct FastScanner { static constexpr int SIZE = 1 << 16; char buffer[SIZE]; int pos = 0, len = 0; inline char getChar() { if (pos == len) { len = (int)fread(buffer, 1, SIZE, stdin); pos = 0; if (len == 0) return 0; } return buffer[pos++]; } bool readInt(int &x) { char c; do { c = getChar(); if (!c) return false; } while (c <= ' '); x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getChar(); } return true; } }; struct RNG { uint64_t state; explicit RNG(uint64_t seed) : state(seed) {} inline uint64_t next() { uint64_t z = (state += 0x9e3779b97f4a7c15ULL); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; return z ^ (z >> 31); } inline int integer(int n) { return n <= 1 ? 0 : (int)(next() % (uint64_t)n); } }; class CoreSolver { private: int n; vector<vector<int>> graph; vector<unsigned char> adjacent; int edgeCount = 0; vector<int> color, bestColor; vector<array<int, 3>> neighborCount; vector<unsigned char> badFlag; vector<int> badVertices; vector<int> badPosition; vector<array<int, 3>> tabuUntil; int conflicts = 0; int bestConflicts = INT_MAX; vector<int> exactColor; vector<array<int, 3>> forbidden; long long exactLimit = 0; long long exactUsed = 0; bool exactAborted = false; RNG rng; int computeConflicts(const vector<int> &c) const { int result = 0; for (int u = 0; u < n; ++u) { for (int v : graph[u]) { if (v > u && c[u] == c[v]) ++result; } } return result; } bool findBipartiteColoring() { color.assign(n, -1); vector<int> queue; queue.reserve(n); for (int root = 0; root < n; ++root) { if (color[root] != -1) continue; color[root] = 0; queue.clear(); queue.push_back(root); for (size_t head = 0; head < queue.size(); ++head) { int u = queue[head]; for (int v : graph[u]) { if (color[v] == -1) { color[v] = color[u] ^ 1; queue.push_back(v); } else if (color[v] == color[u]) { return false; } } } } return true; } void greedyColoring() { bestConflicts = INT_MAX; for (int variant = 0; variant < 6; ++variant) { color.assign(n, -1); vector<array<int, 3>> same(n); for (auto &a : same) a.fill(0); vector<int> mask(n, 0); array<int, 3> classSize{0, 0, 0}; vector<int> candidates; candidates.reserve(n); for (int step = 0; step < n; ++step) { int bestSaturation = -1; int bestDegree = -1; candidates.clear(); for (int v = 0; v < n; ++v) { if (color[v] != -1) continue; int saturation = __builtin_popcount((unsigned)mask[v]); int degree = (int)graph[v].size(); if (saturation > bestSaturation || (saturation == bestSaturation && degree > bestDegree)) { bestSaturation = saturation; bestDegree = degree; candidates.clear(); } if (saturation == bestSaturation && degree == bestDegree) { candidates.push_back(v); } } int v; if (variant % 3 == 0) { v = candidates.front(); } else { v = candidates[rng.integer((int)candidates.size())]; } int minimumSame = INT_MAX; for (int c = 0; c < 3; ++c) { minimumSame = min(minimumSame, same[v][c]); } array<int, 3> choices{0, 0, 0}; int numberOfChoices = 0; if (variant & 1) { for (int c = 0; c < 3; ++c) { if (same[v][c] == minimumSame) { choices[numberOfChoices++] = c; } } } else { int minimumClassSize = INT_MAX; for (int c = 0; c < 3; ++c) { if (same[v][c] == minimumSame) { minimumClassSize = min(minimumClassSize, classSize[c]); } } for (int c = 0; c < 3; ++c) { if (same[v][c] == minimumSame && classSize[c] == minimumClassSize) { choices[numberOfChoices++] = c; } } } int chosenColor = choices[rng.integer(numberOfChoices)]; color[v] = chosenColor; ++classSize[chosenColor]; for (int u : graph[v]) { if (color[u] == -1) { mask[u] |= 1 << chosenColor; ++same[u][chosenColor]; } } } int current = computeConflicts(color); if (current < bestConflicts) { bestConflicts = current; bestColor = color; } } } bool exactDFS(int remaining) { if (exactUsed >= exactLimit) { exactAborted = true; return false; } ++exactUsed; if (remaining == 0) return true; int chosen = -1; int minimumAvailable = 4; int maximumDegree = -1; for (int v = 0; v < n; ++v) { if (exactColor[v] != -1) continue; int available = 0; for (int c = 0; c < 3; ++c) { if (forbidden[v][c] == 0) ++available; } if (available == 0) return false; int degree = (int)graph[v].size(); if (available < minimumAvailable || (available == minimumAvailable && degree > maximumDegree)) { chosen = v; minimumAvailable = available; maximumDegree = degree; } } array<int, 3> order{0, 1, 2}; int shift = rng.integer(3); for (int k = 0; k < 3; ++k) { int c = order[(k + shift) % 3]; if (forbidden[chosen][c] != 0) continue; exactColor[chosen] = c; for (int u : graph[chosen]) { if (exactColor[u] == -1) ++forbidden[u][c]; } if (exactDFS(remaining - 1)) return true; for (int u : graph[chosen]) { if (exactColor[u] == -1) --forbidden[u][c]; } exactColor[chosen] = -1; if (exactAborted) return false; } return false; } bool attemptExactColoring(long long &globalBudget, long long allowance, vector<int> &answer) { if (globalBudget <= 0 || allowance <= 0) return false; exactLimit = min(globalBudget, allowance); exactUsed = 0; exactAborted = false; exactColor.assign(n, -1); forbidden.assign(n, {}); for (auto &a : forbidden) a.fill(0); int root = 0; for (int v = 1; v < n; ++v) { if (graph[v].size() > graph[root].size()) root = v; } exactColor[root] = 0; for (int u : graph[root]) { if (exactColor[u] == -1) ++forbidden[u][0]; } bool success = exactDFS(n - 1); globalBudget -= min(globalBudget, exactUsed); if (success) answer = exactColor; return success; } void rebuildBadVertices() { badFlag.assign(n, 0); badPosition.assign(n, -1); badVertices.clear(); badVertices.reserve(n); for (int v = 0; v < n; ++v) { if (neighborCount[v][color[v]] > 0) { badFlag[v] = 1; badPosition[v] = (int)badVertices.size(); badVertices.push_back(v); } } } void rebuildState() { for (int v = 0; v < n; ++v) { neighborCount[v].fill(0); for (int u : graph[v]) { ++neighborCount[v][color[u]]; } } conflicts = 0; for (int v = 0; v < n; ++v) { conflicts += neighborCount[v][color[v]]; } conflicts /= 2; rebuildBadVertices(); } inline void refreshBad(int v) { bool nowBad = neighborCount[v][color[v]] > 0; if (nowBad == (bool)badFlag[v]) return; badFlag[v] = nowBad; if (nowBad) { badPosition[v] = (int)badVertices.size(); badVertices.push_back(v); } else { int p = badPosition[v]; int last = badVertices.back(); badVertices[p] = last; badPosition[last] = p; badVertices.pop_back(); badPosition[v] = -1; } } inline void applyMove(int v, int newColor) { int oldColor = color[v]; if (oldColor == newColor) return; conflicts += neighborCount[v][newColor] - neighborCount[v][oldColor]; color[v] = newColor; refreshBad(v); for (int u : graph[v]) { --neighborCount[u][oldColor]; ++neighborCount[u][newColor]; refreshBad(u); } } inline int swapDelta(int u, int v) const { int a = color[u]; int b = color[v]; int delta = neighborCount[u][b] - neighborCount[u][a] + neighborCount[v][a] - neighborCount[v][b]; if (adjacent[u * n + v]) delta -= 2; return delta; } inline void applySwap(int u, int v) { int a = color[u]; int b = color[v]; if (a == b) return; conflicts += swapDelta(u, v); color[u] = b; color[v] = a; for (int x : graph[u]) { --neighborCount[x][a]; ++neighborCount[x][b]; } for (int x : graph[v]) { --neighborCount[x][b]; ++neighborCount[x][a]; } rebuildBadVertices(); } inline void saveBestIfBetter() { if (conflicts < bestConflicts) { bestConflicts = conflicts; bestColor = color; } } void restoreBest() { color = bestColor; rebuildState(); } void improveVertices(int maximumMoves) { for (int step = 0; step < maximumMoves; ++step) { int bestDelta = 0; int chosenVertex = -1; int chosenColor = -1; uint64_t bestKey = 0; for (int v : badVertices) { int current = color[v]; for (int c = 0; c < 3; ++c) { if (c == current) continue; int delta = neighborCount[v][c] - neighborCount[v][current]; if (delta < bestDelta) { bestDelta = delta; chosenVertex = v; chosenColor = c; bestKey = rng.next(); } else if (delta == bestDelta) { uint64_t key = rng.next(); if (key > bestKey) { bestKey = key; chosenVertex = v; chosenColor = c; } } } } if (chosenVertex == -1) break; applyMove(chosenVertex, chosenColor); saveBestIfBetter(); } } void improveSwaps(int maximumMoves) { for (int step = 0; step < maximumMoves; ++step) { int bestDelta = 0; int chosenU = -1; int chosenV = -1; uint64_t bestKey = 0; for (int u = 0; u < n; ++u) { int a = color[u]; for (int v = u + 1; v < n; ++v) { int b = color[v]; if (a == b) continue; int delta = neighborCount[u][b] - neighborCount[u][a] + neighborCount[v][a] - neighborCount[v][b]; if (adjacent[u * n + v]) delta -= 2; if (delta < bestDelta) { bestDelta = delta; chosenU = u; chosenV = v; bestKey = rng.next(); } else if (delta == bestDelta) { uint64_t key = rng.next(); if (key > bestKey) { bestKey = key; chosenU = u; chosenV = v; } } } } if (chosenU == -1) break; applySwap(chosenU, chosenV); saveBestIfBetter(); } } void perturb() { int numberOfSwaps = 2 + n / 100; for (int k = 0; k < numberOfSwaps; ++k) { int u = rng.integer(n); int v = -1; for (int attempt = 0; attempt < 20; ++attempt) { v = rng.integer(n); if (u != v && color[u] != color[v]) break; } if (u != v && color[u] != color[v]) { applySwap(u, v); saveBestIfBetter(); } } int selectedCount = min(n, max(2, n / 25)); vector<int> selected; vector<unsigned char> marked(n, 0); selected.reserve(selectedCount); if (!badVertices.empty()) { int v = badVertices[rng.integer((int)badVertices.size())]; selected.push_back(v); marked[v] = 1; } while ((int)selected.size() < selectedCount) { int v = rng.integer(n); if (!marked[v]) { marked[v] = 1; selected.push_back(v); } } for (int v : selected) { int c = rng.integer(3); if (c == color[v]) c = (c + 1) % 3; applyMove(v, c); saveBestIfBetter(); } for (int i = (int)selected.size() - 1; i > 0; --i) { int j = rng.integer(i + 1); swap(selected[i], selected[j]); } for (int v : selected) { int minimum = INT_MAX; for (int c = 0; c < 3; ++c) { minimum = min(minimum, neighborCount[v][c]); } array<int, 3> choices{0, 0, 0}; int numberOfChoices = 0; for (int c = 0; c < 3; ++c) { if (neighborCount[v][c] == minimum) { choices[numberOfChoices++] = c; } } int c = choices[rng.integer(numberOfChoices)]; if (c != color[v]) { applyMove(v, c); saveBestIfBetter(); } } } void optimize(int iterationAllowance) { restoreBest(); tabuUntil.resize(n); for (auto &a : tabuUntil) a.fill(0); int phaseLength = max(400, min(8000, 8 * n)); int phaseBest = bestConflicts; for (int iteration = 0; iteration < iterationAllowance && bestConflicts > 0; ++iteration) { if (badVertices.empty()) break; int anyDelta = INT_MAX; int anyVertex = -1; int anyColor = -1; uint64_t anyKey = 0; int allowedDelta = INT_MAX; int allowedVertex = -1; int allowedColor = -1; uint64_t allowedKey = 0; bool haveAllowed = false; for (int v : badVertices) { int current = color[v]; for (int c = 0; c < 3; ++c) { if (c == current) continue; int delta = neighborCount[v][c] - neighborCount[v][current]; if (delta < anyDelta) { anyDelta = delta; anyVertex = v; anyColor = c; anyKey = rng.next(); } else if (delta == anyDelta) { uint64_t key = rng.next(); if (key > anyKey) { anyKey = key; anyVertex = v; anyColor = c; } } bool isTabu = tabuUntil[v][c] > iteration; bool aspiration = conflicts + delta < bestConflicts; if (isTabu && !aspiration) continue; if (delta < allowedDelta) { allowedDelta = delta; allowedVertex = v; allowedColor = c; allowedKey = rng.next(); haveAllowed = true; } else if (delta == allowedDelta) { uint64_t key = rng.next(); if (key > allowedKey) { allowedKey = key; allowedVertex = v; allowedColor = c; } } } } int selectedDelta = haveAllowed ? allowedDelta : anyDelta; int selectedVertex = haveAllowed ? allowedVertex : anyVertex; int selectedColor = haveAllowed ? allowedColor : anyColor; bool useSwap = false; int swapU = -1; int swapV = -1; if ((iteration & 7) == 7 && badVertices.size() >= 2) { int bestSwapDelta = INT_MAX; uint64_t bestSwapKey = 0; for (int sample = 0; sample < 32; ++sample) { int u = badVertices[rng.integer((int)badVertices.size())]; int v; if (rng.next() & 1ULL) { v = badVertices[rng.integer((int)badVertices.size())]; } else { v = rng.integer(n); } if (u == v || color[u] == color[v]) continue; int delta = swapDelta(u, v); if (delta < bestSwapDelta) { bestSwapDelta = delta; bestSwapKey = rng.next(); swapU = u; swapV = v; } else if (delta == bestSwapDelta) { uint64_t key = rng.next(); if (key > bestSwapKey) { bestSwapKey = key; swapU = u; swapV = v; } } } if (swapU != -1 && (bestSwapDelta < selectedDelta || (bestSwapDelta == selectedDelta && (rng.next() & 1ULL)))) { useSwap = true; selectedDelta = bestSwapDelta; } } int tenure = 8 + rng.integer(8); if (useSwap) { int oldU = color[swapU]; int oldV = color[swapV]; applySwap(swapU, swapV); tabuUntil[swapU][oldU] = iteration + tenure; tabuUntil[swapV][oldV] = iteration + tenure; } else { int oldColor = color[selectedVertex]; applyMove(selectedVertex, selectedColor); tabuUntil[selectedVertex][oldColor] = iteration + tenure; } saveBestIfBetter(); if ((iteration + 1) % phaseLength == 0) { improveVertices(60); improveSwaps(2); saveBestIfBetter(); if (bestConflicts >= phaseBest) { restoreBest(); for (auto &a : tabuUntil) a.fill(0); perturb(); saveBestIfBetter(); } phaseBest = bestConflicts; } } restoreBest(); for (int round = 0; round < 5; ++round) { int before = bestConflicts; improveVertices(200); improveSwaps(8); if (bestConflicts == before) break; } saveBestIfBetter(); } public: CoreSolver(vector<vector<int>> g, uint64_t seed) : n((int)g.size()), graph(std::move(g)), rng(seed) { adjacent.assign((size_t)n * n, 0); edgeCount = 0; for (int u = 0; u < n; ++u) { for (int v : graph[u]) { if (!adjacent[u * n + v]) { adjacent[u * n + v] = 1; adjacent[v * n + u] = 1; ++edgeCount; } } } edgeCount /= 2; neighborCount.resize(n); } vector<int> solve(long long &globalExactBudget, long long exactAllowance, int iterationAllowance) { if (n == 0) return {}; if (edgeCount == (long long)n * (n - 1) / 2) { vector<int> answer(n); for (int v = 0; v < n; ++v) answer[v] = v % 3; return answer; } if (findBipartiteColoring()) return color; greedyColoring(); if (bestConflicts == 0) return bestColor; vector<int> exactAnswer; if (attemptExactColoring(globalExactBudget, exactAllowance, exactAnswer)) { return exactAnswer; } restoreBest(); improveVertices(100); improveSwaps(8); saveBestIfBetter(); optimize(iterationAllowance); return bestColor; } }; int main() { FastScanner scanner; int n, m; if (!scanner.readInt(n)) return 0; scanner.readInt(m); vector<vector<int>> graph(n); for (int i = 0; i < m; ++i) { int u, v; scanner.readInt(u); scanner.readInt(v); --u; --v; graph[u].push_back(v); graph[v].push_back(u); } vector<int> degree(n); vector<unsigned char> removed(n, 0); vector<int> removalOrder; removalOrder.reserve(n); for (int v = 0; v < n; ++v) degree[v] = (int)graph[v].size(); vector<int> queue; queue.reserve(n); for (int v = 0; v < n; ++v) { if (degree[v] < 3) queue.push_back(v); } for (size_t head = 0; head < queue.size(); ++head) { int v = queue[head]; if (removed[v]) continue; removed[v] = 1; removalOrder.push_back(v); for (int u : graph[v]) { if (!removed[u]) { --degree[u]; if (degree[u] == 2) queue.push_back(u); } } } vector<unsigned char> visited(n, 0); vector<vector<int>> coreComponents; for (int root = 0; root < n; ++root) { if (removed[root] || visited[root]) continue; vector<int> component; component.push_back(root); visited[root] = 1; for (size_t head = 0; head < component.size(); ++head) { int v = component[head]; for (int u : graph[v]) { if (!removed[u] && !visited[u]) { visited[u] = 1; component.push_back(u); } } } coreComponents.push_back(std::move(component)); } sort(coreComponents.begin(), coreComponents.end(), [](const vector<int> &a, const vector<int> &b) { return a.size() < b.size(); }); int totalCoreVertices = 0; for (const auto &component : coreComponents) { totalCoreVertices += (int)component.size(); } vector<int> answer(n, -1); long long exactBudget = 300000; const long long totalLocalIterations = 120000; for (size_t componentIndex = 0; componentIndex < coreComponents.size(); ++componentIndex) { const auto &component = coreComponents[componentIndex]; int size = (int)component.size(); vector<int> localIndex(n, -1); for (int i = 0; i < size; ++i) { localIndex[component[i]] = i; } vector<vector<int>> localGraph(size); for (int i = 0; i < size; ++i) { int v = component[i]; for (int u : graph[v]) { if (!removed[u] && localIndex[u] != -1) { localGraph[i].push_back(localIndex[u]); } } } long long exactAllowance = 100000; if (!coreComponents.empty()) { long long remainingComponents = (long long)coreComponents.size() - (long long)componentIndex; exactAllowance = min(exactAllowance, max(5000LL, exactBudget / remainingComponents)); } int localAllowance = 3000; if (totalCoreVertices > 0) { localAllowance = (int)min( 120000LL, max(3000LL, totalLocalIterations * size / totalCoreVertices) ); } uint64_t seed = 0x9e3779b97f4a7c15ULL ^ ((uint64_t)size << 32) ^ ((uint64_t)componentIndex * 0x94d049bb133111ebULL); CoreSolver solver(std::move(localGraph), seed); vector<int> localAnswer = solver.solve( exactBudget, exactAllowance, localAllowance ); for (int i = 0; i < size; ++i) { answer[component[i]] = localAnswer[i]; } } for (int i = (int)removalOrder.size() - 1; i >= 0; --i) { int v = removalOrder[i]; array<int, 3> used{0, 0, 0}; for (int u : graph[v]) { if (answer[u] != -1) used[answer[u]] = 1; } int chosen = -1; for (int c = 0; c < 3; ++c) { if (!used[c]) { chosen = c; break; } } if (chosen == -1) { array<int, 3> counts{0, 0, 0}; for (int u : graph[v]) { if (answer[u] != -1) ++counts[answer[u]]; } chosen = (int)(min_element(counts.begin(), counts.end()) - counts.begin()); } answer[v] = chosen; } string output; output.reserve((size_t)n * 2); for (int v = 0; v < n; ++v) { if (v) output.push_back(' '); output.push_back(char('0' + answer[v] + 1)); } output.push_back('\n'); fwrite(output.data(), 1, output.size(), stdout); return 0; }