結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー | 👑 emthrm |
提出日時 | 2021-08-07 01:04:56 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 117 ms / 2,000 ms |
コード長 | 4,016 bytes |
コンパイル時間 | 2,758 ms |
コンパイル使用メモリ | 226,652 KB |
実行使用メモリ | 12,724 KB |
最終ジャッジ日時 | 2024-09-17 05:16:48 |
合計ジャッジ時間 | 6,303 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 81 ms
11,960 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 85 ms
12,724 KB |
testcase_06 | AC | 65 ms
10,368 KB |
testcase_07 | AC | 61 ms
10,240 KB |
testcase_08 | AC | 71 ms
10,112 KB |
testcase_09 | AC | 57 ms
10,112 KB |
testcase_10 | AC | 53 ms
10,112 KB |
testcase_11 | AC | 58 ms
10,112 KB |
testcase_12 | AC | 75 ms
10,112 KB |
testcase_13 | AC | 74 ms
10,240 KB |
testcase_14 | AC | 75 ms
10,112 KB |
testcase_15 | AC | 106 ms
12,288 KB |
testcase_16 | AC | 117 ms
12,160 KB |
testcase_17 | AC | 100 ms
12,116 KB |
testcase_18 | AC | 2 ms
6,944 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 3 ms
6,940 KB |
testcase_21 | AC | 3 ms
6,940 KB |
testcase_22 | AC | 3 ms
6,944 KB |
testcase_23 | AC | 2 ms
6,940 KB |
testcase_24 | AC | 3 ms
6,940 KB |
testcase_25 | AC | 3 ms
6,940 KB |
testcase_26 | AC | 3 ms
6,940 KB |
testcase_27 | AC | 2 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,944 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,940 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 2 ms
6,944 KB |
testcase_35 | AC | 1 ms
6,940 KB |
testcase_36 | AC | 2 ms
6,944 KB |
testcase_37 | AC | 2 ms
6,944 KB |
testcase_38 | AC | 2 ms
6,944 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <typename CostType> struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); vector<char> c(m); vector<vector<int>> graph(n); REP(i, m) { cin >> a[i] >> b[i] >> c[i]; --a[i]; --b[i]; graph[a[i]].emplace_back(i); graph[b[i]].emplace_back(i); } if (set<char>(ALL(c)).size() == 1) { cout << "-1\n"; return 0; } vector<int> prev(n, -1); prev[0] = -2; queue<int> que({0}); while (!que.empty()) { int ver = que.front(); que.pop(); for (int id : graph[ver]) { const int dst = (a[id] == ver ? b : a)[id]; if (prev[dst] == -1) { prev[dst] = id; que.emplace(dst); } } } vector<int> ans; string s = ""; int cur = n - 1; while (cur != 0) { ans.emplace_back(prev[cur]); s += c[prev[cur]]; cur = (a[prev[cur]] == cur ? b : a)[prev[cur]]; } reverse(ALL(ans)); reverse(ALL(s)); if (equal(ALL(s), s.rbegin())) { if (set<char>(ALL(s)).size() > 1) { ans.resize(n * 2 - (ans.size() & 1), ans.back()); } else { const char ch = s.front(); fill(ALL(prev), -1); for (int id : ans) prev[a[id]] = prev[b[id]] = -2; REP(i, n) { if (prev[i] == -2) que.emplace(i); } cur = -1; while (!que.empty()) { int ver = que.front(); que.pop(); for (int id : graph[ver]) { const int dst = (a[id] == ver ? b : a)[id]; if (prev[dst] == -1) { prev[dst] = id; que.emplace(dst); if (cur == -1 && c[id] != ch) cur = dst; } } } assert(cur != -1); vector<int> sub_ans; string sub_s = ""; while (prev[cur] != -2) { sub_ans.emplace_back(prev[cur]); sub_s += c[prev[cur]]; cur = (a[prev[cur]] == cur ? b : a)[prev[cur]]; } reverse(ALL(sub_ans)); reverse(ALL(sub_s)); for (int i = static_cast<int>(sub_ans.size()) - 1; i >= 0; --i) { sub_ans.emplace_back(sub_ans[i]); sub_s += sub_s[i]; } if (cur == 0) { ans.insert(ans.begin(), ALL(sub_ans)); s.insert(s.begin(), ALL(sub_s)); } else { int pos = 0; REP(i, ans.size()) { pos = (a[ans[i]] == pos ? b : a)[ans[i]]; if (pos == cur) { ans.insert(ans.begin() + (i + 1), ALL(sub_ans)); s.insert(s.begin() + (i + 1), ALL(sub_s)); break; } } } if (equal(ALL(s), s.rbegin())) ans.resize(n * 2 - (ans.size() & 1), ans.back()); } } const int k = ans.size(); cout << k << '\n'; REP(i, k) cout << ans[i] + 1 << '\n'; return 0; }