結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー | jupiro |
提出日時 | 2021-07-21 22:18:15 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,914 bytes |
コンパイル時間 | 2,539 ms |
コンパイル使用メモリ | 231,108 KB |
実行使用メモリ | 15,940 KB |
最終ジャッジ日時 | 2024-07-17 19:10:38 |
合計ジャッジ時間 | 9,190 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 1 ms
6,940 KB |
testcase_28 | AC | 2 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 2 ms
6,940 KB |
testcase_31 | AC | 2 ms
6,944 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 1 ms
6,940 KB |
testcase_35 | AC | 1 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 2 ms
6,940 KB |
testcase_38 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using ll = long long; using std::cin; using std::cout; using std::endl; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = (int)1e9 + 7; const long long INF = 1LL << 60; bool palindrome(const std::string &s) { int l = 0; int r = (int)s.size() - 1; while (l < r) { if(s[l] != s[r]) return false; l += 1, r -= 1; } return true; } void solve() { int n, m; cin >> n >> m; std::vector<std::vector<std::pair<int, char>>> g(n); std::vector<std::vector<int>> edge(26); std::vector<std::pair<int,int>> vp(m); std::vector<char> c(m); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; u -= 1, v -= 1; vp[i] = {u, v}; cin >> c[i]; g[u].emplace_back(v, i); g[v].emplace_back(u, i); edge[c[i] - 'a'].emplace_back(i); } std::vector<std::pair<int, int>> e(2, { -1, -1 }); std::vector<int> eid(2, -1); for (int i = 0; i < 26; ++i) { if(edge[i].empty()) continue; if(e[0].first == -1) { eid[0] = edge[i][0]; e[0] = vp[edge[i][0]]; } else { eid[1] = edge[i][0]; e[1] = vp[edge[i][0]]; } } if(e[1].first == -1) { cout << -1 << "\n"; return; } { std::vector<int> d(n, inf); std::queue<int> q; q.emplace(0); d[0] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { q.emplace(nxt); } } } for (int i = 0; i < 2; i++) if(d[e[i].first] > d[e[i].second]) std::swap(e[i].first, e[i].second); if(d[e[0].first] > d[e[1].first]) { std::swap(eid[0], eid[1]); std::swap(e[0], e[1]); } } std::vector<int> res; std::string s; { const int st = 0; std::vector<int> d(n, inf), b(n, -1), bid(n, -1); std::queue<int> q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } std::string t; std::vector<int> ans; int to = e[0].first; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } s += c[eid[0]]; res.emplace_back(eid[0]); { const int st = e[0].second; std::vector<int> d(n, inf), b(n, -1), bid(n, -1); std::queue<int> q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } if(d[e[1].first] > d[e[1].second]) std::swap(e[1].first, e[1].second); std::string t; std::vector<int> ans; int to = e[1].first; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } s += c[eid[1]]; res.emplace_back(eid[1]); { const int st = e[1].second; std::vector<int> d(n, inf), b(n, -1), bid(n, -1); std::queue<int> q; q.emplace(st); d[st] = 0; while(not q.empty()) { const int cur = q.front(); q.pop(); for (const auto &[nxt, id] : g[cur]) { if(chmin(d[nxt], d[cur] + 1)) { b[nxt] = cur; bid[nxt] = id; q.emplace(nxt); } } } std::string t; std::vector<int> ans; int to = n - 1; while (to != st) { t += c[bid[to]]; ans.emplace_back(bid[to]); to = b[to]; } std::reverse(t.begin(), t.end()); std::reverse(ans.begin(), ans.end()); s += t; for (const auto &e : ans) res.emplace_back(e); } if (palindrome(s)) { s += c[g[n - 1][0].second]; s += c[g[n - 1][0].second]; res.emplace_back(g[n - 1][0].second); res.emplace_back(g[n - 1][0].second); } if(palindrome(s)) { assert(false); } cout << res.size() << "\n"; for (const auto & e : res) cout << e + 1 << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int kkt = 1; // cin >> kkt; while(kkt--) solve(); return 0; }