結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー | nok0 |
提出日時 | 2021-06-18 00:08:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 389 ms / 2,000 ms |
コード長 | 2,066 bytes |
コンパイル時間 | 2,605 ms |
コンパイル使用メモリ | 221,996 KB |
実行使用メモリ | 14,764 KB |
最終ジャッジ日時 | 2024-07-17 15:11:22 |
合計ジャッジ時間 | 7,262 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 5 ms
5,376 KB |
testcase_02 | AC | 375 ms
11,960 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 5 ms
5,376 KB |
testcase_05 | AC | 389 ms
11,964 KB |
testcase_06 | AC | 159 ms
11,300 KB |
testcase_07 | AC | 165 ms
11,416 KB |
testcase_08 | AC | 145 ms
11,560 KB |
testcase_09 | AC | 110 ms
11,044 KB |
testcase_10 | AC | 109 ms
10,924 KB |
testcase_11 | AC | 112 ms
11,048 KB |
testcase_12 | AC | 117 ms
11,244 KB |
testcase_13 | AC | 123 ms
11,236 KB |
testcase_14 | AC | 120 ms
11,244 KB |
testcase_15 | AC | 213 ms
14,636 KB |
testcase_16 | AC | 204 ms
14,764 KB |
testcase_17 | AC | 207 ms
14,632 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 3 ms
6,944 KB |
testcase_21 | AC | 3 ms
6,940 KB |
testcase_22 | AC | 3 ms
6,944 KB |
testcase_23 | AC | 2 ms
6,944 KB |
testcase_24 | AC | 3 ms
6,940 KB |
testcase_25 | AC | 3 ms
6,940 KB |
testcase_26 | AC | 3 ms
6,944 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | AC | 2 ms
6,940 KB |
testcase_29 | AC | 2 ms
6,944 KB |
testcase_30 | AC | 2 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,944 KB |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 2 ms
6,940 KB |
testcase_35 | AC | 2 ms
6,940 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 |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:97:22: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized] 97 | tmp = restore(x, n - 1); | ~~~~~~~^~~~~~~~~~ main.cpp:73:20: note: 'x' was declared here 73 | int s = 0, x, y, xy_id; | ^ main.cpp:86:25: warning: 'sc' may be used uninitialized [-Wmaybe-uninitialized] 86 | if(c != sc) { | ^~ main.cpp:74:14: note: 'sc' was declared here 74 | char sc; | ^~
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, x) for(int i = 0; i < (x); i++) constexpr int inf = 1 << 30; using T = tuple<int, int, int>; int bit; int main() { int n, m; cin >> n >> m; vector<T> edges; vector<vector<pair<int, int>>> g(n); auto is_palindrome = [](const string &s) { rep(i, s.size()) { if(s[i] != s[s.size() - 1 - i]) return false; } return true; }; auto bfs = [&](int s) { vector dist(n, inf); dist[s] = 0; queue<int> que; que.push(s); while(que.size()) { int now = que.front(); que.pop(); for(auto [to, ignored] : g[now]) { if(dist[to] <= dist[now] + 1) continue; dist[to] = dist[now] + 1; que.push(to); } } return dist; }; auto restore = [&](int s, int t) { vector<int> res; auto dist = bfs(s); int now = t; while(now != s) { for(auto [to, id] : g[now]) { if(dist[to] + 1 == dist[now]) { now = to; res.push_back(id); break; } } } reverse(res.begin(), res.end()); return res; }; rep(i, m) { int a, b; char c; cin >> a >> b >> c; --a, --b; g[a].emplace_back(b, i); g[b].emplace_back(a, i); edges.push_back({a, b, c - 'a'}); bit |= 1 << (c - 'a'); } if(__builtin_popcount(bit) == 1) { puts("-1"); return 0; } //方針 //0-s s-x x-y y-x x-(n-1) vector<int> res; int s = 0, x, y, xy_id; char sc; rep(i, m) { auto [u, v, ig] = edges[i]; if(!u or !v) { s = max(u, v); res.push_back(i); sc = get<2>(edges[i]); break; } } rep(i, m) { auto [u, v, c] = edges[i]; if(c != sc) { auto dist = bfs(s); if(dist[u] > dist[v]) swap(u, v); x = u, y = v, xy_id = i; break; } } auto tmp = restore(s, x); res.insert(res.end(), tmp.begin(), tmp.end()); rep(i, 2) res.push_back(xy_id); tmp = restore(x, n - 1); res.insert(res.end(), tmp.begin(), tmp.end()); string str = ""; for(auto v : res) str.push_back(get<2>(edges[v])); if(is_palindrome(str)) rep(i, 2) res.push_back(res.back()); cout << res.size() << endl; for(auto &v : res) cout << v + 1 << endl; return 0; }