結果

問題 No.1612 I hate Construct a Palindrome
ユーザー 👑 NachiaNachia
提出日時 2021-07-21 22:37:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,192 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 90,028 KB
実行使用メモリ 15,156 KB
最終ジャッジ日時 2023-09-24 18:53:19
合計ジャッジ時間 4,988 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 84 ms
15,156 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 95 ms
15,156 KB
testcase_06 AC 66 ms
12,556 KB
testcase_07 AC 73 ms
12,320 KB
testcase_08 AC 68 ms
12,316 KB
testcase_09 AC 71 ms
12,016 KB
testcase_10 AC 69 ms
12,004 KB
testcase_11 AC 81 ms
12,024 KB
testcase_12 AC 73 ms
12,228 KB
testcase_13 AC 80 ms
12,168 KB
testcase_14 AC 79 ms
12,140 KB
testcase_15 AC 126 ms
13,988 KB
testcase_16 AC 115 ms
14,156 KB
testcase_17 AC 129 ms
13,984 KB
testcase_18 AC 2 ms
4,376 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,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++) 

struct Edge2{ int u,v; char c; };
struct Edge{ int to; char c; int i; };
struct PathInfo{
  string s; vector<int> e;
  PathInfo& operator+=(const PathInfo& r){
    s += r.s;
    for(auto i : r.e) e.push_back(i);
    return *this;
  }
};

int N,M;
vector<vector<int>> E;
vector<Edge2> J;

PathInfo getPath(int s,int t){
  vector<int> I = {s};
  vector<int> P(N,-1);
  vector<int> Pi(N,-1);
  vector<int> F(N,0);
  F[s] = 1;
  rep(i,I.size()){
    int p = I[i];
    for(auto e : E[p]){
      int to = J[e].u ^ J[e].v ^ p;
      if(F[to] != 0) continue;
      F[to] = 1;
      I.push_back(to);
      Pi[to] = e;
      P[to] = p;
    }
  }
  string ans;
  vector<int> ansi;
  while(t != s){
    ans.push_back(J[Pi[t]].c);
    ansi.push_back(Pi[t]);
    t = P[t];
  }
  reverse(ans.begin(),ans.end());
  reverse(ansi.begin(),ansi.end());
  return { move(ans),move(ansi) };
}

bool iskaibun(const string& s){
  rep(i,s.size()) if(s[i] != s[s.size()-1-i]) return false;
  return true;
}

int main(){
  cin >> N >> M;
  E.resize(N);
  rep(i,M){
    int u,v; char c; cin >> u >> v >> c; u--; v--;
    E[u].push_back(i);
    E[v].push_back(i);
    J.push_back({u,v,c});
  }

  int firstE = E[N-1][0];
  int needE = -1;
  rep(i,M) if(J[i].c != J[firstE].c) needE = i;
  if(needE == -1){ cout << "-1\n"; return 0; }

  auto ans1 = getPath(0,J[needE].u);
  ans1 += PathInfo{string(1,J[needE].c),{needE}};
  ans1 += getPath(J[needE].v,N-1);
  auto ans2 = getPath(0,J[needE].v);
  ans2 += PathInfo{string(1,J[needE].c),{needE}};
  ans2 += getPath(J[needE].u,N-1);
  
  if(ans1.s.size() > ans2.s.size()) swap(ans1,ans2);

  if(iskaibun(ans1.s)){
    ans1.s += string(2,J[firstE].c);
    rep(c,2) ans1.e.push_back(firstE);
  }

  if(ans1.e.size() > 2*N){ cout << "-1\n"; return 0; }

  cout << ans1.e.size() << "\n";
  for(auto a : ans1.e) cout << (a+1) << "\n";

  return 0;
}

struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0