結果

問題 No.1612 I hate Construct a Palindrome
ユーザー risujirohrisujiroh
提出日時 2021-07-21 23:27:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,937 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 214,648 KB
実行使用メモリ 18,400 KB
最終ジャッジ日時 2023-09-24 19:54:25
合計ジャッジ時間 5,682 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 62 ms
13,472 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 66 ms
13,504 KB
testcase_06 AC 53 ms
13,548 KB
testcase_07 AC 59 ms
13,532 KB
testcase_08 AC 61 ms
13,844 KB
testcase_09 AC 57 ms
13,956 KB
testcase_10 AC 57 ms
13,688 KB
testcase_11 AC 54 ms
13,632 KB
testcase_12 AC 60 ms
14,080 KB
testcase_13 AC 57 ms
14,112 KB
testcase_14 AC 56 ms
13,984 KB
testcase_15 AC 105 ms
18,356 KB
testcase_16 AC 100 ms
18,400 KB
testcase_17 AC 102 ms
18,332 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  vector<vector<tuple<int, int, char>>> g(n);
  string s;
  for (int id = 0; id < m; ++id) {
    char c;
    cin >> a[id] >> b[id] >> c;
    --a[id], --b[id];
    g[a[id]].emplace_back(id, b[id], c);
    g[b[id]].emplace_back(id, a[id], c);
    s += c;
  }
  auto bfs = [&](int start) {
    vector<int> que{start};
    vector<int> d(n, -1), pe(n, -1);
    d[start] = 0;
    for (int z = 0; z < int(size(que)); ++z) {
      int v = que[z];
      for (auto&& [id, u, c] : g[v])
        if (d[u] == -1) {
          d[u] = d[v] + 1;
          pe[u] = id;
          que.push_back(u);
        }
    }
    return pair{d, pe};
  };
  int x = get<1>(g[0][0]);
  auto [dx, pex] = bfs(x);
  auto [dn, pen] = bfs(n - 1);
  for (int id = 0; id < m; ++id)
    if (s[id] != get<2>(g[0][0])) {
      string t;
      vector<int> ans;
      t += get<2>(g[0][0]);
      ans.push_back(get<0>(g[0][0]));
      for (int v = a[id]; v != x;) {
        int cur_id = pex[v];
        t += s[cur_id];
        ans.push_back(cur_id);
        v ^= a[cur_id] ^ b[cur_id];
      }
      reverse(begin(t) + 1, end(t));
      reverse(begin(ans) + 1, end(ans));
      t += s[id];
      ans.push_back(id);
      for (int v = b[id]; v != n - 1;) {
        int cur_id = pen[v];
        t += s[cur_id];
        ans.push_back(cur_id);
        v ^= a[cur_id] ^ b[cur_id];
      }
      if (t != string(rbegin(t), rend(t))) {
        cout << size(ans) << '\n';
        for (auto&& e : ans) cout << e + 1 << '\n';
      } else {
        cout << size(ans) + 2 << '\n';
        for (auto&& e : ans) cout << e + 1 << '\n';
        cout << ans.back() + 1 << '\n';
        cout << ans.back() + 1 << '\n';
      }
      exit(0);
    }
  cout << "-1\n";
}
0