题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
# Forest Mycelium Delay Tour (Optimization Challenge)
## Problem
Vasya is mapping a forest trail network with **mushroom clearings**.
The forest is modeled as an undirected connected graph with `N` clearings (vertices) and `M` trails (edges).
At clearing `i`, mushrooms grow at a constant rate `r[i]` grams per minute, starting from **0 grams at time 0**.
- Vasya starts at clearing `1` at time `t = 0`.
- Each minute, Vasya **must** traverse exactly one trail to an adjacent clearing (no waiting).
- When Vasya **first arrives** at a clearing `i` at time `t`, he instantly collects **all mushrooms grown there so far**, equal to `r[i] * t` grams.
- After the first collection at clearing `i`, that clearing becomes barren and yields **0** on future visits.
- Vasya will walk for exactly `K` minutes (i.e., traverse exactly `K` edges), ending anywhere.
- To be feasible, his walk must visit **every clearing at least once** during times `0..K`.
Your goal is to output a feasible walk that **maximizes the total collected mushroom weight**.
This is an open-ended optimization task: many solutions are valid, and they receive different scores.
## Input
```
N M K
r1 r2 ... rN
u1 v1
u2 v2
...
uM vM
```
- `1 ≤ N ≤ 20000`
- `N-1 ≤ M ≤ 60000`
- `2·(N-1) ≤ K ≤ 200000`
- `1 ≤ r[i] ≤ 10^6`
- `1 ≤ uj, vj ≤ N`, `uj != vj`
- The graph is connected.
- Multiple edges may appear; self-loops do not appear.
The constraints guarantee that a walk of length `K` visiting all vertices exists.
## Output
Output exactly `K` integers:
```
x1 x2 ... xK
```
where `xt` is the clearing Vasya is at **after** minute `t` (after the `t`-th move).
Feasibility requirements:
- Vasya starts at clearing `1` at time `0`.
- For each `t = 1..K`, the move from the previous clearing to `xt` must follow an existing trail (edge).
- Every clearing `1..N` must appear at least once among the visited clearings at times `0..K` (including the start at time 0).
If the output is infeasible, the solution receives **0 score for that test**.
## Objective
Let `pos(t)` be Vasya’s clearing at time `t` (`pos(0)=1`, `pos(t)=xt` for `t≥1`).
For each clearing `i`, define its first-visit time:
- `T[i] = min { t ∈ [0..K] | pos(t) = i }`
Total collected weight:
\[
V = \sum_{i=1}^{N} r[i] \cdot T[i]
\]
(Notice `T[1]=0`, so clearing 1 contributes `0`.)
You must **maximize** `V`.
## Scoring
The physical objective is weighted walk reward V=computeValue(walk); larger is better. The physical reference is `a = 2.5*B`; matching a positive reference earns 60 points.
Define the historical low-score expression (in points):
P_low = 100*max(0,1-B/V) if V>0; 0 otherwise.
If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award:
r = V / 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.
B is the reward of the deterministic baseline walk. If B=0 and V>0, no positive 60-point physical reference exists; retain the existing 100 branch.
## Constraints
- Time limit: 2 seconds
- Memory limit: 256 MB
## Example
Sample input:
```
4 4 6
5 1 4 10
1 2
2 3
3 4
1 4
```
One valid sample output:
```
2 3 2 1 4 3
```
Explanation (high level):
- Path: time 0 at 1, time 1 at 2, time 2 at 3, time 3 at 2, time 4 at 1, time 5 at 4, time 6 at 3.
- First-visit times: `T[1]=0, T[2]=1, T[3]=2, T[4]=5`.
- Objective value: `V = 5·0 + 1·1 + 4·2 + 10·5 = 59`.
- The judge also computes the baseline value `B` for this test, then computes S=100*max(0,1-B/V) when V>0 and applies the piecewise scoring formula above.
结果
Kimi K3.1 ProKimi
max
58.5
平均得分 ↑
- 58.5采样 1 · 部分分
- 41.9k平均输出 token(含思考) ↓
- 26.8输出速度 tokens/s ↑
- 1567 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
58.5
平均得分 ↑
- 58.5采样 1 · 部分分
- 13.1k平均输出 token(含思考) ↓
- 24.7输出速度 tokens/s ↑
- 532 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
56.2
平均得分 ↑
- 56.2采样 1 · 部分分
- 41.8k平均输出 token(含思考) ↓
- 177.1输出速度 tokens/s ↑
- 237 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
0.0
平均得分 ↑
- 0.0采样 1 · 编译错误
- 83.1k平均输出 token(含思考) ↓
- 147.7输出速度 tokens/s ↑
- 564 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 525 行
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll NEG = numeric_limits<ll>::min() / 4; struct Tree { vector<vector<int>> ch; vector<int> order; }; struct DPResult { vector<array<ll, 2>> val; vector<array<int, 2>> len; vector<array<int, 2>> to; vector<array<int, 2>> toPar; }; struct Task { int kind, v, p; }; struct Best { ll value = -1; vector<int> base; int prefixKind = 0; int prefixLen = 0; int x = -1, y = -1; }; struct Solver { int N, M, K; vector<ll> r; ll totalR = 0; vector<vector<int>> adj, adjSorted; vector<int> eu, ev; unordered_set<unsigned long long> edges; vector<pair<int, int>> triangles; Best best; static unsigned long long edgeKey(int a, int b) { if (a > b) swap(a, b); return (unsigned long long)(unsigned int)a << 32 | (unsigned int)b; } bool hasEdge(int a, int b) const { return edges.find(edgeKey(a, b)) != edges.end(); } Tree buildTree(bool dfs, int mode) { Tree t; t.ch.resize(N + 1); t.order.reserve(N); vector<char> vis(N + 1, 0); vis[1] = 1; t.order.push_back(1); auto listSize = [&](int v) { return (int)(mode == 0 ? adj[v].size() : adjSorted[v].size()); }; auto getNeighbor = [&](int v, int i) { const vector<int> &lst = (mode == 0 ? adj[v] : adjSorted[v]); int idx = (mode == 2 ? (int)lst.size() - 1 - i : i); return lst[idx]; }; if (!dfs) { for (size_t head = 0; head < t.order.size(); ++head) { int v = t.order[head]; int d = listSize(v); for (int i = 0; i < d; ++i) { int u = getNeighbor(v, i); if (!vis[u]) { vis[u] = 1; t.ch[v].push_back(u); t.order.push_back(u); } } } } else { vector<int> it(N + 1, 0), st = {1}; while (!st.empty()) { int v = st.back(); bool advanced = false; int d = listSize(v); while (it[v] < d) { int u = getNeighbor(v, it[v]++); if (!vis[u]) { vis[u] = 1; t.ch[v].push_back(u); t.order.push_back(u); st.push_back(u); advanced = true; break; } } if (!advanced) st.pop_back(); } } return t; } DPResult runDP(Tree &tree, const vector<ll> &w, ll lambda) { DPResult d; d.val.resize(N + 1); d.len.resize(N + 1); d.to.resize(N + 1); d.toPar.resize(N + 1); for (int i = 1; i <= N; ++i) { d.val[i] = {NEG, NEG}; d.len[i] = {INT_MAX, INT_MAX}; d.to[i] = {-1, -1}; d.toPar[i] = {-1, -1}; } vector<ll> subW(N + 1), closed(N + 1, 0); vector<int> sz(N + 1); int maxDeg = 0; for (int v = 1; v <= N; ++v) maxDeg = max(maxDeg, (int)tree.ch[v].size()); vector<ll> prefV(maxDeg + 1), prefT(maxDeg + 1); vector<ll> suffV(maxDeg + 1), suffT(maxDeg + 1), suffW(maxDeg + 1); for (int oi = N - 1; oi >= 0; --oi) { int v = tree.order[oi]; auto &ch = tree.ch[v]; sz[v] = 1; subW[v] = w[v]; for (int c : ch) { sz[v] += sz[c]; subW[v] += subW[c]; } sort(ch.begin(), ch.end(), [&](int a, int b) { __int128 lhs = (__int128)subW[a] * sz[b]; __int128 rhs = (__int128)subW[b] * sz[a]; if (lhs != rhs) return lhs < rhs; if (sz[a] != sz[b]) return sz[a] < sz[b]; return a < b; }); int deg = ch.size(); prefV[0] = prefT[0] = 0; for (int i = 0; i < deg; ++i) { int c = ch[i]; ll A = closed[c] - 2 * lambda + subW[c]; prefV[i + 1] = prefV[i] + A + subW[c] * prefT[i]; prefT[i + 1] = prefT[i] + 2LL * sz[c]; } closed[v] = prefV[deg]; suffV[deg] = suffT[deg] = suffW[deg] = 0; for (int i = deg - 1; i >= 0; --i) { int c = ch[i]; ll A = closed[c] - 2 * lambda + subW[c]; ll duration = 2LL * sz[c]; suffV[i] = A + suffV[i + 1] + suffW[i + 1] * duration; suffT[i] = duration + suffT[i + 1]; suffW[i] = subW[c] + suffW[i + 1]; } if (deg == 0) { d.val[v][0] = 0; d.len[v][0] = 0; continue; } for (int i = 0; i < deg; ++i) { int f = ch[i]; ll others = prefV[i] + suffV[i + 1] + suffW[i + 1] * prefT[i]; ll start = prefT[i] + suffT[i + 1]; for (int q = 0; q < 2; ++q) { if (d.val[f][q] == NEG) continue; ll cand = others + d.val[f][q] - lambda + subW[f] * (start + 1); int p = q ^ 1; int length = (int)start + 1 + d.len[f][q]; if (cand > d.val[v][p] || (cand == d.val[v][p] && length < d.len[v][p])) { d.val[v][p] = cand; d.len[v][p] = length; d.to[v][p] = f; d.toPar[v][p] = q; } } } } return d; } vector<int> buildRoute(const Tree &tree, const DPResult &d, int par) { vector<int> base; base.reserve(2 * N); vector<Task> st; st.push_back({1, 1, par}); while (!st.empty()) { Task task = st.back(); st.pop_back(); if (task.kind == 2) { base.push_back(task.v); continue; } int v = task.v; const auto &ch = tree.ch[v]; if (task.kind == 0) { for (int i = (int)ch.size() - 1; i >= 0; --i) { int c = ch[i]; st.push_back({2, v, 0}); st.push_back({0, c, 0}); st.push_back({2, c, 0}); } } else { int p = task.p; int f = d.to[v][p]; if (f == -1) continue; int q = d.toPar[v][p]; st.push_back({1, f, q}); st.push_back({2, f, 0}); for (int i = (int)ch.size() - 1; i >= 0; --i) { int c = ch[i]; if (c == f) continue; st.push_back({2, v, 0}); st.push_back({0, c, 0}); st.push_back({2, c, 0}); } } } return base; } vector<int> shortcut(const vector<int> &base) { int L = base.size(); if (L < 2) return base; int S = L + 1; vector<int> seq(S); seq[0] = 1; for (int i = 0; i < L; ++i) seq[i + 1] = base[i]; vector<int> pre(S), nxt(S), cnt(N + 1, 0); vector<char> alive(S, 1); for (int i = 0; i < S; ++i) { pre[i] = i - 1; nxt[i] = (i + 1 < S ? i + 1 : -1); ++cnt[seq[i]]; } vector<int> q; q.reserve(4 * S); for (int i = 1; i < L; ++i) q.push_back(i); for (size_t head = 0; head < q.size(); ++head) { int b = q[head]; if (!alive[b]) continue; int a = pre[b], c = nxt[b]; if (a < 0 || c < 0 || !alive[a] || !alive[c]) continue; int va = seq[a], vb = seq[b], vc = seq[c]; if (va == vc) { if (cnt[vb] <= 1) continue; int d = nxt[c]; nxt[a] = d; if (d != -1) pre[d] = a; alive[b] = alive[c] = 0; --cnt[vb]; --cnt[va]; q.push_back(a); if (d != -1) q.push_back(d); } else { if (cnt[vb] <= 1 || !hasEdge(va, vc)) continue; nxt[a] = c; pre[c] = a; alive[b] = 0; --cnt[vb]; q.push_back(a); q.push_back(c); } } vector<int> out; out.reserve(L); int node = 0; while (nxt[node] != -1) { node = nxt[node]; out.push_back(seq[node]); } return out; } void consider(const vector<int> &base) { int L = base.size(); if (L > K) return; vector<int> T(N + 1, -1); T[1] = 0; int seen = 1; ll baseV = 0; for (int i = 0; i < L; ++i) { int v = base[i]; if (T[v] == -1) { T[v] = i + 1; baseV += r[v] * (i + 1LL); ++seen; } } if (seen != N) return; int D = K - L; auto update = [&](ll value, int kind, int plen, int x, int y) { if (value > best.value) { best.value = value; best.base = base; best.prefixKind = kind; best.prefixLen = plen; best.x = x; best.y = y; } }; int p = D - (D & 1); if (p == 0) { update(baseV, 0, 0, -1, -1); } else { ll constant = baseV + (ll)p * (totalR - r[1]); ll localBest = NEG; int localA = -1; for (int a : adj[1]) { ll value = constant + r[a] * (1LL - p - T[a]); if (value > localBest) { localBest = value; localA = a; } } if (localA != -1) update(localBest, 0, p, localA, -1); } if ((D & 1) && D >= 3 && !triangles.empty()) { ll constant = baseV + (ll)D * (totalR - r[1]); ll localBest = NEG; int localA = -1, localB = -1; for (auto [a, b] : triangles) { ll value = constant + r[a] * (1LL - D - T[a]) + r[b] * (-1LL - T[b]); if (value > localBest) { localBest = value; localA = a; localB = b; } } if (localA != -1) update(localBest, 1, D, localA, localB); } } void considerWithShortcuts(const vector<int> &base) { consider(base); vector<int> shorter = shortcut(base); if (shorter.size() != base.size()) consider(shorter); } vector<int> fallbackDFS() { vector<int> base; base.reserve(2 * N); vector<char> vis(N + 1, 0); vector<int> it(N + 1, 0), st = {1}; vis[1] = 1; while (!st.empty()) { int v = st.back(); while (it[v] < (int)adj[v].size() && vis[adj[v][it[v]]]) ++it[v]; if (it[v] == (int)adj[v].size()) { st.pop_back(); if (!st.empty()) base.push_back(st.back()); } else { int u = adj[v][it[v]++]; vis[u] = 1; base.push_back(u); st.push_back(u); } } return base; } void run() { cin >> N >> M >> K; r.assign(N + 1, 0); for (int i = 1; i <= N; ++i) { cin >> r[i]; totalR += r[i]; } adj.assign(N + 1, {}); eu.resize(M); ev.resize(M); edges.reserve(2 * M + 1); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; eu[i] = u; ev[i] = v; adj[u].push_back(v); adj[v].push_back(u); edges.insert(edgeKey(u, v)); } if (N == 1) return; adjSorted = adj; for (int v = 1; v <= N; ++v) { sort(adjSorted[v].begin(), adjSorted[v].end(), [&](int a, int b) { if (r[a] != r[b]) return r[a] < r[b]; return a < b; }); } vector<char> rootNeighbor(N + 1, 0); int minRoot = adj[1][0]; for (int a : adj[1]) { rootNeighbor[a] = 1; if (r[a] < r[minRoot] || (r[a] == r[minRoot] && a < minRoot)) minRoot = a; } for (int i = 0; i < M; ++i) { int u = eu[i], v = ev[i]; if (u != 1 && v != 1 && rootNeighbor[u] && rootNeighbor[v]) { triangles.push_back({u, v}); triangles.push_back({v, u}); } } vector<int> fallback = fallbackDFS(); considerWithShortcuts(fallback); for (int type = 0; type < 2; ++type) { for (int mode = 0; mode < 3; ++mode) { Tree tree = buildTree(type == 1, mode); for (int kind = 0; kind < 2; ++kind) { vector<ll> w = r; w[1] = 0; ll lambda = 0; if (kind == 0) { w[minRoot] = 0; lambda = totalR - r[1] - r[minRoot]; } DPResult dp = runDP(tree, w, lambda); for (int par = 0; par < 2; ++par) { if (dp.val[1][par] == NEG) continue; vector<int> base = buildRoute(tree, dp, par); considerWithShortcuts(base); } } } } if (best.value < 0) { best.base = fallback; best.prefixKind = 0; best.prefixLen = 0; best.x = best.y = -1; } vector<int> answer; answer.reserve(K); int cur = 1; auto push = [&](int v) { answer.push_back(v); cur = v; }; if (best.prefixKind == 1) { int E = best.prefixLen - 3; for (int i = 0; i < E; ++i) push((i & 1) ? 1 : best.x); push(best.x); push(best.y); push(1); } else { for (int i = 0; i < best.prefixLen; ++i) push((i & 1) ? 1 : best.x); } for (int v : best.base) push(v); while ((int)answer.size() < K) { int nxt = adj[cur][0]; push(nxt); } for (int i = 0; i < K; ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solver solver; solver.run(); return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 304 行
#include <bits/stdc++.h> using namespace std; using ll = long long; static uint64_t mix64(uint64_t x) { x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } struct Tree { vector<vector<int>> ch; vector<int> parent, depth, order; }; Tree buildDFS(const vector<vector<int>>& adj) { int n = adj.size(); Tree t; t.ch.assign(n, {}); t.parent.assign(n, -1); t.depth.assign(n, 0); t.order.clear(); t.order.reserve(n); vector<int> it(n, 0), st; st.reserve(n); st.push_back(0); t.parent[0] = 0; t.order.push_back(0); while (!st.empty()) { int u = st.back(); if (it[u] == (int)adj[u].size()) { st.pop_back(); continue; } int v = adj[u][it[u]++]; if (t.parent[v] != -1) continue; t.parent[v] = u; t.depth[v] = t.depth[u] + 1; t.ch[u].push_back(v); t.order.push_back(v); st.push_back(v); } return t; } Tree buildBFS(const vector<vector<int>>& adj) { int n = adj.size(); Tree t; t.ch.assign(n, {}); t.parent.assign(n, -1); t.depth.assign(n, 0); t.order.clear(); t.order.reserve(n); queue<int> q; q.push(0); t.parent[0] = 0; t.order.push_back(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (t.parent[v] != -1) continue; t.parent[v] = u; t.depth[v] = t.depth[u] + 1; t.ch[u].push_back(v); t.order.push_back(v); q.push(v); } } return t; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M, K; cin >> N >> M >> K; vector<ll> rate(N); ll totalRate = 0; for (int i = 0; i < N; ++i) { cin >> rate[i]; totalRate += rate[i]; } 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 (N == 1) { cout << '\n'; return 0; } vector<vector<int>> lowAdj = adj; for (auto& a : lowAdj) { sort(a.begin(), a.end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] < rate[y]; return x < y; }); } vector<vector<int>> highAdj = adj; for (auto& a : highAdj) { sort(a.begin(), a.end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] > rate[y]; return x < y; }); } vector<vector<int>> randomAdj1 = adj; for (auto& a : randomAdj1) { sort(a.begin(), a.end(), [&](int x, int y) { uint64_t hx = mix64((uint64_t)x + 1234567ULL); uint64_t hy = mix64((uint64_t)y + 1234567ULL); if (hx != hy) return hx < hy; return x < y; }); } vector<vector<int>> randomAdj2 = adj; for (auto& a : randomAdj2) { sort(a.begin(), a.end(), [&](int x, int y) { uint64_t hx = mix64((uint64_t)x + 987654321ULL); uint64_t hy = mix64((uint64_t)y + 987654321ULL); if (hx != hy) return hx < hy; return x < y; }); } vector<Tree> trees; trees.reserve(7); trees.push_back(buildDFS(adj)); trees.push_back(buildDFS(lowAdj)); trees.push_back(buildDFS(highAdj)); trees.push_back(buildDFS(randomAdj1)); trees.push_back(buildDFS(randomAdj2)); trees.push_back(buildBFS(adj)); trees.push_back(buildBFS(lowAdj)); ll bestScore = -1; vector<int> bestBase; int bestDelay = 0; for (int ti = 0; ti < (int)trees.size(); ++ti) { Tree& t = trees[ti]; vector<ll> subWeight(N, 0); vector<int> subSize(N, 1), subHeight(N, 0), subMaxRate(N, 0); for (int i = 0; i < N; ++i) { subWeight[i] = rate[i]; subMaxRate[i] = (int)min<ll>(rate[i], INT_MAX); } for (int oi = N - 1; oi >= 1; --oi) { int u = t.order[oi]; int p = t.parent[u]; subWeight[p] += subWeight[u]; subSize[p] += subSize[u]; subHeight[p] = max(subHeight[p], subHeight[u] + 1); subMaxRate[p] = max(subMaxRate[p], subMaxRate[u]); } auto densityLess = [&](int a, int b) { __int128 lhs = (__int128)subWeight[a] * subSize[b]; __int128 rhs = (__int128)subWeight[b] * subSize[a]; if (lhs != rhs) return lhs < rhs; if (subWeight[a] != subWeight[b]) return subWeight[a] < subWeight[b]; return a < b; }; for (int mode = 0; mode < 7; ++mode) { for (int u = 0; u < N; ++u) { auto& c = t.ch[u]; if (c.size() < 2) continue; if (mode == 0) { sort(c.begin(), c.end(), densityLess); } else if (mode == 1) { sort(c.begin(), c.end(), [&](int a, int b) { if (subWeight[a] != subWeight[b]) return subWeight[a] < subWeight[b]; return densityLess(a, b); }); } else if (mode == 2) { sort(c.begin(), c.end(), [&](int a, int b) { if (subMaxRate[a] != subMaxRate[b]) return subMaxRate[a] < subMaxRate[b]; return densityLess(a, b); }); } else if (mode == 3) { sort(c.begin(), c.end(), [&](int a, int b) { if (subSize[a] != subSize[b]) return subSize[a] < subSize[b]; return densityLess(a, b); }); } else if (mode == 4) { sort(c.begin(), c.end(), [&](int a, int b) { if (rate[a] != rate[b]) return rate[a] < rate[b]; return densityLess(a, b); }); } else if (mode == 5) { sort(c.begin(), c.end(), [&](int a, int b) { if (subHeight[a] != subHeight[b]) return subHeight[a] < subHeight[b]; return densityLess(a, b); }); } else { uint64_t seed = 0x123456789abcdefULL + 1000003ULL * (ti + 1); sort(c.begin(), c.end(), [&](int a, int b) { uint64_t ha = mix64((uint64_t)a + seed); uint64_t hb = mix64((uint64_t)b + seed); if (ha != hb) return ha < hb; return a < b; }); } } vector<int> base; base.reserve(2 * N); vector<pair<int, int>> st; st.reserve(N); st.push_back({0, 0}); int seen = 1; ll baseValue = 0; while (!st.empty() && seen < N) { int u = st.back().first; int& idx = st.back().second; if (idx == (int)t.ch[u].size()) { st.pop_back(); if (!st.empty()) base.push_back(st.back().first); continue; } int v = t.ch[u][idx++]; base.push_back(v); ++seen; baseValue += rate[v] * (ll)base.size(); if (seen == N) break; st.push_back({v, 0}); } int L = (int)base.size(); int extra = K - L; int delay = extra - (extra & 1); int first = base[0]; ll score = baseValue + (ll)delay * (totalRate - rate[0] - rate[first]); if (score > bestScore) { bestScore = score; bestBase = move(base); bestDelay = delay; } } } vector<int> answer; answer.reserve(K); int first = bestBase[0]; answer.push_back(first); for (int i = 0; i < bestDelay / 2; ++i) { answer.push_back(0); answer.push_back(first); } for (int i = 1; i < (int)bestBase.size(); ++i) answer.push_back(bestBase[i]); if ((int)answer.size() < K) { int cur = answer.back(); answer.push_back(adj[cur][0]); } for (int i = 0; i < K; ++i) { if (i) cout << ' '; cout << answer[i] + 1; } cout << '\n'; return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 281 行
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, M, K; if(!(cin>>N>>M>>K)) return 0; vector<int> r(N+1); ll totalR = 0; for(int i=1;i<=N;i++){ cin>>r[i]; totalR += r[i]; } vector<vector<int>> adj(N+1); for(int i=0;i<M;i++){ int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } if(N==1){ for(int i=0;i<K;i++){ if(i) cout << ' '; cout << 1; } cout << '\n'; return 0; } // v0: neighbor of 1 with minimum r int v0 = -1; int minr = INT_MAX; for(int v: adj[1]){ if(r[v] < minr){ minr = r[v]; v0 = v; } } // sort adjacency by degree ascending to encourage long DFS path for(int i=1;i<=N;i++){ sort(adj[i].begin(), adj[i].end(), [&](int a, int b){ if(adj[a].size() != adj[b].size()) return adj[a].size() < adj[b].size(); return a < b; }); } vector<int> parent(N+1, 0), depth(N+1, 0); vector<vector<int>> children(N+1); vector<int> discovery; discovery.reserve(N); vector<int> iter(N+1, 0); vector<int> st; st.reserve(N); st.push_back(1); parent[1] = -1; discovery.push_back(1); while(!st.empty()){ int u = st.back(); if(iter[u] < (int)adj[u].size()){ int v = adj[u][iter[u]++]; if(parent[v] == 0){ parent[v] = u; depth[v] = depth[u] + 1; children[u].push_back(v); discovery.push_back(v); st.push_back(v); } } else { st.pop_back(); } } vector<int> sz(N+1, 1); vector<ll> sumR(N+1, 0); vector<ll> pathSumR(N+1, 0); pathSumR[1] = r[1]; for(int u : discovery){ if(u != 1){ pathSumR[u] = pathSumR[parent[u]] + r[u]; } } // bottom-up compute sz and sumR for(int i = (int)discovery.size() - 1; i >= 0; --i){ int u = discovery[i]; sz[u] = 1; sumR[u] = r[u]; for(int v : children[u]){ sz[u] += sz[v]; sumR[u] += sumR[v]; } } // sort children by average rate ascending for(int u=1; u<=N; ++u){ sort(children[u].begin(), children[u].end(), [&](int a, int b){ ll lhs = sumR[a] * sz[b]; ll rhs = sumR[b] * sz[a]; if(lhs != rhs) return lhs < rhs; return a < b; }); } // candidate selection struct Cand { ll score; int v; }; vector<Cand> cands; cands.reserve(N); for(int i=1; i<=N; ++i){ ll score = (ll)depth[i] * totalR + pathSumR[i]; cands.push_back({score, i}); } int CAND = min(N, 2000); if(CAND < N){ nth_element(cands.begin(), cands.begin() + CAND, cands.end(), [](const Cand& a, const Cand& b){ return a.score > b.score; }); cands.resize(CAND); } // arrays for path marking and simulation vector<int> nextVal(N+1, 0), nextStamp(N+1, 0); vector<int> visited(N+1, 0); int stamp = 0; int bestX = 1; ll bestV = -1; // stack arrays for simulation vector<int> stackU(N+1), stackIdx(N+1); auto evaluate = [&](int x, int candId) -> ll { // mark path from x to root int cur = x; while(cur != 1){ int p = parent[cur]; nextVal[p] = cur; nextStamp[p] = candId; cur = p; } int C = 2*(N-1) - depth[x]; int P_use = K - C; if(P_use < 0) P_use = 0; if(P_use % 2 != 0) P_use--; if(P_use < 0) P_use = 0; stamp++; ll V = 0; visited[1] = stamp; if(P_use > 0){ visited[v0] = stamp; V += (ll)r[v0] * 1; } int step = 0; int top = 0; stackU[0] = 1; stackIdx[0] = 0; while(top >= 0){ int u = stackU[top]; int pc = (nextStamp[u] == candId) ? nextVal[u] : 0; int &idx = stackIdx[top]; int c = -1; while(idx < (int)children[u].size()){ int v = children[u][idx++]; if(v == pc) continue; c = v; break; } if(c != -1){ step++; int time = P_use + step; if(visited[c] != stamp){ visited[c] = stamp; V += (ll)r[c] * time; } ++top; stackU[top] = c; stackIdx[top] = 0; continue; } if(pc != 0){ step++; int time = P_use + step; if(visited[pc] != stamp){ visited[pc] = stamp; V += (ll)r[pc] * time; } ++top; stackU[top] = pc; stackIdx[top] = 0; continue; } if(u == x){ break; } step++; --top; } return V; }; for(int i=0; i<(int)cands.size(); ++i){ int x = cands[i].v; ll V = evaluate(x, i+1); if(V > bestV){ bestV = V; bestX = x; } } // generate final walk for bestX int finalCandId = CAND + 1; { int cur = bestX; while(cur != 1){ int p = parent[cur]; nextVal[p] = cur; nextStamp[p] = finalCandId; cur = p; } } int C = 2*(N-1) - depth[bestX]; int P_use = K - C; if(P_use < 0) P_use = 0; if(P_use % 2 != 0) P_use--; if(P_use < 0) P_use = 0; vector<int> walk; walk.reserve(K); // initial oscillation if(P_use > 0){ for(int i=0; i<P_use/2; ++i){ walk.push_back(v0); walk.push_back(1); } } // generate cover int top = 0; stackU[0] = 1; stackIdx[0] = 0; while(top >= 0){ int u = stackU[top]; int pc = (nextStamp[u] == finalCandId) ? nextVal[u] : 0; int &idx = stackIdx[top]; int c = -1; while(idx < (int)children[u].size()){ int v = children[u][idx++]; if(v == pc) continue; c = v; break; } if(c != -1){ walk.push_back(c); ++top; stackU[top] = c; stackIdx[top] = 0; continue; } if(pc != 0){ walk.push_back(pc); ++top; stackU[top] = pc; stackIdx[top] = 0; continue; } if(u == bestX){ break; } int p = parent[u]; walk.push_back(p); --top; } // post waste int current = bestX; int post = K - (int)walk.size(); if(post > 0){ int d = adj[current][0]; for(int i=0; i<post; ++i){ if(i % 2 == 0) walk.push_back(d); else walk.push_back(current); } } // output for(int i=0; i<K; ++i){ if(i) cout << ' '; cout << walk[i]; } cout << '\n'; return 0; }
MiniMax M3.1 Flash · 采样 1 · 825 行
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; struct DSU { vector<int> p, sz; explicit DSU(int n) : p(n), sz(n, 1) { iota(p.begin(), p.end(), 0); } int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (sz[a] < sz[b]) swap(a, b); p[b] = a; sz[a] += sz[b]; return true; } }; struct Tree { vector<int> parent, depth, order, sz; vector<int> deepLeaf, hotLeaf; vector<int> start, child; vector<ll> weight; }; class Solver { enum { RANDOM_DFS, RATE_DFS, DEGREE_DFS, WARN_DFS, WARN_RATE_DFS, RATE_NOISE_DFS, MAX_DEGREE_DFS }; int n, K, firstVertex; vector<ll> rate; vector<vector<int>> adj; vector<pair<int, int>> edges; vector<int> orderRate, orderDegree, rootEdge; unordered_set<ull> edgeSet; mt19937 rng; struct RouteFrame { int u, idx, phase; bool specialDone, finalDone, noReturn; }; struct SearchFrame { int u, idx; vector<int> order; }; ull edgeKey(int u, int v) const { if (u > v) swap(u, v); return (ull)u * (ull)n + (ull)v; } bool hasEdge(int u, int v) const { return edgeSet.find(edgeKey(u, v)) != edgeSet.end(); } void finalizeTree(Tree &t, vector<vector<int>> &children) const { int m = (int)t.order.size(); t.sz.assign(n, 1); t.weight.assign(n, 0); t.deepLeaf.assign(n, -1); t.hotLeaf.assign(n, -1); for (int i = m - 1; i >= 0; --i) { int u = t.order[i]; t.sz[u] = 1; t.weight[u] = (u == firstVertex ? 0 : rate[u]); if (children[u].empty()) { t.deepLeaf[u] = u; t.hotLeaf[u] = u; } else { for (int v : children[u]) { t.sz[u] += t.sz[v]; t.weight[u] += t.weight[v]; if (t.deepLeaf[v] != -1 && (t.deepLeaf[u] == -1 || t.depth[t.deepLeaf[v]] > t.depth[t.deepLeaf[u]])) { t.deepLeaf[u] = t.deepLeaf[v]; } if (t.hotLeaf[v] != -1 && (t.hotLeaf[u] == -1 || rate[t.hotLeaf[v]] > rate[t.hotLeaf[u]])) { t.hotLeaf[u] = t.hotLeaf[v]; } } } } for (int u = 0; u < n; ++u) { sort(children[u].begin(), children[u].end(), [&](int x, int y) { ll lhs = t.weight[x] * (ll)t.sz[y]; ll rhs = t.weight[y] * (ll)t.sz[x]; if (lhs != rhs) return lhs < rhs; if (rate[x] != rate[y]) return rate[x] < rate[y]; return x < y; }); } t.start.assign(n + 1, 0); t.child.clear(); t.child.reserve(max(0, n - 1)); for (int u = 0; u < n; ++u) { t.start[u] = (int)t.child.size(); for (int v : children[u]) t.child.push_back(v); } t.start[n] = (int)t.child.size(); } Tree buildDFS(int mode) { Tree t; t.parent.assign(n, -1); t.depth.assign(n, 0); t.order.clear(); t.order.reserve(n); vector<vector<int>> children(n); vector<vector<int>> candidates(n); vector<int> candidatePos(n, 0); vector<int> randomRank(n); iota(randomRank.begin(), randomRank.end(), 0); shuffle(randomRank.begin(), randomRank.end(), rng); if (mode == RANDOM_DFS) { for (int u = 0; u < n; ++u) shuffle(adj[u].begin(), adj[u].end(), rng); } vector<int> remainingDegree(n); for (int u = 0; u < n; ++u) remainingDegree[u] = (int)adj[u].size(); auto enterVertex = [&](int u) { for (int v : adj[u]) --remainingDegree[v]; }; auto prepareCandidates = [&](int u) { vector<int> v; if (mode == RANDOM_DFS || mode == RATE_DFS || mode == DEGREE_DFS) { const vector<int> &src = mode == RATE_DFS ? orderRate[u] : mode == DEGREE_DFS ? orderDegree[u] : adj[u]; for (int x : src) { if (t.parent[x] == -1) v.push_back(x); } } else { for (int x : adj[u]) { if (t.parent[x] == -1) v.push_back(x); } if (mode == WARN_DFS || mode == WARN_RATE_DFS) { sort(v.begin(), v.end(), [&](int x, int y) { if (remainingDegree[x] != remainingDegree[y]) { return remainingDegree[x] < remainingDegree[y]; } if (mode == WARN_RATE_DFS && rate[x] != rate[y]) { return rate[x] < rate[y]; } return randomRank[x] < randomRank[y]; }); } else if (mode == RATE_NOISE_DFS) { sort(v.begin(), v.end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] < rate[y]; return randomRank[x] < randomRank[y]; }); } else if (mode == MAX_DEGREE_DFS) { sort(v.begin(), v.end(), [&](int x, int y) { if (adj[x].size() != adj[y].size()) { return adj[x].size() > adj[y].size(); } return randomRank[x] < randomRank[y]; }); } } if (u == 0) { auto it = find(v.begin(), v.end(), firstVertex); if (it != v.end()) { v.erase(it); v.insert(v.begin(), firstVertex); } } candidates[u] = move(v); }; vector<int> st; t.parent[0] = 0; t.order.push_back(0); enterVertex(0); prepareCandidates(0); st.push_back(0); while (!st.empty()) { int u = st.back(); bool descended = false; while (candidatePos[u] < (int)candidates[u].size()) { int v = candidates[u][candidatePos[u]++]; if (t.parent[v] != -1) continue; t.parent[v] = u; t.depth[v] = t.depth[u] + 1; children[u].push_back(v); t.order.push_back(v); enterVertex(v); prepareCandidates(v); st.push_back(v); descended = true; break; } if (!descended) st.pop_back(); } finalizeTree(t, children); return t; } Tree buildKruskalTree(int rootEdgeCount) { DSU dsu(n); vector<pair<int, int>> selected; selected.reserve(n - 1); vector<int> rootNeighbors; for (int v = 0; v < n; ++v) { if (rootEdge[v] != -1) rootNeighbors.push_back(v); } shuffle(rootNeighbors.begin(), rootNeighbors.end(), rng); int chosen = 0; auto includeRootEdge = [&](int v) { if (v == 0 || rootEdge[v] == -1) return; if (dsu.unite(0, v)) { selected.push_back({0, v}); ++chosen; } }; includeRootEdge(firstVertex); for (int v : rootNeighbors) { if (chosen >= rootEdgeCount) break; includeRootEdge(v); } vector<int> edgeOrder(edges.size()); iota(edgeOrder.begin(), edgeOrder.end(), 0); shuffle(edgeOrder.begin(), edgeOrder.end(), rng); for (int id : edgeOrder) { if ((int)selected.size() == n - 1) break; auto [u, v] = edges[id]; if (dsu.unite(u, v)) selected.push_back({u, v}); } vector<vector<int>> treeAdj(n); for (auto [u, v] : selected) { treeAdj[u].push_back(v); treeAdj[v].push_back(u); } Tree t; t.parent.assign(n, -1); t.depth.assign(n, 0); t.order.reserve(n); vector<vector<int>> children(n); vector<int> st{0}; t.parent[0] = 0; t.order.push_back(0); while (!st.empty()) { int u = st.back(); st.pop_back(); for (int v : treeAdj[u]) { if (t.parent[v] != -1) continue; t.parent[v] = u; t.depth[v] = t.depth[u] + 1; children[u].push_back(v); t.order.push_back(v); st.push_back(v); } } finalizeTree(t, children); return t; } Tree buildPathTree(const vector<int> &path) const { Tree t; t.parent.assign(n, -1); t.depth.assign(n, 0); t.order = path; vector<vector<int>> children(n); for (int i = 1; i < (int)path.size(); ++i) { t.parent[path[i]] = path[i - 1]; t.depth[path[i]] = t.depth[path[i - 1]] + 1; children[path[i - 1]].push_back(path[i]); } t.parent[0] = 0; finalizeTree(t, children); return t; } bool makeRoute(const Tree &t, int endpoint, bool forceFirst, vector<int> &route) const { vector<int> finalChild(n, -1); int x = endpoint; while (t.parent[x] != 0) { finalChild[t.parent[x]] = x; x = t.parent[x]; } int rootDegree = t.start[1] - t.start[0]; bool firstIsRootChild = false; for (int i = t.start[0]; i < t.start[1]; ++i) { if (t.child[i] == firstVertex) firstIsRootChild = true; } if (forceFirst) { if (!firstIsRootChild) return false; if (finalChild[0] == firstVertex && rootDegree > 1) return false; } route.clear(); route.reserve(2 * n); vector<int> firstTime(n, -1); route.push_back(0); firstTime[0] = 0; vector<RouteFrame> st; st.reserve(n); st.push_back({0, 0, 0, false, false, true}); while (!st.empty()) { RouteFrame &f = st.back(); int u = f.u; int chosen = -1; if (u == 0 && forceFirst && !f.specialDone) { f.specialDone = true; chosen = firstVertex; if (finalChild[u] == chosen) f.finalDone = true; } else if (f.phase == 0) { while (f.idx < t.start[u + 1] - t.start[u]) { int v = t.child[t.start[u] + f.idx++]; if (forceFirst && u == 0 && v == firstVertex) continue; if (v == finalChild[u]) continue; chosen = v; break; } if (chosen == -1) f.phase = 1; } else if (f.phase == 1) { int v = finalChild[u]; if (v != -1 && !f.finalDone) { f.finalDone = true; chosen = v; } f.phase = 2; } if (chosen != -1) { bool noReturn = (finalChild[u] == chosen); route.push_back(chosen); if (firstTime[chosen] == -1) { firstTime[chosen] = (int)route.size() - 1; } st.push_back({chosen, 0, 0, false, false, noReturn}); } else { bool noReturn = f.noReturn; int par = t.parent[u]; st.pop_back(); if (!noReturn && par != u) route.push_back(par); } } return true; } vector<int> endpointCandidates(const Tree &t, int topCount, int randomCount) const { vector<int> leaves; for (int u = 0; u < n; ++u) { if (t.start[u] == t.start[u + 1]) leaves.push_back(u); } vector<char> used(n, false); vector<int> result; auto add = [&](int v) { if (v >= 0 && v < n && !used[v]) { used[v] = true; result.push_back(v); } }; if ((int)leaves.size() <= 40) { for (int v : leaves) add(v); } else { vector<int> tmp = leaves; nth_element(tmp.begin(), tmp.begin() + topCount, tmp.end(), [&](int x, int y) { if (t.depth[x] != t.depth[y]) { return t.depth[x] > t.depth[y]; } return rate[x] > rate[y]; }); for (int i = 0; i < topCount; ++i) add(tmp[i]); tmp = leaves; nth_element(tmp.begin(), tmp.begin() + topCount, tmp.end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] > rate[y]; return t.depth[x] > t.depth[y]; }); for (int i = 0; i < topCount; ++i) add(tmp[i]); tmp = leaves; nth_element(tmp.begin(), tmp.begin() + topCount, tmp.end(), [&](int x, int y) { ll sx = rate[x] * t.depth[x]; ll sy = rate[y] * t.depth[y]; if (sx != sy) return sx > sy; return t.depth[x] > t.depth[y]; }); for (int i = 0; i < topCount; ++i) add(tmp[i]); for (int i = 0; i < randomCount; ++i) { add(leaves[rng() % leaves.size()]); } } int deepest = -1, hottest = -1, bestCombined = -1; for (int u = 0; u < n; ++u) { if (deepest == -1 || t.depth[u] > t.depth[deepest]) deepest = u; if (hottest == -1 || rate[u] > rate[hottest]) hottest = u; ll cu = rate[u] * t.depth[u]; ll cb = bestCombined == -1 ? -1 : rate[bestCombined] * t.depth[bestCombined]; if (cu > cb) bestCombined = u; } add(deepest); add(hottest); add(bestCombined); add(0); int branchCount = min(12, t.start[1] - t.start[0]); for (int i = 0; i < branchCount; ++i) { int c = t.child[t.start[0] + i]; add(t.deepLeaf[c]); add(t.hotLeaf[c]); } return result; } void improvePath(vector<int> &path) const { if (path.size() < 3) return; vector<ll> prefW(path.size() + 1, 0), prefQ(path.size() + 1, 0); auto rebuild = [&]() { prefW[0] = prefQ[0] = 0; for (int i = 0; i < (int)path.size(); ++i) { ll w = (path[i] == firstVertex ? 0 : rate[path[i]]); prefW[i + 1] = prefW[i] + w; prefQ[i + 1] = prefQ[i] + w * i; } }; rebuild(); for (int iteration = 0; iteration < 120; ++iteration) { int k = (int)path.size() - 1; ll bestDelta = 0; int bestCut = -1; for (int i = 0; i + 1 < k; ++i) { if (!hasEdge(path[i], path[k])) continue; int l = i + 1; ll delta = (ll)(l + k) * (prefW[k + 1] - prefW[l]) - 2LL * (prefQ[k + 1] - prefQ[l]); if (delta > bestDelta) { bestDelta = delta; bestCut = i; } } if (bestCut == -1) break; reverse(path.begin() + bestCut + 1, path.end()); rebuild(); } } bool findHamiltonianPath(int mode, vector<int> &path) const { vector<int> randomRank(n); iota(randomRank.begin(), randomRank.end(), 0); shuffle(randomRank.begin(), randomRank.end(), rng); vector<int> remainingDegree(n); for (int u = 0; u < n; ++u) remainingDegree[u] = (int)adj[u].size(); vector<char> used(n, false); path.clear(); path.reserve(n); auto makeOrder = [&](int u) { vector<int> v = adj[u]; if (mode == 0) { sort(v.begin(), v.end(), [&](int x, int y) { if (remainingDegree[x] != remainingDegree[y]) { return remainingDegree[x] < remainingDegree[y]; } if (rate[x] != rate[y]) return rate[x] < rate[y]; return randomRank[x] < randomRank[y]; }); } else if (mode == 1) { sort(v.begin(), v.end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] < rate[y]; if (remainingDegree[x] != remainingDegree[y]) { return remainingDegree[x] < remainingDegree[y]; } return randomRank[x] < randomRank[y]; }); } else if (mode == 2) { sort(v.begin(), v.end(), [&](int x, int y) { if (remainingDegree[x] != remainingDegree[y]) { return remainingDegree[x] < remainingDegree[y]; } return randomRank[x] < randomRank[y]; }); } else { shuffle(v.begin(), v.end(), rng); } return v; }; vector<SearchFrame> st; st.reserve(n); path.push_back(0); used[0] = true; for (int v : adj[0]) --remainingDegree[v]; st.push_back({0, 0, makeOrder(0)}); int budget = max(100000, 30 * n); int pushes = 0; while (!st.empty()) { SearchFrame &f = st.back(); bool descended = false; while (f.idx < (int)f.order.size()) { int v = f.order[f.idx++]; if (used[v]) continue; if (++pushes > budget) return false; used[v] = true; for (int x : adj[v]) --remainingDegree[x]; path.push_back(v); st.push_back({v, 0, makeOrder(v)}); descended = true; if ((int)path.size() == n) return true; break; } if (!descended) { int u = f.u; st.pop_back(); if (!path.empty()) path.pop_back(); used[u] = false; for (int x : adj[u]) ++remainingDegree[x]; } } return false; } public: Solver(int n_, int K_, vector<ll> rate_, vector<vector<int>> adj_, vector<pair<int, int>> edges_) : n(n_), K(K_), rate(move(rate_)), adj(move(adj_)), edges(move(edges_)), rng(712367821) { orderRate.resize(n); orderDegree.resize(n); rootEdge.assign(n, -1); for (int u = 0; u < n; ++u) { orderRate[u] = adj[u]; orderDegree[u] = adj[u]; } for (int u = 0; u < n; ++u) { sort(orderRate[u].begin(), orderRate[u].end(), [&](int x, int y) { if (rate[x] != rate[y]) return rate[x] < rate[y]; return x < y; }); sort(orderDegree[u].begin(), orderDegree[u].end(), [&](int x, int y) { if (adj[x].size() != adj[y].size()) { return adj[x].size() < adj[y].size(); } return x < y; }); } edgeSet.reserve(edges.size() * 2 + 1); edgeSet.max_load_factor(0.7f); for (auto [u, v] : edges) { edgeSet.insert(edgeKey(u, v)); if (u == 0 && rootEdge[v] == -1) rootEdge[v] = (int)edges.size(); if (v == 0 && rootEdge[u] == -1) rootEdge[u] = (int)edges.size(); } int best = 0; for (int v : adj[0]) { if (rate[v] < rate[best] || (rate[v] == rate[best] && v < best)) { best = v; } } firstVertex = best; } vector<int> solve() { if (n == 1) return {}; ll totalRate = accumulate(rate.begin(), rate.end(), 0LL); ll delayedRate = totalRate - rate[0] - rate[firstVertex]; ll bestScore = -1; vector<int> bestRoute; int bestPrefix = 0; Tree bestTree; auto evaluateTree = [&](const Tree &tree, const vector<int> &endpoints) { for (int endpoint : endpoints) { int length = 2 * (n - 1) - tree.depth[endpoint]; if (length > K) continue; int slack = K - length; int prefix = slack - (slack & 1); bool forceFirst = (prefix == 0); vector<int> route; if (!makeRoute(tree, endpoint, forceFirst, route)) continue; if ((int)route.size() - 1 != length) continue; vector<int> firstTime(n, -1); firstTime[0] = 0; for (int i = 1; i < (int)route.size(); ++i) { if (firstTime[route[i]] == -1) firstTime[route[i]] = i; } ll baseScore = 0; for (int u = 0; u < n; ++u) { if (firstTime[u] != -1) { baseScore += rate[u] * firstTime[u]; } } ll score = baseScore - rate[firstVertex] * firstTime[firstVertex] + (ll)prefix * delayedRate + rate[firstVertex]; if (score > bestScore) { bestScore = score; bestRoute = route; bestPrefix = prefix; bestTree = tree; } } }; for (int attempt = 0; attempt < 8; ++attempt) { vector<int> path; if (!findHamiltonianPath(attempt % 4, path)) continue; improvePath(path); Tree tree = buildPathTree(path); evaluateTree(tree, {path.back()}); } vector<int> modes = { RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, RANDOM_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_DFS, WARN_RATE_DFS, WARN_RATE_DFS, WARN_RATE_DFS, WARN_RATE_DFS, RATE_DFS, RATE_DFS, RATE_NOISE_DFS, RATE_NOISE_DFS, DEGREE_DFS, MAX_DEGREE_DFS }; for (int mode : modes) { Tree tree = buildDFS(mode); vector<int> endpoints = endpointCandidates(tree, 4, 6); evaluateTree(tree, endpoints); } for (int rootEdges : {1, 3, 8, 16}) { for (int rep = 0; rep < (rootEdges <= 3 ? 2 : 1); ++rep) { Tree tree = buildKruskalTree(rootEdges); vector<int> endpoints = endpointCandidates(tree, 4, 6); evaluateTree(tree, endpoints); } } if (bestScore >= 0) { vector<int> endpoints = endpointCandidates(bestTree, 30, 60); evaluateTree(bestTree, endpoints); } vector<int> answer; answer.reserve(K); for (int t = 1; t <= bestPrefix; ++t) { answer.push_back((t & 1) ? firstVertex : 0); } for (int i = 1; i < (int)bestRoute.size(); ++i) { answer.push_back(bestRoute[i]); } while ((int)answer.size() < K) { if (bestRoute.back() != 0) { answer.push_back(bestTree.parent[bestRoute.back()]); } else { answer.push_back(firstVertex); } } if ((int)answer.size() > K) answer.resize(K); auto valid = [&]() { if ((int)answer.size() != K) return false; vector<char> seen(n, false); int cur = 0; seen[0] = true; for (int v : answer) { if (!hasEdge(cur, v)) return false; cur = v; seen[v] = true; } for (char x : seen) { if (!x) return false; } return true; }; if (!valid()) { Tree fallbackTree = buildDFS(RATE_DFS); vector<int> fallbackRoute; makeRoute(fallbackTree, 0, true, fallbackRoute); answer.clear(); for (int i = 1; i < (int)fallbackRoute.size(); ++i) { answer.push_back(fallbackRoute[i]); } while ((int)answer.size() < K) { int u = answer.back(); int v = answer[answer.size() - 2]; answer.push_back(v); (void)u; } } return answer; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M, K; cin >> N >> M >> K; vector<ll> rate(N); for (ll &x : rate) cin >> x; 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.emplace_back(u, v); } Solver solver(N, K, move(rate), move(adj), move(edges)); vector<int> answer = solver.solve(); for (int i = 0; i < (int)answer.size(); ++i) { if (i) cout << ' '; cout << answer[i] + 1; } cout << '\n'; return 0; }