結果

問題 No.1612 I hate Construct a Palindrome
ユーザー risujirohrisujiroh
提出日時 2021-07-21 23:26:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,937 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 213,764 KB
実行使用メモリ 18,588 KB
最終ジャッジ日時 2023-09-24 19:54:36
合計ジャッジ時間 6,810 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 73 ms
13,484 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 62 ms
13,648 KB
testcase_07 AC 61 ms
13,648 KB
testcase_08 AC 62 ms
13,564 KB
testcase_09 AC 62 ms
13,796 KB
testcase_10 AC 59 ms
13,680 KB
testcase_11 AC 59 ms
13,668 KB
testcase_12 AC 63 ms
14,124 KB
testcase_13 AC 68 ms
13,996 KB
testcase_14 AC 66 ms
14,020 KB
testcase_15 AC 113 ms
18,588 KB
testcase_16 AC 115 ms
18,360 KB
testcase_17 AC 114 ms
18,396 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 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,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 WA -
testcase_38 AC 2 ms
4,376 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(s), rend(s))) {
        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