結果

問題 No.1612 I hate Construct a Palindrome
ユーザー LayCurseLayCurse
提出日時 2021-07-21 21:38:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 7,329 bytes
コンパイル時間 3,268 ms
コンパイル使用メモリ 223,928 KB
実行使用メモリ 17,160 KB
最終ジャッジ日時 2023-09-24 16:06:38
合計ジャッジ時間 6,545 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,648 KB
testcase_01 AC 3 ms
7,460 KB
testcase_02 AC 22 ms
16,244 KB
testcase_03 AC 2 ms
7,684 KB
testcase_04 AC 2 ms
7,564 KB
testcase_05 AC 22 ms
16,188 KB
testcase_06 AC 19 ms
15,084 KB
testcase_07 AC 18 ms
15,064 KB
testcase_08 AC 17 ms
14,940 KB
testcase_09 AC 16 ms
14,772 KB
testcase_10 AC 16 ms
14,876 KB
testcase_11 AC 17 ms
14,800 KB
testcase_12 AC 18 ms
14,828 KB
testcase_13 AC 17 ms
14,780 KB
testcase_14 AC 18 ms
14,760 KB
testcase_15 AC 25 ms
14,864 KB
testcase_16 AC 27 ms
17,160 KB
testcase_17 AC 25 ms
14,900 KB
testcase_18 AC 2 ms
7,428 KB
testcase_19 AC 2 ms
7,552 KB
testcase_20 AC 3 ms
7,504 KB
testcase_21 AC 3 ms
7,488 KB
testcase_22 AC 3 ms
7,424 KB
testcase_23 AC 2 ms
7,552 KB
testcase_24 AC 3 ms
7,448 KB
testcase_25 AC 3 ms
7,676 KB
testcase_26 AC 2 ms
7,580 KB
testcase_27 AC 2 ms
7,548 KB
testcase_28 AC 3 ms
7,580 KB
testcase_29 AC 3 ms
7,468 KB
testcase_30 AC 2 ms
7,428 KB
testcase_31 AC 3 ms
7,724 KB
testcase_32 AC 2 ms
7,580 KB
testcase_33 AC 2 ms
7,412 KB
testcase_34 AC 3 ms
7,492 KB
testcase_35 AC 2 ms
7,528 KB
testcase_36 AC 2 ms
7,540 KB
testcase_37 AC 2 ms
7,488 KB
testcase_38 AC 2 ms
7,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
struct graph{
  int N;
  int*es;
  int**edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
  int shortestPath(const int s, const int t, int res[], void *mem=wmem){
    int i;
    int j;
    int k;
    int*q;
    int qs = 0;
    int qe = 0;
    int*b;
    walloc1d(&b, N, &mem);
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      b[i] = -1;
    }
    b[s] = -2;
    q[qe++] = s;
    while(qe > qs){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(b[k]!=-1){
          continue;
        }
        b[k] = i;
        q[qe++] = k;
      }
      if(b[t]!=-1){
        break;
      }
    }
    if(b[t]==-1){
      return -1;
    }
    k = 0;
    res[k] = i = t;
    while(i != s){
      res[++k] = (i = b[i]);
    }
    std::reverse(res, res+k+1);
    return k;
  }
}
;
template<class T> struct wgraph{
  int N;
  int*es;
  int**edge;
  T**cost;
  graph g;
  void setEdge(int N__, int M, int A[], int B[], T C[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    walloc1d(&cost, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&cost[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]] = B[i];
      edge[B[i]][es[B[i]]] = A[i];
      cost[A[i]][es[A[i]]++] = C[i];
      cost[B[i]][es[B[i]]++] = C[i];
    }
    g.N = N;
    g.es = es;
    g.edge = edge;
  }
}
;
template<class T> inline int isPalindrome(const int N, const T A[]){
  int i = 0;
  int j = N-1;
  while(i < j){
    if(A[i] != A[j]){
      return 0;
    }
    ;
    i++;
    j--;
  }
  return 1;
}
int N;
int M;
int A[200000+2];
int B[200000+2];
int ind[200000+2];
char C[200000+2];
wgraph<int> g;
int ps;
int p[200000+2];
int ress;
int res[200000+2];
char str[200000+2];
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int m;
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(M);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
      rd(C[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(M);i++){
    ind[i] = i;
  }
  for(i=(1);i<(M);i++){
    if(C[i] != C[0]){
      break;
    }
  }
  if(i==M){
    wt_L(-1);
    wt_L('\n');
    return 0;
  }
  g.setEdge(N,M,A,B,ind);
  k = C[g.cost[N-1][0]];
  for(m=(0);m<(M);m++){
    if(C[m] != k){
      break;
    }
  }
  ps = g.g.shortestPath(0, A[m], p);
  for(i=(1);i<(ps+1);i++){
    k = p[i-1];
    for(j=(0);j<(g.es[k]);j++){
      if(g.edge[k][j]==p[i]){
        res[ress++] = ind[g.cost[k][j]];
        break;
      }
    }
  }
  res[ress++] = m;
  ps = g.g.shortestPath(B[m], N-1, p);
  for(i=(1);i<(ps+1);i++){
    k = p[i-1];
    for(j=(0);j<(g.es[k]);j++){
      if(g.edge[k][j]==p[i]){
        res[ress++] = ind[g.cost[k][j]];
        break;
      }
    }
  }
  for(i=(0);i<(ress);i++){
    str[i] = C[res[i]];
  }
  while(isPalindrome(ress,str)){
    res[ress++] = g.cost[N-1][0];
    res[ress++] = g.cost[N-1][0];
    for(i=(0);i<(ress);i++){
      str[i] = C[res[i]];
    }
  }
  wt_L(ress);
  wt_L('\n');
  {
    int XJIcIBrW;
    for(XJIcIBrW=(0);XJIcIBrW<(ress);XJIcIBrW++){
      wt_L(res[XJIcIBrW]+1);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay version 20210717-1 [beta]

// --- original code ---
// int N, M, A[2d5+2], B[], ind[];
// char C[];
// wgraph<int> g;
// int ps, p[];
// 
// int ress, res[]; char str[];
// 
// {
//   int i, j, k, m;
// 
//   rd(N,M,(A--,B--,C)(M));
//   rep(i,M) ind[i] = i;
//   rep(i,1,M) if(C[i] != C[0]) break;
//   if(i==M) wt(-1), return 0;
//   g.setEdge(N,M,A,B,ind);
// 
//   k = C[g.cost[N-1][0]];
//   rep(m,M) if(C[m] != k) break;
// 
//   ps = g.g.shortestPath(0, A[m], p);
//   rep(i,1,ps+1){
//     k = p[i-1];
//     rep(j,g.es[k]) if(g.edge[k][j]==p[i]) res[ress++] = ind[g.cost[k][j]], break;
//   }
//   res[ress++] = m;
//   ps = g.g.shortestPath(B[m], N-1, p);
//   rep(i,1,ps+1){
//     k = p[i-1];
//     rep(j,g.es[k]) if(g.edge[k][j]==p[i]) res[ress++] = ind[g.cost[k][j]], break;
//   }
// 
//   rep(i,ress) str[i] = C[res[i]];
//   while(isPalindrome(ress,str)){
//     res[ress++] = g.cost[N-1][0];
//     res[ress++] = g.cost[N-1][0];
//     rep(i,ress) str[i] = C[res[i]];
//   }
//   wt(ress);
//   wtLn(res(ress)+1);
// }
0