题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie is given an undirected graph with $N$ ($1\le N\le 2 \cdot 10^5$) vertices labeled $1\dots N$ and $M$ edges ($N - 1\le M\le 2 \cdot 10^5$). Each edge is described by two integers $u, v$ ($1\le u, v \le N$) describing an undirected edge between nodes $u$ and $v$ and a lowercase Latin letter $c$ in the range a..z that is the value on the edge. The graph given is guaranteed to be connected. There may be multiple edges or self-loops.
Define $f(a, b)$ as the lexicographically smallest concatenation of edge values over all paths starting from node $a$ and ending at node $b$. A path may contain the same edge more than once (i.e., cycles are allowed).
For each $i$ ($1\le i \le N$), help Bessie determine the length of $f(1, i)$. Output this length if it is finite, otherwise output $-1$.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $T$ ($1\le T\le 10$), the number of independent tests. Each test is specified in the following format:The first line contains $N$ and $M$.
The next $M$ lines each contain two integers followed by a lowercase Latin character.
It is guaranteed that neither the sum of $N$ nor the sum of $M$ over all tests exceeds $4\cdot 10^5$.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output $N$ space-separated integers on a new line.SAMPLE INPUT:
2 1 0 2 2 1 1 a 2 1 b
SAMPLE OUTPUT:
0 0 -1
For the first test case, node 1 can be reached with an empty path, so the answer is 0. In the second test case, node 2 cannot have a lexicographically smallest path, since FJ can repeat the 'a' self-loop any number of times before moving to node 2, producing arbitrarily long strings that are still lexicographically minimal. Therefore, the answer for node 2 is -1.
SAMPLE INPUT:
2 7 7 1 2 a 1 3 a 2 4 b 3 5 a 5 6 a 6 7 a 7 4 a 4 3 1 2 z 2 3 x 3 4 y
SAMPLE OUTPUT:
0 1 1 5 2 3 4 0 1 2 -1For the first test case, node 1 has distance 0. Nodes 2 and 3 are adjacent with node 1, and they have distance 1. For nodes 4, 5, 6, and 7, it can be proven that the lexicographically shortest path does not pass through the edge between node 2 and 4.
For the second test case, node 4 again has no lexicographically smallest path, since the string can be extended indefinitely while remaining lexicographically minimal. Thus, its answer is -1.
SCORING:
- Inputs 3-4: Every character is a.
- Inputs 5-8: Every character is a or b.
- Inputs 9-14: $N,M\le 5000$
- Inputs 15-22: No additional constraints.
Problem credits: Daniel Zhu and Yash Belani
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 9.0k平均输出 token(含思考) ↓
- 20.5输出速度 tokens/s ↑
- 214 秒延迟 ↓
- 0.50×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 64.5k平均输出 token(含思考) ↓
- 182.0输出速度 tokens/s ↑
- 318 秒延迟 ↓
- 0.53×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 52.8k平均输出 token(含思考) ↓
- 62.3输出速度 tokens/s ↑
- 810 秒延迟 ↓
- 0.42×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N, M; cin >> N >> M; vector<vector<pair<int, int>>> adj(N + 1); for (int i = 0; i < M; i++) { int u, v; char c; cin >> u >> v >> c; int w = c - 'a'; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } vector<int> dist(N + 1, -1); dist[1] = 0; vector<int> cur = {1}; bool hasInfinite = false; int infiniteChar = -1; bool stopped = false; while (!cur.empty() && !stopped) { for (int c = 0; c < 26; c++) { vector<int> nxt; bool cycle = false; for (int u : cur) { for (auto [v, w] : adj[u]) { if (w != c) continue; if (dist[v] == -1) { dist[v] = dist[cur[0]] + 1; nxt.push_back(v); } else { cycle = true; } } } if (!nxt.empty()) { hasInfinite = cycle || (hasInfinite && infiniteChar == c); infiniteChar = hasInfinite ? c : -1; cur.swap(nxt); break; } if (cycle || (hasInfinite && infiniteChar == c)) { stopped = true; break; } if (c == 25) cur.clear(); } } for (int i = 1; i <= N; i++) { if (i > 1) cout << ' '; cout << dist[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N, M; cin >> N >> M; vector<vector<Edge>> graph(N); for (int i = 0; i < M; ++i) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int c = ch - 'a'; graph[u].push_back({v, c}); graph[v].push_back({u, c}); } vector<int> answer(N, -1); answer[0] = 0; // Vertices discovered for the first time at the current depth. vector<int> current = {0}; vector<int> next; int previousChar = 26; int depth = 0; while (!current.empty()) { int c = previousChar; for (int v : current) { for (const Edge &e : graph[v]) { c = min(c, e.c); } } if (c == 26) { break; } next.clear(); for (int v : current) { for (const Edge &e : graph[v]) { if (e.c == c && answer[e.to] == -1) { answer[e.to] = depth + 1; next.push_back(e.to); } } } current.swap(next); previousChar = c; ++depth; } for (int i = 0; i < N; ++i) { if (i) { cout << ' '; } cout << answer[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; char c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N, M; cin >> N >> M; vector<vector<Edge>> graph(N + 1); for (int i = 0; i < M; ++i) { int u, v; char c; cin >> u >> v >> c; graph[u].push_back({v, c}); graph[v].push_back({u, c}); } const int UNVISITED = -2; vector<int> answer(N + 1, UNVISITED); vector<int> frontier = {1}; answer[1] = 0; int length = 0; while (!frontier.empty()) { char best = '{'; vector<int> candidates; for (int v : frontier) { for (const Edge &e : graph[v]) { if (e.c < best) { best = e.c; candidates.clear(); } if (e.c == best && answer[e.to] == UNVISITED) { candidates.push_back(e.to); } } } if (candidates.empty()) { break; } vector<int> next; for (int v : candidates) { if (answer[v] == UNVISITED) { answer[v] = length + 1; next.push_back(v); } } ++length; frontier.swap(next); } for (int i = 1; i <= N; ++i) { if (answer[i] == UNVISITED) { answer[i] = -1; } if (i > 1) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; const int INF = 1e9; while (T--) { int N, M; cin >> N >> M; vector<vector<Edge>> adj(N); vector<int> minLabel(N, 26); for (int i = 0; i < M; i++) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int c = ch - 'a'; adj[u].push_back({v, c}); adj[v].push_back({u, c}); minLabel[u] = min(minLabel[u], c); minLabel[v] = min(minLabel[v], c); } vector<int> ans(N, -1); ans[0] = 0; vector<int> S; S.push_back(0); int depth = 0; vector<int> dist0(N), dist1(N), q(2 * N); while (!S.empty()) { int c = 26; for (int u : S) { c = min(c, minLabel[u]); } vector<int> T; T.reserve(S.size()); for (int u : S) { if (minLabel[u] == c) { T.push_back(u); } } fill(dist0.begin(), dist0.end(), INF); fill(dist1.begin(), dist1.end(), INF); int head = 0, tail = 0; for (int u : T) { if (dist0[u] == INF) { dist0[u] = 0; q[tail++] = 2 * u; } } while (head < tail) { int id = q[head++]; int v = id >> 1; int parity = id & 1; int cur = parity ? dist1[v] : dist0[v]; for (const Edge &e : adj[v]) { if (e.c != c) continue; int to = e.to; int np = parity ^ 1; if (np == 0) { if (dist0[to] == INF) { dist0[to] = cur + 1; q[tail++] = 2 * to; } } else { if (dist1[to] == INF) { dist1[to] = cur + 1; q[tail++] = 2 * to + 1; } } } } int endDist = INF; for (int v = 0; v < N; v++) { if (minLabel[v] < c) { int d = min(dist0[v], dist1[v]); if (d < endDist) endDist = d; } } if (endDist == INF) { for (int v = 0; v < N; v++) { int d = min(dist0[v], dist1[v]); if (d < INF) { int val = depth + d; if (ans[v] == -1 || val < ans[v]) { ans[v] = val; } } } break; } for (int v = 0; v < N; v++) { int d = min(dist0[v], dist1[v]); if (d <= endDist) { int val = depth + d; if (ans[v] == -1 || val < ans[v]) { ans[v] = val; } } } vector<int> newS; int needParity = endDist & 1; if (needParity == 0) { for (int v = 0; v < N; v++) { if (dist0[v] <= endDist) { newS.push_back(v); } } } else { for (int v = 0; v < N; v++) { if (dist1[v] <= endDist) { newS.push_back(v); } } } depth += endDist; S.swap(newS); if (depth >= N) break; } for (int i = 0; i < N; i++) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N, M; cin >> N >> M; vector<vector<pair<int,int>>> adj(N); vector<unsigned int> mask(N, 0); for (int i = 0; i < M; i++) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int c = ch - 'a'; if (u == v) { adj[u].push_back({v, c}); } else { adj[u].push_back({v, c}); adj[v].push_back({u, c}); } mask[u] |= (1u << c); mask[v] |= (1u << c); } vector<long long> ans(N, -1); ans[0] = 0; vector<int> S; S.push_back(0); long long curTime = 0; int curChar = -1; if (mask[0] != 0) { curChar = __builtin_ctz(mask[0]); } vector<int> q; q.reserve(2 * N); while (curChar != -1) { vector<int> d0(N, INF), d1(N, INF); q.clear(); for (int v : S) { if (d0[v] == INF) { d0[v] = 0; q.push_back(v * 2); } } size_t head = 0; while (head < q.size()) { int state = q[head++]; int u = state >> 1; int parity = state & 1; int du = parity ? d1[u] : d0[u]; for (auto [v, label] : adj[u]) { if (label != curChar) continue; int np = parity ^ 1; int nd = du + 1; if (np == 0) { if (d0[v] > nd) { d0[v] = nd; q.push_back(v * 2); } } else { if (d1[v] > nd) { d1[v] = nd; q.push_back(v * 2 + 1); } } } } unsigned lowerMask = (curChar == 0 ? 0u : ((1u << curChar) - 1u)); int firstDrop = INF; for (int v = 0; v < N; v++) { if (mask[v] & lowerMask) { int d = min(d0[v], d1[v]); if (d < firstDrop) firstDrop = d; } } if (firstDrop == INF) { for (int v = 0; v < N; v++) { int d = min(d0[v], d1[v]); if (d < INF && ans[v] == -1) { ans[v] = curTime + d; } } break; } for (int v = 0; v < N; v++) { int d = min(d0[v], d1[v]); if (d <= firstDrop && ans[v] == -1) { ans[v] = curTime + d; } } vector<int> nextS; nextS.reserve(N); int needParity = firstDrop & 1; unsigned hasCurChar = (1u << curChar); for (int v = 0; v < N; v++) { int dpar = needParity ? d1[v] : d0[v]; if (dpar <= firstDrop) { if (dpar == 0 && firstDrop > 0 && (mask[v] & hasCurChar) == 0) { continue; } nextS.push_back(v); } } int nextChar = 26; for (int v : nextS) { unsigned m = mask[v] & lowerMask; if (m) { int lb = __builtin_ctz(m); if (lb < nextChar) nextChar = lb; } } curTime += firstDrop; S.swap(nextS); curChar = nextChar; if (curChar == 26) break; } for (int i = 0; i < N; i++) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; const int INF = 1e9; while (T--) { int N, M; cin >> N >> M; vector<int> mn(N, 26); vector<vector<pair<int,int>>> edgesByLabel(26); for (int i = 0; i < M; ++i) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int c = ch - 'a'; edgesByLabel[c].push_back({u, v}); mn[u] = min(mn[u], c); mn[v] = min(mn[v], c); } if (N == 1 && M == 0) { cout << 0 << '\n'; continue; } // Compute B(v): the lexicographically smallest infinite walk from v. vector<vector<int>> activeByLabel(26); for (int v = 0; v < N; ++v) { activeByLabel[mn[v]].push_back(v); } vector<int> runLen(N, -1), suffixRank(N, -1), rankB(N, -1); vector<int> rep; int nextRank = 0; vector<int> head(N, -1), distTmp(N, -1), bestTmp(N, -1); for (int c = 0; c < 26; ++c) { if (activeByLabel[c].empty()) continue; vector<int> touched; vector<int> to, nxt; int E = (int)edgesByLabel[c].size(); to.reserve(2 * E); nxt.reserve(2 * E); for (auto [u, v] : edgesByLabel[c]) { if (head[u] == -1) { touched.push_back(u); distTmp[u] = -1; bestTmp[u] = -1; } to.push_back(v); nxt.push_back(head[u]); head[u] = (int)to.size() - 1; if (head[v] == -1) { touched.push_back(v); distTmp[v] = -1; bestTmp[v] = -1; } to.push_back(u); nxt.push_back(head[v]); head[v] = (int)to.size() - 1; } vector<int> q; q.reserve(touched.size()); for (int x : touched) { if (mn[x] < c) { distTmp[x] = 0; bestTmp[x] = rankB[x]; q.push_back(x); } } for (size_t qi = 0; qi < q.size(); ++qi) { int u = q[qi]; int du = distTmp[u]; for (int e = head[u]; e != -1; e = nxt[e]) { int v = to[e]; if (mn[v] < c) continue; if (distTmp[v] == -1) { distTmp[v] = du + 1; bestTmp[v] = bestTmp[u]; q.push_back(v); } else if (distTmp[v] == du + 1) { bestTmp[v] = min(bestTmp[v], bestTmp[u]); } } } for (int v : activeByLabel[c]) { if (distTmp[v] == -1) { runLen[v] = INF; suffixRank[v] = -1; } else { runLen[v] = distTmp[v]; suffixRank[v] = bestTmp[v]; } } auto &vec = activeByLabel[c]; sort(vec.begin(), vec.end(), [&](int a, int b) { if (runLen[a] != runLen[b]) return runLen[a] < runLen[b]; return suffixRank[a] < suffixRank[b]; }); int prevRun = INT_MIN, prevSuf = INT_MIN, curRank = -1; for (int v : vec) { if (runLen[v] != prevRun || suffixRank[v] != prevSuf) { curRank = nextRank++; rep.push_back(v); prevRun = runLen[v]; prevSuf = suffixRank[v]; } rankB[v] = curRank; } for (int x : touched) head[x] = -1; } // Build the run-length encoding of W = B(1). vector<pair<int,int>> runs; int finalChar = -1; int cur = 0; while (true) { int c = mn[cur]; if (runLen[cur] == INF) { finalChar = c; break; } else { int d = runLen[cur]; runs.push_back({c, d}); int suf = suffixRank[cur]; cur = rep[suf]; } } // Simulate the word W to find the first occurrence of each vertex. vector<int> ans(N, -1); vector<char> inCurrent(N, 0); vector<int> currentSet; currentSet.push_back(0); inCurrent[0] = 1; ans[0] = 0; int curTime = 0; vector<int> headSim(N, -1); vector<int> d0(N, -1), d1(N, -1); for (auto [c, L] : runs) { vector<int> touched; vector<int> to, nxt; for (auto [u, v] : edgesByLabel[c]) { if (headSim[u] == -1) { touched.push_back(u); headSim[u] = -1; } to.push_back(v); nxt.push_back(headSim[u]); headSim[u] = (int)to.size() - 1; if (headSim[v] == -1) { touched.push_back(v); headSim[v] = -1; } to.push_back(u); nxt.push_back(headSim[v]); headSim[v] = (int)to.size() - 1; } for (int x : touched) { d0[x] = d1[x] = -1; } vector<pair<int,int>> q; q.reserve(touched.size() * 2); for (int x : touched) { if (inCurrent[x]) { d0[x] = 0; q.push_back({x, 0}); } } for (size_t qi = 0; qi < q.size(); ++qi) { int u = q[qi].first; int p = q[qi].second; int du = (p == 0 ? d0[u] : d1[u]); for (int e = headSim[u]; e != -1; e = nxt[e]) { int v = to[e]; int np = p ^ 1; if (np == 0) { if (d0[v] == -1) { d0[v] = du + 1; q.push_back({v, 0}); } } else { if (d1[v] == -1) { d1[v] = du + 1; q.push_back({v, 1}); } } } } int parity = L & 1; vector<int> nextSet; for (int v : touched) { int md = INF; if (d0[v] != -1) md = min(md, d0[v]); if (d1[v] != -1) md = min(md, d1[v]); if (md <= L && ans[v] == -1) { ans[v] = curTime + md; } int dpar = (parity == 0 ? d0[v] : d1[v]); if (dpar != -1 && dpar <= L) { nextSet.push_back(v); } } for (int v : currentSet) inCurrent[v] = 0; for (int v : nextSet) inCurrent[v] = 1; currentSet.swap(nextSet); curTime += L; for (int x : touched) headSim[x] = -1; } // Final infinite run. if (finalChar != -1) { int c = finalChar; vector<int> touched; vector<int> to, nxt; for (auto [u, v] : edgesByLabel[c]) { if (headSim[u] == -1) { touched.push_back(u); headSim[u] = -1; } to.push_back(v); nxt.push_back(headSim[u]); headSim[u] = (int)to.size() - 1; if (headSim[v] == -1) { touched.push_back(v); headSim[v] = -1; } to.push_back(u); nxt.push_back(headSim[v]); headSim[v] = (int)to.size() - 1; } for (int x : touched) d0[x] = -1; vector<int> q; for (int x : touched) { if (inCurrent[x]) { d0[x] = 0; q.push_back(x); } } for (size_t qi = 0; qi < q.size(); ++qi) { int u = q[qi]; for (int e = headSim[u]; e != -1; e = nxt[e]) { int v = to[e]; if (d0[v] == -1) { d0[v] = d0[u] + 1; q.push_back(v); } } } for (int v : touched) { if (d0[v] != -1 && ans[v] == -1) { ans[v] = curTime + d0[v]; } } for (int x : touched) headSim[x] = -1; } for (int i = 0; i < N; ++i) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int label; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; constexpr int ALPHABET = 26; constexpr int INF = 1'000'000'000; while (T--) { int N, M; cin >> N >> M; vector<vector<Edge>> adj(N); vector<int> minLabel(N, ALPHABET); for (int i = 0; i < M; ++i) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int label = ch - 'a'; adj[u].push_back({v, label}); if (u != v) { adj[v].push_back({u, label}); } else { // A self-loop only needs to be stored once. } minLabel[u] = min(minLabel[u], label); minLabel[v] = min(minLabel[v], label); } if (N == 1) { cout << 0 << '\n'; continue; } vector<int> answer(N, -1); vector<unsigned char> seen(N, 0); answer[0] = 0; seen[0] = 1; // The lexicographically greedy sequence of edge labels is // nonincreasing, so it has at most 26 constant-label phases. vector<int> current = {0}; int currentLength = 0; int currentLabel = minLabel[0]; vector<array<int, 2>> dist(N); vector<int> first(N); vector<int> bfsQueue; bfsQueue.reserve(2 * N); while (true) { for (int v : current) { if (!seen[v]) { seen[v] = 1; answer[v] = currentLength; } } // Only vertices whose minimum incident label equals // currentLabel can continue a currentLabel edge. vector<int> seeds; seeds.reserve(current.size()); for (int v : current) { if (minLabel[v] == currentLabel) { seeds.push_back(v); } } fill(dist.begin(), dist.end(), array<int, 2>{INF, INF}); bfsQueue.clear(); for (int v : seeds) { dist[v][0] = 0; bfsQueue.push_back(2 * v); } // BFS on (vertex, path-length parity), using only // currentLabel edges. for (size_t head = 0; head < bfsQueue.size(); ++head) { int state = bfsQueue[head]; int v = state >> 1; int parity = state & 1; int nextParity = parity ^ 1; for (const Edge &e : adj[v]) { if (e.label != currentLabel) continue; if (dist[e.to][nextParity] == INF) { dist[e.to][nextParity] = dist[v][parity] + 1; bfsQueue.push_back(2 * e.to + nextParity); } } } // A phase ends when a vertex with a smaller incident label // first becomes reachable. int phaseEnd = INF; for (int v = 0; v < N; ++v) { first[v] = min(dist[v][0], dist[v][1]); if (minLabel[v] < currentLabel) { phaseEnd = min(phaseEnd, first[v]); } } for (int v = 0; v < N; ++v) { if (!seen[v] && first[v] != INF && first[v] <= phaseEnd) { seen[v] = 1; answer[v] = currentLength + first[v]; } } if (phaseEnd == INF) { // This label phase continues forever. No later phase exists. break; } // Construct the exact endpoint set after phaseEnd edges // of the current label. vector<int> nextCurrent; nextCurrent.reserve(N); int nextLabel = ALPHABET; int parity = phaseEnd & 1; for (int v = 0; v < N; ++v) { if (dist[v][parity] <= phaseEnd) { nextCurrent.push_back(v); nextLabel = min(nextLabel, minLabel[v]); } } current.swap(nextCurrent); currentLength += phaseEnd; currentLabel = nextLabel; } for (int v = 0; v < N; ++v) { if (v) cout << ' '; cout << answer[v]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct FlatAdjacency { vector<int> head, to, nextEdge; FlatAdjacency(int n, size_t edgeCount) : head(n, -1) { to.reserve(2 * edgeCount); nextEdge.reserve(2 * edgeCount); } void addDirected(int u, int v) { int id = (int)to.size(); to.push_back(v); nextEdge.push_back(head[u]); head[u] = id; } void addUndirected(int u, int v) { addDirected(u, v); addDirected(v, u); } }; void solve() { int N, M; cin >> N >> M; array<vector<pair<int, int>>, 26> edgesByLabel; vector<int> minimumIncidentLabel(N, 26); for (int i = 0; i < M; ++i) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int label = ch - 'a'; edgesByLabel[label].push_back({u, v}); minimumIncidentLabel[u] = min(minimumIncidentLabel[u], label); minimumIncidentLabel[v] = min(minimumIncidentLabel[v], label); } vector<long long> answer(N, -1); answer[0] = 0; if (N > 1) { const int INF = 1'000'000'000; // Endpoints of the current greedy prefix. vector<int> current{0}; // Character used by the current constant-character phase. int phaseLabel = minimumIncidentLabel[0]; // Length of the prefix before the current phase. long long phaseStartTime = 0; vector<int> dist(2 * N); vector<int> bfsQueue(2 * N); while (true) { FlatAdjacency adjacency(N, edgesByLabel[phaseLabel].size()); vector<char> hasPhaseEdge(N, false); for (auto [u, v] : edgesByLabel[phaseLabel]) { adjacency.addUndirected(u, v); hasPhaseEdge[u] = true; hasPhaseEdge[v] = true; } /* * BFS on parity-expanded vertices. * * State 2*v+p means that we are at v after a walk whose length * has parity p. */ fill(dist.begin(), dist.end(), INF); int front = 0, back = 0; for (int v : current) { dist[2 * v] = 0; bfsQueue[back++] = 2 * v; } while (front < back) { int state = bfsQueue[front++]; int v = state / 2; int nextParity = (state & 1) ^ 1; for (int e = adjacency.head[v]; e != -1; e = adjacency.nextEdge[e]) { int w = adjacency.to[e]; int nextState = 2 * w + nextParity; if (dist[nextState] == INF) { dist[nextState] = dist[state] + 1; bfsQueue[back++] = nextState; } } } vector<int> firstAppearance(N, INF); int phaseEnd = INF; for (int v = 0; v < N; ++v) { firstAppearance[v] = min(dist[2 * v], dist[2 * v + 1]); // This vertex offers a smaller next character. if (minimumIncidentLabel[v] < phaseLabel) { phaseEnd = min(phaseEnd, firstAppearance[v]); } } if (phaseEnd == INF) { // The phase never changes character. for (int v = 0; v < N; ++v) { if (firstAppearance[v] != INF && answer[v] == -1) { answer[v] = phaseStartTime + firstAppearance[v]; } } break; } // Assign all vertices that first appear during this phase. for (int v = 0; v < N; ++v) { if (firstAppearance[v] != INF && firstAppearance[v] <= phaseEnd && answer[v] == -1) { answer[v] = phaseStartTime + firstAppearance[v]; } } // Compute the endpoint set after exactly phaseEnd phase edges. vector<int> nextCurrent; nextCurrent.reserve(N); if (phaseEnd == 0) { nextCurrent = current; } else { int parity = phaseEnd & 1; for (int v = 0; v < N; ++v) { if (hasPhaseEdge[v] && dist[2 * v + parity] <= phaseEnd) { nextCurrent.push_back(v); } } } int nextLabel = 26; for (int v : nextCurrent) { nextLabel = min(nextLabel, minimumIncidentLabel[v]); } phaseStartTime += phaseEnd; current.swap(nextCurrent); phaseLabel = nextLabel; } } for (int i = 0; i < N; ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int label; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; constexpr int ALPHABET = 26; while (T--) { int N, M; cin >> N >> M; vector<vector<Edge>> adj(N); vector<int> smallest(N, ALPHABET); for (int i = 0; i < M; ++i) { int u, v; char ch; cin >> u >> v >> ch; --u; --v; int label = ch - 'a'; adj[u].push_back({v, label}); adj[v].push_back({u, label}); smallest[u] = min(smallest[u], label); smallest[v] = min(smallest[v], label); } vector<long long> answer(N, -1); answer[0] = 0; vector<int> current{0}; vector<int> nextSet; vector<int> bfsQueue; bfsQueue.reserve(N); vector<int> dist(N); vector<int> marked(N, 0); int stamp = 0; // The current set consists of vertices reachable after exactly // 'depth' edges of the greedily constructed lexicographic prefix. long long depth = 0; while (!current.empty()) { int currentLabel = ALPHABET; for (int v : current) { currentLabel = min(currentLabel, smallest[v]); } // This occurs only for the single-vertex, zero-edge graph. if (currentLabel == ALPHABET) { break; } // While currentLabel stays unchanged, the greedy set evolves // by taking all neighbors through edges with this label. fill(dist.begin(), dist.end(), -1); bfsQueue.clear(); for (int v : current) { if (dist[v] == -1) { dist[v] = 0; bfsQueue.push_back(v); } } for (size_t head = 0; head < bfsQueue.size(); ++head) { int v = bfsQueue[head]; for (const Edge &e : adj[v]) { if (e.label == currentLabel && dist[e.to] == -1) { dist[e.to] = dist[v] + 1; bfsQueue.push_back(e.to); } } } // Find the first layer at which the greedy label decreases. int switchDistance = N + 1; int nextLabel = ALPHABET; for (int v = 0; v < N; ++v) { if (dist[v] == -1 || smallest[v] >= currentLabel) { continue; } if (dist[v] < switchDistance) { switchDistance = dist[v]; nextLabel = smallest[v]; } else if (dist[v] == switchDistance) { nextLabel = min(nextLabel, smallest[v]); } } int limit = (switchDistance == N + 1 ? N : switchDistance); // A vertex's first occurrence in this phase is its BFS distance. for (int v = 0; v < N; ++v) { if (dist[v] != -1 && dist[v] <= limit) { long long candidate = depth + dist[v]; if (answer[v] == -1 || candidate < answer[v]) { answer[v] = candidate; } } } // No smaller label is reachable, so the greedy process remains // in this phase forever. if (switchDistance == N + 1) { break; } // At the switching layer, only vertices whose smallest incident // label equals nextLabel can use an edge with that label. ++stamp; nextSet.clear(); for (int v = 0; v < N; ++v) { if (dist[v] != switchDistance || smallest[v] != nextLabel) { continue; } for (const Edge &e : adj[v]) { if (e.label == nextLabel && marked[e.to] != stamp) { marked[e.to] = stamp; nextSet.push_back(e.to); } } } depth += static_cast<long long>(switchDistance) + 1; current.swap(nextSet); } for (int i = 0; i < N; ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }