結果

問題 No.1612 I hate Construct a Palindrome
ユーザー SSRSSSRS
提出日時 2021-07-21 22:11:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 1,918 ms
コンパイル使用メモリ 188,220 KB
実行使用メモリ 58,840 KB
最終ジャッジ日時 2023-09-24 18:01:31
合計ジャッジ時間 7,588 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 383 ms
54,860 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 386 ms
55,180 KB
testcase_06 AC 179 ms
54,924 KB
testcase_07 AC 185 ms
54,800 KB
testcase_08 AC 166 ms
54,636 KB
testcase_09 AC 125 ms
54,780 KB
testcase_10 AC 128 ms
54,592 KB
testcase_11 AC 127 ms
54,596 KB
testcase_12 AC 134 ms
54,900 KB
testcase_13 AC 136 ms
54,836 KB
testcase_14 AC 134 ms
54,904 KB
testcase_15 AC 224 ms
58,840 KB
testcase_16 AC 217 ms
58,728 KB
testcase_17 AC 227 ms
58,752 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,384 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,384 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<tuple<char, int, int>>> E(N);
  for (int i = 0; i < M; i++){
    int A, B;
    char C;
    cin >> A >> B >> C;
    A--;
    B--;
    E[A].push_back(make_tuple(C, i, B));
    E[B].push_back(make_tuple(C, i, A));
  }
  vector<vector<int>> d(27, vector<int>(N, -1));
  vector<vector<int>> pv(27, vector<int>(N, -1));
  vector<vector<int>> pe(27, vector<int>(N, -1));
  vector<vector<int>> ps(27, vector<int>(N, -1));
  vector<vector<char>> pc(27, vector<char>(N, -1));
  queue<pair<int, int>> Q;
  for (auto e : E[0]){
    char c = get<0>(e);
    int w = get<2>(e);
    int s = c - 'a';
    d[s][w] = 1;
    pv[s][w] = 0;
    pe[s][w] = get<1>(e);
    ps[s][w] = s;
    pc[s][w] = c;
    Q.push(make_pair(s, w));
  }
  while (!Q.empty()){
    int s = Q.front().first;
    int v = Q.front().second;
    Q.pop();
    for (auto e : E[v]){
      char c = get<0>(e);
      int w = get<2>(e);
      int s2 = s;
      if (s != c - 'a'){
        s2 = 26;
      }
      if (d[s2][w] == -1){
        d[s2][w] = d[s][v] + 1;
        pv[s2][w] = v;
        pe[s2][w] = get<1>(e);
        ps[s2][w] = s;
        pc[s2][w] = c;
        Q.push(make_pair(s2, w));
      }
    }
  }
  if (d[26][N - 1] == -1){
    cout << -1 << endl;
  } else {
    string S;
    vector<int> ans;
    int c = N - 1;
    int s = 26;
    while (true){
      S += pc[s][c];
      ans.push_back(pe[s][c]);
      if (d[s][c] == 1){
        break;
      }
      int c2 = pv[s][c];
      int s2 = ps[s][c];
      c = c2;
      s = s2;
    }
    reverse(S.begin(), S.end());
    reverse(ans.begin(), ans.end());
    string S2 = S;
    reverse(S2.begin(), S2.end());
    if (S == S2){
      char ch = pc[26][N - 1];
      S += ch;
      S += ch;
      ans.push_back(pe[26][N - 1]);
      ans.push_back(pe[26][N - 1]);
      S2 = S;
      reverse(S2.begin(), S2.end());
    }
    int k = ans.size();
    cout << k << endl;
    for (int i = 0; i < k; i++){
      cout << ans[i] + 1 << endl;
    }
  }
}
0