题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
# Maximum Independent Set Challenge
## Context
You are given an undirected graph G = (V, E) with |V| = N vertices and |E| = M edges.
You must select a subset of vertices S ⊆ V to form an **Independent Set**.
A subset S is a valid independent set if and only if:
for every pair of distinct vertices u, v ∈ S, there is NO edge between them.
(i.e., {u, v} ∉ E).
This is a heuristic optimization problem.
You are NOT required to find the theoretically optimal solution.
Instead, you should try to **maximize** the size of the set S (denoted as |S|).
Points depend on your set size relative to the stored reference; see Scoring.
## Input Format
The first line contains two integers:
N M
where:
2 ≤ N ≤ 10,000
1 ≤ M ≤ 500,000
The next M lines each contain two integers:
u v
representing an undirected edge {u, v},
with 1 ≤ u, v ≤ N and u ≠ v.
Multiple edges between the same pair of vertices may appear but imply the same constraint.
## Output Format
Output exactly N lines.
The i-th line must contain one integer x_i ∈ {0, 1}:
- 1 indicates that vertex i is included in the independent set (i ∈ S).
- 0 indicates that vertex i is excluded from the independent set (i ∉ S).
Requirements:
- For any pair of vertices u, v where x_u = 1 and x_v = 1, it must be true that {u, v} ∉ E.
## Scoring
The physical objective is independent-set cardinality K; larger is better. The physical reference is `a = R`; matching a positive reference earns 60 points.
R is the positive stored reference independent-set size.
Define the historical low-score expression (in points):
P_low = 60*K/R.
If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award:
r = K / a
P = min(100, 60 + ln(r)).
Here `r` is the improvement factor relative to the physical reference, and `ln` is the natural logarithm with coefficient 1. In the uncapped upper branch, `r = exp(P-60)`: each additional point means an `exp(1)`-fold improvement in the physical objective. The factor is computed from the physical objective and its reference, not from a transformed reward or the old point score. Invalid output retains its existing rejection or zero score; the zero-cost exception applies only after validation. Use one score per file. Both reported ratio channels use points divided by 100. Across official files, take the arithmetic mean.
## Example
Input:
4 3
1 2
2 3
3 4
Output:
1
0
0
1
Explanation:
The edges are {1,2}, {2,3}, {3,4}.
The output selects vertices 1 and 4 (S={1, 4}).
- Is {1,4} an edge? No.
- Vertices 2 and 3 are not selected, so their connections don't violate independence.
Total size K = 2.
If the stored reference independent-set size is R=2, then r=2/2=1 and P=60 points.
(Note: Selecting {1, 2} would be invalid because there is an edge between them.)
## Constraints
- 2 ≤ N ≤ 10,000
- 1 ≤ M ≤ 500,000
- Time Limit: 2.0s
- Memory Limit: 512MB
结果
Kimi K3.1 ProKimi
max
31.9
平均得分 ↑
- 31.9采样 1 · 部分分
- 25.1k平均输出 token(含思考) ↓
- 28.7输出速度 tokens/s ↑
- 875 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
31.6
平均得分 ↑
- 31.6采样 1 · 部分分
- 11.0k平均输出 token(含思考) ↓
- 28.1输出速度 tokens/s ↑
- 393 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
0.0
平均得分 ↑
- 0.0采样 1 · 编译错误
- 37.3k平均输出 token(含思考) ↓
- 191.4输出速度 tokens/s ↑
- 196 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
0.0
平均得分 ↑
- 0.0采样 1 · 编译错误
- 72.6k平均输出 token(含思考) ↓
- 106.2输出速度 tokens/s ↑
- 685 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 526 行
#include <bits/stdc++.h> using namespace std; struct FastInput { static const int SZ = 1 << 20; char buf[SZ]; int len = 0, pos = 0; inline char getChar() { if (pos >= len) { len = (int)fread(buf, 1, SZ, stdin); pos = 0; if (len == 0) return 0; } return buf[pos++]; } bool readInt(int &x) { char c; do { c = getChar(); if (!c) return false; } while (c <= ' '); x = 0; while (c > ' ') { x = x * 10 + (c - '0'); c = getChar(); } return true; } }; int N, W; vector<vector<int>> G; vector<uint64_t> adjBits; mt19937 rng(0x9e3779b9U); chrono::steady_clock::time_point startTime; const double TIME_LIMIT = 1.86; inline double elapsed() { return chrono::duration<double>(chrono::steady_clock::now() - startTime).count(); } inline bool timeUp() { return elapsed() >= TIME_LIMIT; } inline bool hasEdge(int u, int v) { return (adjBits[(size_t)u * W + (v >> 6)] >> (v & 63)) & 1ULL; } void outputSet(const vector<char> &sol) { string out; out.reserve((size_t)N * 2); for (int i = 0; i < N; ++i) { out.push_back(sol[i] ? '1' : '0'); out.push_back('\n'); } fwrite(out.data(), 1, out.size(), stdout); } struct HopcroftKarp { const vector<int> ∂ vector<int> matchL, matchR, dist, q; HopcroftKarp(const vector<int> &p) : part(p), matchL(N, -1), matchR(N, -1), dist(N, -1), q(N) {} bool bfs() { fill(dist.begin(), dist.end(), -1); int qh = 0, qt = 0; for (int u = 0; u < N; ++u) { if (part[u] == 0 && matchL[u] == -1) { dist[u] = 0; q[qt++] = u; } } bool found = false; while (qh < qt) { int u = q[qh++]; for (int v : G[u]) { int nxt = matchR[v]; if (nxt == -1) { found = true; } else if (dist[nxt] == -1) { dist[nxt] = dist[u] + 1; q[qt++] = nxt; } } } return found; } bool dfs(int u) { for (int v : G[u]) { int nxt = matchR[v]; if (nxt == -1 || (dist[nxt] == dist[u] + 1 && dfs(nxt))) { matchL[u] = v; matchR[v] = u; return true; } } dist[u] = -1; return false; } bool run() { while (true) { if (elapsed() > 1.68) return false; if (!bfs()) return true; for (int u = 0; u < N; ++u) { if (part[u] == 0 && matchL[u] == -1 && dist[u] == 0) { dfs(u); } } } } }; vector<char> staticGreedy() { vector<int> order(N); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int a, int b) { if (G[a].size() != G[b].size()) return G[a].size() < G[b].size(); return a < b; }); vector<char> blocked(N, 0), sol(N, 0); for (int v : order) { if (blocked[v]) continue; sol[v] = 1; blocked[v] = 1; for (int u : G[v]) blocked[u] = 1; } return sol; } vector<char> dynamicGreedy(int noise) { struct Node { uint64_t key; int d, v; bool operator<(const Node &other) const { return key > other.key; } }; vector<int> deg(N); vector<char> active(N, 1); vector<uint32_t> randomValue(N); vector<char> sol(N, 0); priority_queue<Node> pq; for (int i = 0; i < N; ++i) { deg[i] = (int)G[i].size(); randomValue[i] = rng(); } auto makeKey = [&](int v, int d) -> uint64_t { uint64_t key = (uint64_t)d << 12; if (noise == 0) key += randomValue[v] & 4095U; else key += randomValue[v] % (noise * 4096U + 1U); return key; }; for (int i = 0; i < N; ++i) { pq.push({makeKey(i, deg[i]), deg[i], i}); } auto removeVertex = [&](int x) { active[x] = 0; for (int y : G[x]) { if (active[y]) { --deg[y]; pq.push({makeKey(y, deg[y]), deg[y], y}); } } }; while (!pq.empty()) { Node node = pq.top(); pq.pop(); int v = node.v; if (!active[v] || node.d != deg[v]) continue; sol[v] = 1; removeVertex(v); for (int u : G[v]) { if (active[u]) removeVertex(u); } } return sol; } struct State { vector<char> inS; vector<int> tight; int sz = 0; vector<vector<int>> groups; vector<int> touched; vector<uint64_t> groupBits; State() : inS(N, 0), tight(N, 0), groups(N), groupBits(W) {} void build(const vector<char> &sol) { inS = sol; fill(tight.begin(), tight.end(), 0); sz = 0; for (int u = 0; u < N; ++u) { if (!inS[u]) continue; ++sz; for (int v : G[u]) ++tight[v]; } } inline void add(int v) { inS[v] = 1; ++sz; for (int u : G[v]) ++tight[u]; } inline void remove(int v) { inS[v] = 0; --sz; for (int u : G[v]) --tight[u]; } void maximalize(bool randomize) { vector<int> candidates; for (int v = 0; v < N; ++v) { if (!inS[v] && tight[v] == 0) candidates.push_back(v); } if (candidates.empty()) return; if (randomize) { shuffle(candidates.begin(), candidates.end(), rng); stable_sort(candidates.begin(), candidates.end(), [&](int a, int b) { return G[a].size() < G[b].size(); }); } else { sort(candidates.begin(), candidates.end(), [&](int a, int b) { if (G[a].size() != G[b].size()) return G[a].size() < G[b].size(); return a < b; }); } for (int v : candidates) { if (!inS[v] && tight[v] == 0) add(v); } } bool findPair(const vector<int> &group, int &a, int &b) { fill(groupBits.begin(), groupBits.end(), 0ULL); for (int v : group) { groupBits[v >> 6] |= 1ULL << (v & 63); } for (int v : group) { uint64_t selfMask = 1ULL << (v & 63); groupBits[v >> 6] &= ~selfMask; const uint64_t *row = &adjBits[(size_t)v * W]; bool found = false; for (int j = 0; j < W; ++j) { uint64_t available = groupBits[j] & ~row[j]; if (available) { a = v; b = (j << 6) + __builtin_ctzll(available); found = true; break; } } groupBits[v >> 6] |= selfMask; if (found) return true; } return false; } void improve(bool randomMode) { vector<int> selected; selected.reserve(N); while (!timeUp()) { maximalize(randomMode); for (int u : touched) groups[u].clear(); touched.clear(); selected.clear(); for (int u = 0; u < N; ++u) { if (inS[u]) selected.push_back(u); } if (randomMode) shuffle(selected.begin(), selected.end(), rng); for (size_t i = 0; i < selected.size(); ++i) { if ((i & 63) == 0 && timeUp()) return; int u = selected[i]; for (int v : G[u]) { if (!inS[v] && tight[v] == 1) groups[u].push_back(v); } if (!groups[u].empty()) touched.push_back(u); } bool swapped = false; for (int u : selected) { if (groups[u].size() < 2) continue; int a, b; if (findPair(groups[u], a, b)) { remove(u); add(a); add(b); swapped = true; break; } } if (!swapped) break; } } void perturb(int strength) { vector<int> removed; for (int step = 0; step < strength; ++step) { int v = -1; if (rng() % 100 < 85) { int bestTight = INT_MAX; for (int t = 0; t < 24; ++t) { int x = (int)(rng() % N); if (inS[x]) continue; if (tight[x] < bestTight || (tight[x] == bestTight && (rng() & 1))) { bestTight = tight[x]; v = x; } } } if (v == -1) { int start = (int)(rng() % N); for (int i = 0; i < N; ++i) { int x = start + i; if (x >= N) x -= N; if (!inS[x]) { v = x; break; } } } if (v == -1) return; removed.clear(); for (int u : G[v]) { if (inS[u]) removed.push_back(u); } for (int u : removed) remove(u); if (!inS[v]) add(v); } } }; int main() { startTime = chrono::steady_clock::now(); static FastInput in; int M; if (!in.readInt(N)) return 0; in.readInt(M); W = (N + 63) >> 6; G.assign(N, {}); adjBits.assign((size_t)N * W, 0ULL); for (int i = 0; i < M; ++i) { int u, v; in.readInt(u); in.readInt(v); --u; --v; size_t idx = (size_t)u * W + (v >> 6); uint64_t mask = 1ULL << (v & 63); if (adjBits[idx] & mask) continue; adjBits[idx] |= mask; adjBits[(size_t)v * W + (u >> 6)] |= 1ULL << (u & 63); G[u].push_back(v); G[v].push_back(u); } vector<int> part(N, -1), q(N); bool bipartite = true; for (int s = 0; s < N && bipartite; ++s) { if (part[s] != -1) continue; part[s] = 0; int qh = 0, qt = 0; q[qt++] = s; while (qh < qt) { int u = q[qh++]; for (int v : G[u]) { if (part[v] == -1) { part[v] = part[u] ^ 1; q[qt++] = v; } else if (part[v] == part[u]) { bipartite = false; break; } } if (!bipartite) break; } } if (bipartite) { HopcroftKarp hk(part); if (!hk.run()) { int leftCount = 0; for (int i = 0; i < N; ++i) leftCount += (part[i] == 0); vector<char> fallback(N, 0); int chosenPart = (leftCount * 2 >= N ? 0 : 1); for (int i = 0; i < N; ++i) fallback[i] = (part[i] == chosenPart); outputSet(fallback); return 0; } vector<char> reachable(N, 0); int qh = 0, qt = 0; for (int u = 0; u < N; ++u) { if (part[u] == 0 && hk.matchL[u] == -1) { reachable[u] = 1; q[qt++] = u; } } while (qh < qt) { int u = q[qh++]; if (part[u] == 0) { for (int v : G[u]) { if (hk.matchL[u] != v && !reachable[v]) { reachable[v] = 1; q[qt++] = v; } } } else { int v = hk.matchR[u]; if (v != -1 && !reachable[v]) { reachable[v] = 1; q[qt++] = v; } } } vector<char> exact(N, 0); for (int i = 0; i < N; ++i) { exact[i] = (part[i] == 0 ? reachable[i] : !reachable[i]); } outputSet(exact); return 0; } vector<char> best = staticGreedy(); int bestSize = 0; for (char x : best) bestSize += x; auto runCandidate = [&](int noise) { vector<char> candidate = dynamicGreedy(noise); State state; state.build(candidate); state.improve(false); if (state.sz > bestSize) { bestSize = state.sz; best = state.inS; } }; if (elapsed() < 1.45) runCandidate(0); int extraRuns = 0; while (extraRuns < 2 && elapsed() < 0.82) { runCandidate(1 + extraRuns * 2); ++extraRuns; } State current; current.build(best); int iteration = 0; int noImprovement = 0; while (!timeUp()) { int oldSize = current.sz; int r = (int)(rng() % 100); int strength; if (r < 70) strength = 1; else if (r < 93) strength = 2 + (int)(rng() % 2); else strength = 4 + (int)(rng() % 5); if (noImprovement > 60 && rng() % 12 == 0) { strength = 8 + (int)(rng() % 8); } current.perturb(strength); current.improve(true); if (current.sz > bestSize) { bestSize = current.sz; best = current.inS; noImprovement = 0; } else { ++noImprovement; if (current.sz == bestSize && rng() % 6 == 0) { best = current.inS; } } if (current.sz < bestSize - 3 || current.sz + 3 < oldSize || (iteration % 160 == 159 && current.sz < bestSize)) { current.build(best); } ++iteration; } outputSet(best); return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 408 行
#include <bits/stdc++.h> using namespace std; using Clock = chrono::steady_clock; int n, m, wordCount; vector<vector<int>> g; vector<uint64_t> adjBits; Clock::time_point startTime; mt19937 rng(0x9e3779b9u); inline bool hasEdge(int u, int v) { return (adjBits[(size_t)u * wordCount + (v >> 6)] >> (v & 63)) & 1ULL; } inline bool timeUp() { return chrono::duration<double>(Clock::now() - startTime).count() > 1.80; } struct State { vector<unsigned char> in; vector<int> cnt; int sz = 0; }; State emptyState() { State s; s.in.assign(n, 0); s.cnt.assign(n, 0); s.sz = 0; return s; } inline void addVertex(State &s, int v) { s.in[v] = 1; ++s.sz; for (int u : g[v]) ++s.cnt[u]; } inline void removeVertex(State &s, int v) { s.in[v] = 0; --s.sz; for (int u : g[v]) --s.cnt[u]; } State constructFromOrder(const vector<int> &order) { State s = emptyState(); for (int v : order) { if (s.cnt[v] == 0) addVertex(s, v); } return s; } void maximalize(State &s, const vector<int> &order) { for (int v : order) { if (!s.in[v] && s.cnt[v] == 0) addVertex(s, v); } } State dynamicMinDegreeGreedy() { State s = emptyState(); vector<unsigned char> remaining(n, 1); vector<int> deg(n), tie(n); for (int v = 0; v < n; ++v) { deg[v] = (int)g[v].size(); tie[v] = (int)(rng() & 0x7fffffff); } using Item = tuple<int, int, int>; priority_queue<Item, vector<Item>, greater<Item>> pq; for (int v = 0; v < n; ++v) pq.emplace(deg[v], tie[v], v); while (!pq.empty()) { auto [d, t, v] = pq.top(); pq.pop(); if (!remaining[v] || deg[v] != d) continue; remaining[v] = 0; addVertex(s, v); for (int u : g[v]) { if (!remaining[u]) continue; remaining[u] = 0; for (int w : g[u]) { if (!remaining[w]) continue; --deg[w]; pq.emplace(deg[w], tie[w], w); } } } return s; } void oneTwoImprovements(State &s, int maxRounds) { const int checkLimit = 40000; for (int round = 0; round < maxRounds; ++round) { if ((round & 3) == 0 && timeUp()) return; vector<vector<int>> groups(n); vector<int> blockers; for (int v = 0; v < n; ++v) { if (s.in[v] || s.cnt[v] != 1) continue; for (int u : g[v]) { if (s.in[u]) { if (groups[u].empty()) blockers.push_back(u); groups[u].push_back(v); break; } } } shuffle(blockers.begin(), blockers.end(), rng); bool found = false; int checks = 0; for (int u : blockers) { auto &cand = groups[u]; if (cand.size() < 2) continue; if (cand.size() > 3) shuffle(cand.begin(), cand.end(), rng); for (int i = 0; i < (int)cand.size() && !found; ++i) { for (int j = i + 1; j < (int)cand.size(); ++j) { if (++checks > checkLimit) break; int a = cand[i], b = cand[j]; if (!hasEdge(a, b)) { removeVertex(s, u); addVertex(s, a); addVertex(s, b); found = true; break; } } if (checks > checkLimit) break; } if (found || checks > checkLimit) break; } if (!found) return; } } bool bipartiteColoring(vector<int> &color) { color.assign(n, -1); vector<int> q(n); for (int root = 0; root < n; ++root) { if (color[root] != -1) continue; color[root] = 0; int qh = 0, qt = 0; q[qt++] = root; while (qh < qt) { int v = q[qh++]; for (int u : g[v]) { if (color[u] == -1) { color[u] = color[v] ^ 1; q[qt++] = u; } else if (color[u] == color[v]) { return false; } } } } return true; } vector<unsigned char> solveBipartite(const vector<int> &color) { vector<int> matchL(n, -1), matchR(n, -1), dist(n); auto bfs = [&]() { queue<int> q; for (int v = 0; v < n; ++v) { if (color[v] == 0 && matchL[v] == -1) { dist[v] = 0; q.push(v); } else { dist[v] = -1; } } while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { int nxt = matchR[v]; if (nxt != -1 && dist[nxt] == -1) { dist[nxt] = dist[u] + 1; q.push(nxt); } } } }; auto dfs = [&](auto &&self, int u) -> bool { for (int v : g[u]) { int nxt = matchR[v]; if (nxt == -1 || (dist[nxt] == dist[u] + 1 && self(self, nxt))) { matchL[u] = v; matchR[v] = u; return true; } } dist[u] = -1; return false; }; while (true) { bfs(); bool augmented = false; for (int v = 0; v < n; ++v) { if (color[v] == 0 && matchL[v] == -1) { if (dfs(dfs, v)) augmented = true; } } if (!augmented) break; } vector<unsigned char> reachable(n, 0); queue<int> q; for (int v = 0; v < n; ++v) { if (color[v] == 0 && matchL[v] == -1) { reachable[v] = 1; q.push(v); } } while (!q.empty()) { int u = q.front(); q.pop(); if (color[u] == 0) { for (int v : g[u]) { if (matchL[u] == v || reachable[v]) continue; reachable[v] = 1; q.push(v); } } else { int v = matchR[u]; if (v != -1 && !reachable[v]) { reachable[v] = 1; q.push(v); } } } vector<unsigned char> independent(n, 0); for (int v = 0; v < n; ++v) { if (color[v] == 0) independent[v] = reachable[v]; else independent[v] = !reachable[v]; } return independent; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); startTime = Clock::now(); cin >> n >> m; g.assign(n, {}); wordCount = (n + 63) >> 6; adjBits.assign((size_t)n * wordCount, 0ULL); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; size_t pos = (size_t)u * wordCount + (v >> 6); uint64_t mask = 1ULL << (v & 63); if (adjBits[pos] & mask) continue; adjBits[pos] |= mask; adjBits[(size_t)v * wordCount + (u >> 6)] |= 1ULL << (u & 63); g[u].push_back(v); g[v].push_back(u); } vector<int> bipColor; if (bipartiteColoring(bipColor)) { vector<unsigned char> answer = solveBipartite(bipColor); for (int v = 0; v < n; ++v) cout << (int)answer[v] << '\n'; return 0; } vector<int> baseOrder(n); iota(baseOrder.begin(), baseOrder.end(), 0); sort(baseOrder.begin(), baseOrder.end(), [](int a, int b) { if (g[a].size() != g[b].size()) return g[a].size() < g[b].size(); return a < b; }); State best = dynamicMinDegreeGreedy(); oneTwoImprovements(best, 70); maximalize(best, baseOrder); State candidate = constructFromOrder(baseOrder); if (candidate.sz >= best.sz - 4) { oneTwoImprovements(candidate, 50); maximalize(candidate, baseOrder); } if (candidate.sz > best.sz) best = candidate; vector<pair<double, int>> keyed(n); for (int iter = 0; iter < 14 && !timeUp(); ++iter) { for (int v = 0; v < n; ++v) { double d = (double)g[v].size(); double r = (double)(rng() & 0xffffff) / (double)0xffffff; double key; if (iter & 1) key = d * (0.60 + 0.80 * r); else key = d + r * (3.0 + 0.35 * d); keyed[v] = {key, v}; } sort(keyed.begin(), keyed.end(), [](const auto &a, const auto &b) { return a.first < b.first; }); vector<int> order(n); for (int v = 0; v < n; ++v) order[v] = keyed[v].second; State s = constructFromOrder(order); if (s.sz >= best.sz - 4) { oneTwoImprovements(s, 30); maximalize(s, order); } if (s.sz > best.sz) best = s; } State current = best; vector<int> randomOrder(n); iota(randomOrder.begin(), randomOrder.end(), 0); int iteration = 0, stall = 0; while (!timeUp()) { ++iteration; if (iteration % 40 == 0 || current.sz + 3 < best.sz) current = best; State s = current; int chosen = -1; for (int attempt = 0; attempt < 40; ++attempt) { int v = (int)(rng() % n); if (!s.in[v] && (s.cnt[v] == 1 || (s.cnt[v] == 2 && attempt >= 25))) { chosen = v; break; } } if (chosen == -1) { int start = (int)(rng() % n); for (int k = 0; k < n; ++k) { int v = start + k; if (v >= n) v -= n; if (!s.in[v]) { chosen = v; break; } } } if (chosen == -1) break; for (int u : g[chosen]) { if (s.in[u]) removeVertex(s, u); } addVertex(s, chosen); if (rng() % 100 < 18) { int removals = 1 + (int)(rng() % 3); for (int r = 0; r < removals; ++r) { for (int attempt = 0; attempt < 20; ++attempt) { int v = (int)(rng() % n); if (s.in[v] && v != chosen) { removeVertex(s, v); break; } } } } shuffle(randomOrder.begin(), randomOrder.end(), rng); maximalize(s, randomOrder); if (s.sz >= best.sz - 3 || iteration % 7 == 0) { oneTwoImprovements(s, 10); maximalize(s, randomOrder); } if (s.sz > best.sz) { best = s; stall = 0; } else { ++stall; } if (s.sz >= current.sz || (s.sz + 1 == current.sz && rng() % 5 == 0)) { current = s; } if (stall > 70) { current = best; stall = 0; } } for (int v = 0; v < n; ++v) { if (!best.in[v]) continue; for (int u : g[v]) { if (u > v && best.in[u]) removeVertex(best, u); } } for (int v = 0; v < n; ++v) cout << (int)best.in[v] << '\n'; return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 442 行
#include <bits/stdc++.h> using namespace std; struct Result { int size; uint64_t chosen; }; Result exactMIS(uint64_t mask, const vector<uint64_t>& adj, unordered_map<uint64_t, Result>& memo) { if (mask == 0) return {0, 0}; auto it = memo.find(mask); if (it != memo.end()) return it->second; int isolated = -1; int leaf = -1; int leafNeighbor = -1; uint64_t temp = mask; while (temp) { int v = __builtin_ctzll(temp); temp &= temp - 1; uint64_t neighbors = adj[v] & mask; int deg = __builtin_popcountll(neighbors); if (deg == 0) { isolated = v; break; } else if (deg == 1) { leaf = v; leafNeighbor = __builtin_ctzll(neighbors); break; } } if (isolated != -1) { uint64_t newMask = mask & ~(1ULL << isolated); Result res = exactMIS(newMask, adj, memo); res.size += 1; res.chosen |= (1ULL << isolated); memo[mask] = res; return res; } if (leaf != -1) { uint64_t newMask = mask & ~(1ULL << leaf) & ~(1ULL << leafNeighbor); Result res = exactMIS(newMask, adj, memo); res.size += 1; res.chosen |= (1ULL << leaf); memo[mask] = res; return res; } int bestV = -1; int maxDeg = -1; temp = mask; while (temp) { int v = __builtin_ctzll(temp); temp &= temp - 1; int deg = __builtin_popcountll(adj[v] & mask); if (deg > maxDeg) { maxDeg = deg; bestV = v; } } uint64_t includeMask = mask & ~(1ULL << bestV) & ~adj[bestV]; Result resInc = exactMIS(includeMask, adj, memo); resInc.size += 1; resInc.chosen |= (1ULL << bestV); uint64_t excludeMask = mask & ~(1ULL << bestV); Result resExc = exactMIS(excludeMask, adj, memo); Result best = (resInc.size >= resExc.size) ? resInc : resExc; memo[mask] = best; return best; } int greedyMinDegree(const vector<vector<int>>& adj, mt19937& rng, vector<char>& sol) { int N = adj.size(); vector<int> deg(N); vector<uint32_t> rank(N); for (int i = 0; i < N; ++i) { deg[i] = adj[i].size(); rank[i] = rng(); } vector<char> active(N, 1), inS(N, 0); priority_queue<pair<uint64_t, int>, vector<pair<uint64_t, int>>, greater<pair<uint64_t, int>>> pq; for (int i = 0; i < N; ++i) { pq.push({((uint64_t)deg[i] << 32) | rank[i], i}); } vector<int> removed; while (!pq.empty()) { auto [key, u] = pq.top(); pq.pop(); int d = key >> 32; if (!active[u] || deg[u] != d) continue; inS[u] = 1; removed.clear(); active[u] = 0; removed.push_back(u); for (int v : adj[u]) { if (active[v]) { active[v] = 0; removed.push_back(v); } } for (int x : removed) { for (int y : adj[x]) { if (active[y]) { deg[y]--; pq.push({((uint64_t)deg[y] << 32) | rank[y], y}); } } } } sol = move(inS); int K = 0; for (char c : sol) K += c; return K; } void improveILS(const vector<vector<int>>& adj, vector<char>& bestS, steady_clock::time_point deadline, mt19937& rng) { int N = adj.size(); if (N == 0) return; vector<char> curS = bestS; vector<int> degS(N, 0); for (int v = 0; v < N; ++v) { if (curS[v]) { for (int u : adj[v]) degS[u]++; } } vector<int> sel; for (int v = 0; v < N; ++v) if (curS[v]) sel.push_back(v); int curK = sel.size(); int bestK = curK; bestS = curS; int stagnation = 0; vector<int> candidates; candidates.reserve(1024); vector<char> backupS; vector<int> backupDegS; vector<int> backupSel; int backupK; while (steady_clock::now() < deadline) { int p = 1; bool kick = false; if (stagnation > 500) { p = 2 + (rng() % 3); kick = true; stagnation = 0; } if (kick) { backupS = curS; backupDegS = degS; backupSel = sel; backupK = curK; } candidates.clear(); int remCount = min(p, (int)sel.size()); for (int i = 0; i < remCount; ++i) { int idx = rng() % sel.size(); int u = sel[idx]; sel[idx] = sel.back(); sel.pop_back(); curS[u] = 0; curK--; candidates.push_back(u); for (int v : adj[u]) { degS[v]--; if (!curS[v] && degS[v] == 0) { candidates.push_back(v); } } } shuffle(candidates.begin(), candidates.end(), rng); for (int v : candidates) { if (!curS[v] && degS[v] == 0) { curS[v] = 1; curK++; sel.push_back(v); for (int u : adj[v]) { degS[u]++; } } } if (curK > bestK) { bestK = curK; bestS = curS; stagnation = 0; } else { stagnation++; } if (kick) { if (curK >= backupK) { // accept } else if (curK > bestK) { // accept } else if (curK >= backupK - 1 && (rng() % 100 < 20)) { // accept slightly worse } else { curS = backupS; degS = backupDegS; sel = backupSel; curK = backupK; } } if (curK < bestK - 5) { curS = bestS; fill(degS.begin(), degS.end(), 0); sel.clear(); for (int v = 0; v < N; ++v) { if (curS[v]) { sel.push_back(v); for (int u : adj[v]) degS[u]++; } } curK = sel.size(); } } } vector<char> solveMIS(const vector<vector<int>>& adj, steady_clock::time_point deadline) { int N = adj.size(); if (N == 0) return {}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() ^ (uintptr_t)&adj); vector<char> bestS(N, 0); int bestK = 0; vector<int> degree(N); int maxDeg = 0; for (int i = 0; i < N; ++i) { degree[i] = adj[i].size(); maxDeg = max(maxDeg, degree[i]); } vector<vector<int>> buckets(maxDeg + 1); for (int i = 0; i < N; ++i) { buckets[degree[i]].push_back(i); } vector<int> flatOrder; flatOrder.reserve(N); vector<pair<int, int>> segments; for (int d = 0; d <= maxDeg; ++d) { int l = flatOrder.size(); for (int v : buckets[d]) flatOrder.push_back(v); int r = flatOrder.size(); if (r > l) segments.push_back({l, r}); } vector<int> order(N); vector<char> inS(N); auto now = steady_clock::now(); auto remaining = deadline - now; if (remaining.count() <= 0) remaining = chrono::milliseconds(0); auto staticDeadline = now + remaining * 20 / 100; auto dynDeadline = now + remaining * 50 / 100; int runs = 0; while (steady_clock::now() < staticDeadline && runs < 1000) { order = flatOrder; for (auto& seg : segments) { shuffle(order.begin() + seg.first, order.begin() + seg.second, rng); } fill(inS.begin(), inS.end(), 0); int K = 0; for (int v : order) { bool conflict = false; for (int u : adj[v]) { if (inS[u]) { conflict = true; break; } } if (!conflict) { inS[v] = 1; K++; } } if (K > bestK) { bestK = K; bestS = inS; } runs++; } if (bestK == 0 && N > 0) { order = flatOrder; for (auto& seg : segments) { shuffle(order.begin() + seg.first, order.begin() + seg.second, rng); } fill(inS.begin(), inS.end(), 0); int K = 0; for (int v : order) { bool conflict = false; for (int u : adj[v]) { if (inS[u]) { conflict = true; break; } } if (!conflict) { inS[v] = 1; K++; } } bestK = K; bestS = inS; } int dynRuns = 0; while (steady_clock::now() < dynDeadline && dynRuns < 10) { vector<char> sol; int K = greedyMinDegree(adj, rng, sol); if (K > bestK) { bestK = K; bestS = sol; } dynRuns++; } if (steady_clock::now() < deadline) { improveILS(adj, bestS, deadline, rng); } return bestS; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto programStart = steady_clock::now(); auto overallDeadline = programStart + chrono::milliseconds(1900); 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); } for (int i = 0; i < N; ++i) { sort(adj[i].begin(), adj[i].end()); adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end()); } vector<int> compId(N, -1); vector<vector<int>> components; for (int i = 0; i < N; ++i) { if (compId[i] == -1) { vector<int> verts; vector<int> stack; stack.push_back(i); compId[i] = components.size(); while (!stack.empty()) { int u = stack.back(); stack.pop_back(); verts.push_back(u); for (int v : adj[u]) { if (compId[v] == -1) { compId[v] = components.size(); stack.push_back(v); } } } components.push_back(verts); } } vector<char> finalS(N, 0); vector<int> largeVerts; const int EXACT_LIMIT = 25; vector<int> localIdx(N, -1); for (auto& verts : components) { int sz = verts.size(); if (sz <= EXACT_LIMIT) { for (int i = 0; i < sz; ++i) localIdx[verts[i]] = i; vector<uint64_t> adjMask(sz, 0); for (int i = 0; i < sz; ++i) { int u = verts[i]; for (int v : adj[u]) { int j = localIdx[v]; if (j != -1) adjMask[i] |= (1ULL << j); } } unordered_map<uint64_t, Result> memo; uint64_t fullMask = (1ULL << sz) - 1; Result res = exactMIS(fullMask, adjMask, memo); for (int i = 0; i < sz; ++i) { if (res.chosen & (1ULL << i)) { finalS[verts[i]] = 1; } } for (int v : verts) localIdx[v] = -1; } else { for (int v : verts) largeVerts.push_back(v); } } int L = largeVerts.size(); if (L > 0) { vector<int> globalToLocal(N, -1); for (int i = 0; i < L; ++i) { globalToLocal[largeVerts[i]] = i; } vector<vector<int>> g(L); for (int i = 0; i < L; ++i) { int u = largeVerts[i]; for (int v : adj[u]) { int j = globalToLocal[v]; if (j != -1) g[i].push_back(j); } } vector<char> largeS = solveMIS(g, overallDeadline); for (int i = 0; i < L; ++i) { if (largeS[i]) { finalS[largeVerts[i]] = 1; } } } for (int i = 0; i < N; ++i) { cout << (int)finalS[i] << '\n'; } return 0; }
MiniMax M3.1 Flash · 采样 1 · 1447 行
#include <bits/stdc++.h> using namespace std; using u64 = uint64_t; using u128 = unsigned __int128; static mt19937 rng( static_cast<unsigned>( chrono::steady_clock::now().time_since_epoch().count() ) ); static int randInt(int n) { return n <= 1 ? 0 : static_cast<int>(rng() % static_cast<unsigned>(n)); } struct FastScanner { static constexpr int SIZE = 1 << 16; char buffer[SIZE]; int pos = 0, len = 0; inline char getChar() { if (pos == len) { len = static_cast<int>(fread(buffer, 1, SIZE, stdin)); pos = 0; if (len == 0) return 0; } return buffer[pos++]; } int nextInt() { char c; do { c = getChar(); } while (c <= ' ' && c); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getChar(); } return x; } }; static inline bool adjacent( const vector<vector<u64>>& matrix, int u, int v ) { return (matrix[u][v >> 6] >> (v & 63)) & 1ULL; } struct U128Hash { static u64 mix(u64 x) { x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } size_t operator()(u128 x) const noexcept { u64 low = static_cast<u64>(x); u64 high = static_cast<u64>(x >> 64); return static_cast<size_t>(mix(low ^ mix(high + 0x123456789abcdefULL))); } }; struct MaxClique64 { int n; vector<u64> original; vector<u64> complement; int bestSize = 0; u64 bestMask = 0; long long nodes = 0; long long nodeLimit; bool aborted = false; explicit MaxClique64( int n, const vector<u64>& original, long long nodeLimit = 3000000 ) : n(n), original(original), nodeLimit(nodeLimit) { u64 full = (n == 64 ? ~0ULL : ((1ULL << n) - 1)); complement.resize(n); for (int i = 0; i < n; ++i) { complement[i] = full & ~(original[i] | (1ULL << i)); } } int colorSort(u64 candidates, int* order, int* bound) const { int length = 0; int color = 0; u64 remaining = candidates; while (remaining) { ++color; u64 available = remaining; while (available) { int v = __builtin_ctzll(available); order[length] = v; bound[length] = color; ++length; remaining &= ~(1ULL << v); available &= original[v]; available &= ~(1ULL << v); } } return length; } void expand(u64 candidates, int size, u64 chosen) { if (aborted) return; if (size > bestSize) { bestSize = size; bestMask = chosen; } if (!candidates) return; if (++nodes > nodeLimit) { aborted = true; return; } int order[64], bound[64]; int length = colorSort(candidates, order, bound); for (int i = length - 1; i >= 0; --i) { if (size + bound[i] <= bestSize) return; int v = order[i]; u64 next = candidates & complement[v]; expand(next, size + 1, chosen | (1ULL << v)); if (aborted) return; candidates &= ~(1ULL << v); } } void solve() { u64 full = (n == 64 ? ~0ULL : ((1ULL << n) - 1)); expand(full, 0, 0); } }; struct GeneralComponent { vector<int> vertices; vector<vector<int>> adj; int edgeCount = 0; vector<char> best; vector<char> alternative; }; static vector<char> greedyMinimumDegree( const GeneralComponent& component, int noise, bool useNoise ) { const int n = static_cast<int>(component.vertices.size()); const auto& adj = component.adj; vector<int> degree(n); int maxDegree = 0; for (int i = 0; i < n; ++i) { degree[i] = static_cast<int>(adj[i].size()); maxDegree = max(maxDegree, degree[i]); } vector<vector<int>> bucket(maxDegree + 1); vector<int> bucketPosition(n, -1); vector<char> active(n, true); vector<char> selected(n, false); auto eraseBucket = [&](int v, int d) { auto& b = bucket[d]; int p = bucketPosition[v]; if (p < 0) return; int last = b.back(); b[p] = last; bucketPosition[last] = p; b.pop_back(); bucketPosition[v] = -1; }; auto insertBucket = [&](int v, int d) { bucket[d].push_back(v); bucketPosition[v] = static_cast<int>(bucket[d].size()) - 1; }; for (int v = 0; v < n; ++v) insertBucket(v, degree[v]); int minimumDegree = 0; vector<int> removed; while (true) { while (minimumDegree <= maxDegree && bucket[minimumDegree].empty()) { ++minimumDegree; } if (minimumDegree > maxDegree) break; int chosenDegree = minimumDegree; if (useNoise && noise > 0 && randInt(3) == 0) { for (int offset = 1; offset <= noise; ++offset) { int d = minimumDegree + offset; if (d <= maxDegree && !bucket[d].empty()) { chosenDegree = d; break; } } } int v = bucket[chosenDegree][randInt(static_cast<int>(bucket[chosenDegree].size()))]; selected[v] = true; removed.clear(); removed.push_back(v); for (int u : adj[v]) { if (active[u]) removed.push_back(u); } for (int x : removed) { active[x] = false; eraseBucket(x, degree[x]); } for (int x : removed) { for (int y : adj[x]) { if (active[y]) { eraseBucket(y, degree[y]); --degree[y]; insertBucket(y, degree[y]); minimumDegree = min(minimumDegree, degree[y]); } } } } return selected; } static vector<char> greedyRandomOrder(const GeneralComponent& component) { const int n = static_cast<int>(component.vertices.size()); vector<int> order(n); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), rng); vector<char> selected(n, false); vector<char> blocked(n, false); for (int v : order) { if (blocked[v]) continue; selected[v] = true; for (int u : component.adj[v]) blocked[u] = true; } return selected; } struct HopcroftKarp { const vector<vector<int>>& graph; const vector<int>& leftVertices; const vector<int>& rightVertices; vector<int> pairLeft, pairRight, distance, current; int shortest = 0; HopcroftKarp( const vector<vector<int>>& graph, const vector<int>& leftVertices, const vector<int>& rightVertices ) : graph(graph), leftVertices(leftVertices), rightVertices(rightVertices) { const int n = static_cast<int>(graph.size()); pairLeft.assign(n, -1); pairRight.assign(n, -1); distance.assign(n, -1); current.assign(n, 0); } bool bfs() { const int INF = 1e9; queue<int> q; for (int u : leftVertices) { if (pairLeft[u] == -1) { distance[u] = 0; q.push(u); } else { distance[u] = INF; } } shortest = INF; while (!q.empty()) { int u = q.front(); q.pop(); if (distance[u] >= shortest) continue; for (int v : graph[u]) { int next = pairRight[v]; if (next == -1) { shortest = min(shortest, distance[u] + 1); } else if (distance[next] == INF) { distance[next] = distance[u] + 1; q.push(next); } } } return shortest != INF; } bool dfs(int u) { for (int& i = current[u]; i < static_cast<int>(graph[u].size()); ++i) { int v = graph[u][i]; int next = pairRight[v]; if (next == -1) { if (distance[u] + 1 != shortest) continue; } else { if (distance[next] != distance[u] + 1 || !dfs(next)) continue; } pairLeft[u] = v; pairRight[v] = u; return true; } distance[u] = 1e9; return false; } int solve() { int matching = 0; while (bfs()) { for (int u : leftVertices) current[u] = 0; for (int u : leftVertices) { if (pairLeft[u] == -1 && dfs(u)) ++matching; } } return matching; } }; class LocalOptimizer { static constexpr int MAX_K = 5; struct GroupDescription { u128 key; int poolSize; }; const vector<vector<int>>& adj; int n; int words; vector<vector<u64>> bits; vector<char> selected; vector<int> conflictCount; vector<array<int, MAX_K>> smallBlockers; vector<vector<int>> countBucket; vector<int> countPosition; vector<vector<int>> privateNeighbors; vector<int> privatePosition; vector<int> selectedVertices; vector<int> selectedPosition; vector<int> tabuUntil; vector<int> lockedUntil; void eraseCountBucket(int v, int count) { if (count < 0 || count > MAX_K) return; auto& b = countBucket[count]; int p = countPosition[v]; if (p < 0) return; int last = b.back(); b[p] = last; countPosition[last] = p; b.pop_back(); countPosition[v] = -1; } void insertCountBucket(int v, int count) { if (count < 0 || count > MAX_K) return; countBucket[count].push_back(v); countPosition[v] = static_cast<int>(countBucket[count].size()) - 1; } void removePrivate(int v, int blocker) { if (blocker < 0) return; auto& b = privateNeighbors[blocker]; int p = privatePosition[v]; if (p < 0) return; int last = b.back(); b[p] = last; privatePosition[last] = p; b.pop_back(); privatePosition[v] = -1; } void insertPrivate(int v, int blocker) { if (blocker < 0) return; auto& b = privateNeighbors[blocker]; b.push_back(v); privatePosition[v] = static_cast<int>(b.size()) - 1; } void changeConflict(int v, int delta, int changedBlocker) { int oldCount = conflictCount[v]; int newCount = oldCount + delta; if (oldCount == 1) { removePrivate(v, smallBlockers[v][0]); } if (oldCount <= MAX_K) { if (delta < 0) { int position = -1; for (int i = 0; i < oldCount; ++i) { if (smallBlockers[v][i] == changedBlocker) { position = i; break; } } if (position >= 0) { for (int i = position; i + 1 < oldCount; ++i) { smallBlockers[v][i] = smallBlockers[v][i + 1]; } } else { int count = 0; for (int u : adj[v]) { if (selected[u]) smallBlockers[v][count++] = u; } } } else if (oldCount < MAX_K) { smallBlockers[v][oldCount] = changedBlocker; } } else if (newCount <= MAX_K) { int count = 0; for (int u : adj[v]) { if (selected[u]) smallBlockers[v][count++] = u; } } conflictCount[v] = newCount; if (newCount == 1) { insertPrivate(v, smallBlockers[v][0]); } eraseCountBucket(v, oldCount); insertCountBucket(v, newCount); } void removeFromSelectedList(int v) { int p = selectedPosition[v]; if (p < 0) return; int last = selectedVertices.back(); selectedVertices[p] = last; selectedPosition[last] = p; selectedVertices.pop_back(); selectedPosition[v] = -1; } void addVertex(int v) { eraseCountBucket(v, 0); selected[v] = true; selectedPosition[v] = static_cast<int>(selectedVertices.size()); selectedVertices.push_back(v); for (int u : adj[v]) { if (!selected[u]) changeConflict(u, +1, v); } } void removeVertex(int v) { selected[v] = false; removeFromSelectedList(v); for (int u : adj[v]) { if (!selected[u]) changeConflict(u, -1, v); } conflictCount[v] = 0; smallBlockers[v].fill(-1); insertCountBucket(v, 0); } bool adjacentLocal(int a, int b) const { return (bits[static_cast<size_t>(a) * words + (b >> 6)] >> (b & 63)) & 1ULL; } static u128 encodeKey(const vector<int>& values) { u128 key = 0; for (int i = 0; i < static_cast<int>(values.size()); ++i) { key |= static_cast<u128>(values[i]) << (14 * i); } return key; } static u128 encodeKey(const array<int, MAX_K>& values, int length) { u128 key = 0; for (int i = 0; i < length; ++i) { key |= static_cast<u128>(values[i]) << (14 * i); } return key; } static vector<int> decodeKey(u128 key, int length) { vector<int> result(length); for (int i = 0; i < length; ++i) { result[i] = static_cast<int>((key >> (14 * i)) & 16383ULL); } return result; } int randomSetBit(const vector<u64>& mask) const { int nonzero = 0; int chosenWord = -1; for (int w = 0; w < words; ++w) { if (mask[w]) { ++nonzero; if (randInt(nonzero) == 0) chosenWord = w; } } if (chosenWord < 0) return -1; u64 value = mask[chosenWord]; int amount = __builtin_popcountll(value); int rank = randInt(amount); while (value) { int bit = __builtin_ctzll(value); value &= value - 1; if (rank-- == 0) return chosenWord * 64 + bit; } return -1; } bool searchIndependentDFS( vector<u64>& mask, int need, vector<int>& chosen, int& nodes, int limit ) const { if (need == 0) return true; int count = 0; for (u64 word : mask) count += __builtin_popcountll(word); if (count < need) return false; if (++nodes > limit) return false; if (need == 1) { for (int w = 0; w < words; ++w) { if (mask[w]) { chosen.push_back(w * 64 + __builtin_ctzll(mask[w])); return true; } } return false; } for (int w = 0; w < words; ++w) { while (mask[w]) { int v = w * 64 + __builtin_ctzll(mask[w]); mask[w] &= ~(1ULL << (v & 63)); vector<u64> child(words); const u64* row = &bits[static_cast<size_t>(v) * words]; for (int j = 0; j < words; ++j) { child[j] = mask[j] & ~row[j]; } child[v >> 6] &= ~(1ULL << (v & 63)); chosen.push_back(v); if (searchIndependentDFS(child, need - 1, chosen, nodes, limit)) { return true; } chosen.pop_back(); } } return false; } vector<int> findIndependentSet(const vector<int>& input, int need) const { vector<int> pool = input; sort(pool.begin(), pool.end()); pool.erase(unique(pool.begin(), pool.end()), pool.end()); if (static_cast<int>(pool.size()) < need) return {}; vector<u64> candidate(words, 0); for (int v : pool) candidate[v >> 6] |= 1ULL << (v & 63); vector<pair<int, u64>> order; order.reserve(pool.size()); if (pool.size() <= 512) { for (int v : pool) { const u64* row = &bits[static_cast<size_t>(v) * words]; int degree = 0; for (int w = 0; w < words; ++w) { degree += __builtin_popcountll(row[w] & candidate[w]); } order.emplace_back(degree, static_cast<u64>(rng())); } } else { for (int v : pool) { order.emplace_back(static_cast<int>(adj[v].size()), static_cast<u64>(rng())); } } sort(order.begin(), order.end(), [](const auto& a, const auto& b) { if (a.first != b.first) return a.first < b.first; return a.second < b.second; }); vector<int> result; result.reserve(need); for (const auto& entry : order) { int v = pool[static_cast<size_t>(entry.second & 0xFFFFFFFFULL)]; bool ok = true; for (int u : result) { if (adjacentLocal(v, u)) { ok = false; break; } } if (ok) { result.push_back(v); if (static_cast<int>(result.size()) == need) return result; } } if (pool.size() <= 512) { vector<u64> mask = candidate; vector<int> found; int nodes = 0; if (searchIndependentDFS(mask, need, found, nodes, 10000)) { return found; } } if (need == 2) { int attempts = min<int>(pool.size(), 128); for (int attempt = 0; attempt < attempts; ++attempt) { int v = pool[attempt]; const u64* row = &bits[static_cast<size_t>(v) * words]; for (int w = 0; w < words; ++w) { u64 possible = candidate[w] & ~row[w]; possible &= ~(1ULL << (v & 63)); if (possible) { int u = w * 64 + __builtin_ctzll(possible); return {v, u}; } } } } for (int attempt = 0; attempt < 24; ++attempt) { vector<u64> eligible = candidate; vector<int> chosen; chosen.reserve(need); while (static_cast<int>(chosen.size()) < need) { int v = randomSetBit(eligible); if (v < 0) break; chosen.push_back(v); const u64* row = &bits[static_cast<size_t>(v) * words]; for (int w = 0; w < words; ++w) { eligible[w] &= ~row[w]; } eligible[v >> 6] &= ~(1ULL << (v & 63)); } if (static_cast<int>(chosen.size()) == need) return chosen; } return {}; } void replaceBlockers( const vector<int>& blockers, const vector<int>& replacement, int iteration ) { for (int v : blockers) { removeVertex(v); tabuUntil[v] = iteration + 8 + randInt(8); } for (int v : replacement) { addVertex(v); lockedUntil[v] = iteration + 8 + randInt(8); } } bool trySingleBlockerReplacement() { vector<int> candidates; for (int v : selectedVertices) { if (privateNeighbors[v].size() >= 2) candidates.push_back(v); } sort(candidates.begin(), candidates.end(), [&](int a, int b) { return privateNeighbors[a].size() > privateNeighbors[b].size(); }); int limit = min<int>(40, candidates.size()); for (int i = 0; i < limit; ++i) { int blocker = candidates[i]; vector<int> replacement = findIndependentSet(privateNeighbors[blocker], 2); if (replacement.size() == 2) { replaceBlockers({blocker}, replacement, iteration); return true; } } return false; } bool tryTwoSingletonBlockers() { vector<int> candidates; for (int v : selectedVertices) { if (!privateNeighbors[v].empty()) candidates.push_back(v); } sort(candidates.begin(), candidates.end(), [&](int a, int b) { return privateNeighbors[a].size() > privateNeighbors[b].size(); }); if (candidates.size() > 70) candidates.resize(70); struct PairDescription { int a, b, size; }; vector<PairDescription> pairs; for (int i = 0; i < static_cast<int>(candidates.size()); ++i) { for (int j = i + 1; j < static_cast<int>(candidates.size()); ++j) { int a = candidates[i]; int b = candidates[j]; int size = static_cast<int>(privateNeighbors[a].size() + privateNeighbors[b].size()); if (size >= 3) pairs.push_back({a, b, size}); } } sort(pairs.begin(), pairs.end(), [](const auto& a, const auto& b) { return a.size > b.size; }); int limit = min<int>(24, pairs.size()); for (int i = 0; i < limit; ++i) { vector<int> pool = privateNeighbors[pairs[i].a]; pool.insert(pool.end(), privateNeighbors[pairs[i].b].begin(), privateNeighbors[pairs[i].b].end()); vector<int> replacement = findIndependentSet(pool, 3); if (replacement.size() == 3) { replaceBlockers({pairs[i].a, pairs[i].b}, replacement, iteration); return true; } } return false; } bool tryGroupedReplacement() { array<unordered_map<u128, vector<int>, U128Hash>, MAX_K + 1> groups; for (int k = 2; k <= MAX_K; ++k) { groups[k].max_load_factor(0.7f); groups[k].reserve(countBucket[k].size() * 2 + 1); for (int v : countBucket[k]) { array<int, MAX_K> blockers = smallBlockers[v]; sort(blockers.begin(), blockers.begin() + k); u128 key = encodeKey(blockers, k); groups[k][key].push_back(v); } } for (int k = 2; k <= MAX_K; ++k) { vector<GroupDescription> descriptions; descriptions.reserve(groups[k].size()); for (const auto& entry : groups[k]) { vector<int> blockers = decodeKey(entry.first, k); int poolSize = static_cast<int>(entry.second.size()); for (int blocker : blockers) { poolSize += static_cast<int>(privateNeighbors[blocker].size()); } for (int mask = 1; mask < (1 << k); ++mask) { int subsetSize = __builtin_popcount(static_cast<unsigned>(mask)); if (subsetSize < 2 || subsetSize >= k) continue; vector<int> subset; for (int i = 0; i < k; ++i) { if (mask & (1 << i)) subset.push_back(blockers[i]); } u128 key = encodeKey(subset); auto it = groups[subsetSize].find(key); if (it != groups[subsetSize].end()) { poolSize += static_cast<int>(it->second.size()); } } if (poolSize >= k + 1) { descriptions.push_back({entry.first, poolSize}); } } sort(descriptions.begin(), descriptions.end(), [](const auto& a, const auto& b) { return a.poolSize > b.poolSize; }); int limit; if (k == 2) limit = 20; else if (k == 3) limit = 14; else if (k == 4) limit = 10; else limit = 8; limit = min(limit, static_cast<int>(descriptions.size())); for (int d = 0; d < limit; ++d) { vector<int> blockers = decodeKey(descriptions[d].key, k); vector<int> pool = groups[k].at(descriptions[d].key); for (int blocker : blockers) { const auto& p = privateNeighbors[blocker]; pool.insert(pool.end(), p.begin(), p.end()); } for (int mask = 1; mask < (1 << k); ++mask) { int subsetSize = __builtin_popcount(static_cast<unsigned>(mask)); if (subsetSize < 2 || subsetSize >= k) continue; vector<int> subset; for (int i = 0; i < k; ++i) { if (mask & (1 << i)) subset.push_back(blockers[i]); } u128 key = encodeKey(subset); auto it = groups[subsetSize].find(key); if (it != groups[subsetSize].end()) { const auto& p = it->second; pool.insert(pool.end(), p.begin(), p.end()); } } vector<int> replacement = findIndependentSet(pool, k + 1); if (replacement.size() == k + 1) { replaceBlockers(blockers, replacement, iteration); return true; } } if (k == 2 && tryTwoSingletonBlockers()) return true; } return false; } bool tryReplacement() { if (trySingleBlockerReplacement()) return true; return tryGroupedReplacement(); } void rebuildFrom(const vector<char>& state) { selected = state; for (auto& bucket : countBucket) bucket.clear(); for (auto& bucket : privateNeighbors) bucket.clear(); fill(countPosition.begin(), countPosition.end(), -1); fill(privatePosition.begin(), privatePosition.end(), -1); fill(selectedPosition.begin(), selectedPosition.end(), -1); selectedVertices.clear(); for (int v = 0; v < n; ++v) { conflictCount[v] = 0; smallBlockers[v].fill(-1); tabuUntil[v] = 0; lockedUntil[v] = 0; if (selected[v]) { selectedPosition[v] = static_cast<int>(selectedVertices.size()); selectedVertices.push_back(v); continue; } int count = 0; for (int u : adj[v]) { if (selected[u]) { if (count < MAX_K) smallBlockers[v][count] = u; ++count; } } conflictCount[v] = count; if (count <= MAX_K) insertCountBucket(v, count); if (count == 1) insertPrivate(v, smallBlockers[v][0]); } } public: int iteration = 0; LocalOptimizer( const vector<vector<int>>& adjacency, const vector<char>& initial ) : adj(adjacency), n(static_cast<int>(adjacency.size())) { words = (n + 63) / 64; bits.assign(static_cast<size_t>(n) * words, 0); for (int u = 0; u < n; ++u) { for (int v : adj[u]) { bits[static_cast<size_t>(u) * words + (v >> 6)] |= 1ULL << (v & 63); } } selected.assign(n, false); conflictCount.assign(n, 0); smallBlockers.resize(n); countBucket.resize(MAX_K + 1); countPosition.assign(n, -1); privateNeighbors.resize(n); privatePosition.assign(n, -1); selectedPosition.assign(n, -1); tabuUntil.assign(n, 0); lockedUntil.assign(n, 0); rebuildFrom(initial); } vector<char> run( int maxIterations, chrono::steady_clock::time_point deadline ) { vector<char> best = selected; int bestSize = static_cast<int>(selectedVertices.size()); for (iteration = 1; iteration <= maxIterations; ++iteration) { if ((iteration & 31) == 0 && chrono::steady_clock::now() >= deadline) { break; } if (tryReplacement()) { int currentSize = static_cast<int>(selectedVertices.size()); if (currentSize > bestSize) { bestSize = currentSize; best = selected; } continue; } if (!countBucket[0].empty()) { int v = countBucket[0][randInt(static_cast<int>(countBucket[0].size()))]; addVertex(v); lockedUntil[v] = iteration + 8 + randInt(8); int currentSize = static_cast<int>(selectedVertices.size()); if (currentSize > bestSize) { bestSize = currentSize; best = selected; } continue; } bool moved = false; for (int attempt = 0; attempt < 24 && !countBucket[1].empty(); ++attempt) { int v = countBucket[1][randInt(static_cast<int>(countBucket[1].size()))]; int blocker = smallBlockers[v][0]; if (tabuUntil[v] > iteration || lockedUntil[blocker] > iteration) { continue; } removeVertex(blocker); tabuUntil[blocker] = iteration + 8 + randInt(8); addVertex(v); lockedUntil[v] = iteration + 8 + randInt(8); moved = true; break; } if (moved) continue; bool perturbed = false; for (int k = 2; k <= MAX_K; ++k) { if (countBucket[k].empty()) continue; int v = countBucket[k][randInt(static_cast<int>(countBucket[k].size()))]; vector<int> blockers; for (int i = 0; i < k; ++i) blockers.push_back(smallBlockers[v][i]); for (int blocker : blockers) { removeVertex(blocker); tabuUntil[blocker] = iteration + 12 + randInt(12); } addVertex(v); lockedUntil[v] = iteration + 12 + randInt(12); perturbed = true; break; } if (!perturbed && !selectedVertices.empty()) { int v = selectedVertices[randInt(static_cast<int>(selectedVertices.size()))]; removeVertex(v); tabuUntil[v] = iteration + 12 + randInt(12); } else if (!perturbed) { break; } } return best; } }; int main() { using Clock = chrono::steady_clock; const auto start = Clock::now(); const auto preprocessingDeadline = start + chrono::milliseconds(300); const auto initializationDeadline = start + chrono::milliseconds(1450); const auto hardDeadline = start + chrono::milliseconds(1820); FastScanner scanner; int N = scanner.nextInt(); int M = scanner.nextInt(); if (N <= 0) return 0; const int globalWords = (N + 63) / 64; vector<vector<u64>> adjacencyMatrix( N, vector<u64>(globalWords, 0) ); vector<vector<int>> graph(N); for (int i = 0; i < M; ++i) { int u = scanner.nextInt() - 1; int v = scanner.nextInt() - 1; u64 mask = 1ULL << (v & 63); if ((adjacencyMatrix[u][v >> 6] & mask) == 0) { adjacencyMatrix[u][v >> 6] |= mask; adjacencyMatrix[v][u >> 6] |= 1ULL << (u & 63); graph[u].push_back(v); graph[v].push_back(u); } } vector<char> chosen(N, false); vector<char> active(N, true); vector<u64> activeMask(globalWords, ~0ULL); if (N & 63) activeMask.back() = (1ULL << (N & 63)) - 1; vector<int> reductionQueue; reductionQueue.reserve(N); vector<char> inQueue(N, true); for (int v = 0; v < N; ++v) reductionQueue.push_back(v); auto enqueue = [&](int v) { if (active[v] && !inQueue[v]) { inQueue[v] = true; reductionQueue.push_back(v); } }; auto deactivate = [&](int v) { if (!active[v]) return; active[v] = false; activeMask[v >> 6] &= ~(1ULL << (v & 63)); inQueue[v] = false; for (int u : graph[v]) { if (active[u]) enqueue(u); } }; size_t queueHead = 0; vector<int> activeNeighbors; while (queueHead < reductionQueue.size()) { if ((queueHead & 2047) == 0 && Clock::now() >= preprocessingDeadline) { break; } int v = reductionQueue[queueHead++]; inQueue[v] = false; if (!active[v]) continue; activeNeighbors.clear(); for (int u : graph[v]) { if (active[u]) activeNeighbors.push_back(u); } int degree = static_cast<int>(activeNeighbors.size()); if (degree <= 1) { int neighbor = degree == 1 ? activeNeighbors[0] : -1; chosen[v] = true; deactivate(v); if (neighbor >= 0) deactivate(neighbor); continue; } bool simplicial = degree <= 64; if (simplicial) { for (int u : activeNeighbors) { for (int w : activeNeighbors) { if (u != w && !adjacent(adjacencyMatrix, u, w)) { simplicial = false; break; } } if (!simplicial) break; } } if (simplicial) { vector<int> neighbors = activeNeighbors; chosen[v] = true; deactivate(v); for (int u : neighbors) deactivate(u); } } vector<char> visited(N, false); vector<vector<int>> components; for (int root = 0; root < N; ++root) { if (!active[root] || visited[root]) continue; vector<int> component; component.push_back(root); visited[root] = true; for (size_t head = 0; head < component.size(); ++head) { int v = component[head]; for (int u : graph[v]) { if (active[u] && !visited[u]) { visited[u] = true; component.push_back(u); } } } components.push_back(move(component)); } vector<int> localIndex(N, -1); vector<int> color(N, -1); vector<GeneralComponent> generalComponents; for (const auto& component : components) { int size = static_cast<int>(component.size()); int edgeCount = 0; for (int v : component) { for (int u : graph[v]) { if (active[u]) ++edgeCount; } } edgeCount /= 2; if (size == 1) { chosen[component[0]] = true; continue; } if (edgeCount == size - 1) { vector<int> parent(N, -2); vector<int> order; order.reserve(size); parent[component[0]] = -1; order.push_back(component[0]); for (size_t i = 0; i < order.size(); ++i) { int v = order[i]; for (int u : graph[v]) { if (!active[u] || u == parent[v]) continue; parent[u] = v; order.push_back(u); } } vector<int> dp0(N, 0), dp1(N, 1); for (int i = size - 1; i >= 0; --i) { int v = order[i]; for (int u : graph[v]) { if (!active[u] || parent[u] != v) continue; dp0[v] += max(dp0[u], dp1[u]); dp1[v] += dp0[u]; } } vector<char> take(N, false); for (int v : order) { if (parent[v] == -1) { take[v] = dp1[v] > dp0[v]; } else { int p = parent[v]; take[v] = !take[p] && dp1[v] > dp0[v]; } if (take[v]) chosen[v] = true; } continue; } bool bipartite = true; vector<int> leftVertices, rightVertices; for (int root : component) { if (color[root] != -1) continue; vector<int> queue; queue.push_back(root); color[root] = 0; for (size_t head = 0; head < queue.size(); ++head) { int v = queue[head]; if (color[v] == 0) leftVertices.push_back(v); else rightVertices.push_back(v); for (int u : graph[v]) { if (!active[u]) continue; if (color[u] == -1) { color[u] = color[v] ^ 1; queue.push_back(u); } else if (color[u] == color[v]) { bipartite = false; } } } } if (bipartite) { HopcroftKarp matching(graph, leftVertices, rightVertices); matching.solve(); vector<char> reachableLeft(N, false); vector<char> reachableRight(N, false); queue<int> q; for (int u : leftVertices) { if (matching.pairLeft[u] == -1) { reachableLeft[u] = true; q.push(u); } } while (!q.empty()) { int u = q.front(); q.pop(); for (int v : graph[u]) { if (matching.pairLeft[u] == v || reachableRight[v]) continue; reachableRight[v] = true; int next = matching.pairRight[v]; if (next != -1 && !reachableLeft[next]) { reachableLeft[next] = true; q.push(next); } } } for (int u : leftVertices) { if (reachableLeft[u]) chosen[u] = true; } for (int v : rightVertices) { if (!reachableRight[v]) chosen[v] = true; } continue; } if (edgeCount == size * (size - 1) / 2) { chosen[component[0]] = true; continue; } if (size <= 64) { for (int i = 0; i < size; ++i) { localIndex[component[i]] = i; } vector<u64> original(size, 0); for (int i = 0; i < size; ++i) { int v = component[i]; for (int u : graph[v]) { int j = localIndex[u]; if (j >= 0) original[i] |= 1ULL << j; } } MaxClique64 solver(size, original); solver.solve(); for (int i = 0; i < size; ++i) { if ((solver.bestMask >> i) & 1ULL) chosen[component[i]] = true; } continue; } GeneralComponent gc; gc.vertices = component; gc.adj.resize(size); gc.edgeCount = edgeCount; for (int i = 0; i < size; ++i) { localIndex[component[i]] = i; } for (int i = 0; i < size; ++i) { for (int u : graph[component[i]]) { int j = localIndex[u]; if (j >= 0) gc.adj[i].push_back(j); } } generalComponents.push_back(move(gc)); } for (auto& component : generalComponents) { int size = static_cast<int>(component.vertices.size()); int target = size < 300 ? 40 : (size < 1200 ? 32 : 24); int bestSize = -1; int alternativeSize = -1; for (int attempt = 0; attempt < target; ++attempt) { if (attempt > 0 && Clock::now() >= initializationDeadline) break; vector<char> candidate; if (attempt % 4 == 3) { candidate = greedyRandomOrder(component); } else { int mode = (attempt % 4 == 2) ? 1 : 0; candidate = greedyMinimumDegree(component, mode, mode != 0); } int count = accumulate(candidate.begin(), candidate.end(), 0); if (count > bestSize) { alternativeSize = bestSize; if (component.alternative.size() == static_cast<size_t>(bestSize)) { component.alternative = component.best; } bestSize = count; component.best = move(candidate); } else if (count > alternativeSize) { alternativeSize = count; component.alternative = move(candidate); } } if (component.best.empty()) { component.best = greedyMinimumDegree(component, 0, false); } if (component.alternative.empty()) { component.alternative = component.best; } } double remainingWeight = 0; for (const auto& component : generalComponents) { remainingWeight += max(1, static_cast<int>(component.vertices.size())); } for (auto& component : generalComponents) { if (Clock::now() >= hardDeadline) break; double weight = max(1, static_cast<int>(component.vertices.size())); double available = chrono::duration<double>( hardDeadline - Clock::now() ).count(); available = max(0.0, available); double slice = remainingWeight > 0 ? available * weight / remainingWeight : available; auto componentDeadline = Clock::now() + chrono::duration_cast<Clock::duration>(duration<double>(slice)); LocalOptimizer optimizer(component.adj, component.best); vector<char> result = optimizer.run(30000, componentDeadline); if (Clock::now() < componentDeadline && component.alternative != component.best) { LocalOptimizer second(component.adj, component.alternative); vector<char> other = second.run(30000, componentDeadline); if (accumulate(other.begin(), other.end(), 0) > accumulate(result.begin(), result.end(), 0)) { result = move(other); } } for (int i = 0; i < static_cast<int>(component.vertices.size()); ++i) { chosen[component.vertices[i]] = result[i]; } remainingWeight -= weight; } queue<int> conflictQueue; for (int v = 0; v < N; ++v) { if (chosen[v]) conflictQueue.push(v); } while (!conflictQueue.empty()) { int v = conflictQueue.front(); conflictQueue.pop(); if (!chosen[v]) continue; for (int u : graph[v]) { if (!chosen[u]) continue; if (graph[v].size() >= graph[u].size()) { chosen[v] = false; break; } else { chosen[u] = false; conflictQueue.push(u); } } } string output; output.reserve(static_cast<size_t>(N) * 2); for (int v = 0; v < N; ++v) { output.push_back(chosen[v] ? '1' : '0'); output.push_back('\n'); } fwrite(output.data(), 1, output.size(), stdout); return 0; }