結果

問題 No.1494 LCS on Tree
ユーザー LayCurseLayCurse
提出日時 2021-04-30 21:53:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,283 bytes
コンパイル時間 2,774 ms
コンパイル使用メモリ 217,760 KB
実行使用メモリ 40,532 KB
最終ジャッジ日時 2023-09-26 05:55:51
合計ジャッジ時間 6,607 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,520 KB
testcase_01 AC 3 ms
9,536 KB
testcase_02 AC 3 ms
9,536 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 78 ms
40,448 KB
testcase_15 AC 78 ms
40,384 KB
testcase_16 AC 77 ms
40,292 KB
testcase_17 AC 77 ms
40,356 KB
testcase_18 AC 78 ms
40,320 KB
testcase_19 AC 3 ms
9,580 KB
testcase_20 AC 2 ms
9,764 KB
testcase_21 AC 3 ms
9,592 KB
testcase_22 AC 3 ms
9,516 KB
testcase_23 AC 3 ms
9,688 KB
testcase_24 AC 3 ms
9,544 KB
testcase_25 AC 3 ms
9,520 KB
testcase_26 AC 3 ms
9,536 KB
testcase_27 AC 3 ms
9,528 KB
testcase_28 AC 2 ms
9,768 KB
testcase_29 AC 3 ms
9,500 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 35 ms
9,660 KB
testcase_46 AC 36 ms
9,628 KB
testcase_47 AC 35 ms
9,560 KB
testcase_48 AC 36 ms
9,544 KB
testcase_49 AC 35 ms
9,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#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');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
struct graph{
  int N;
  int*es;
  int**edge;
  void setEdgeRootedTree(int N__, int M, int A[], int B[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){
    int i;
    int j;
    int k;
    int*dist;
    int*q;
    int qs;
    int qe;
    int*ind;
    void*tmem;
    N = N__;
    tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * 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], &tmem);
    }
    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];
    }
    walloc1d(&dist, N, &tmem);
    walloc1d(&q, N, &tmem);
    walloc1d(&ind, N, &tmem);
    if(cnv==NULL){
      walloc1d(&cnv, N, &tmem);
    }
    for(i=(0);i<(N);i++){
      dist[i] = -1;
    }
    dist[root] = 0;
    qs = qe = 0;
    q[qe++] = root;
    while(qs < qe){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(dist[k]==-1){
          dist[k] = dist[i] + 1;
          q[qe++] = k;
        }
      }
    }
    if(reorder == 0){
      for(i=(0);i<(N);i++){
        cnv[i] = i;
      }
      for(i=(0);i<(N);i++){
        ind[i] = i;
      }
    }
    else{
      for(i=(0);i<(N);i++){
        cnv[i] = q[i];
      }
      for(i=(0);i<(N);i++){
        ind[cnv[i]] = i;
      }
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      es[ind[j]]++;
    }
    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++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      j = ind[j];
      k = ind[k];
      edge[j][es[j]++] = k;
    }
  }
}
;
template<class T> struct wgraph{
  int N;
  int*es;
  int**edge;
  T**cost;
  graph g;
  void setEdgeRootedTree(int N__, int M, int A[], int B[], T C[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){
    int i;
    int j;
    int k;
    int*dist;
    int*q;
    int qs;
    int qe;
    int*ind;
    void*tmem;
    N = N__;
    tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * N) + (sizeof(T*) * N + 15) + (sizeof(T) * M + 15 * N);
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&cost, 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], &tmem);
    }
    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];
    }
    walloc1d(&dist, N, &tmem);
    walloc1d(&q, N, &tmem);
    walloc1d(&ind, N, &tmem);
    if(cnv==NULL){
      walloc1d(&cnv, N, &tmem);
    }
    for(i=(0);i<(N);i++){
      dist[i] = -1;
    }
    dist[root] = 0;
    qs = qe = 0;
    q[qe++] = root;
    while(qs < qe){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(dist[k]==-1){
          dist[k] = dist[i] + 1;
          q[qe++] = k;
        }
      }
    }
    if(reorder == 0){
      for(i=(0);i<(N);i++){
        cnv[i] = i;
      }
      for(i=(0);i<(N);i++){
        ind[i] = i;
      }
    }
    else{
      for(i=(0);i<(N);i++){
        cnv[i] = q[i];
      }
      for(i=(0);i<(N);i++){
        ind[cnv[i]] = i;
      }
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      es[ind[j]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], 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++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      j = ind[j];
      k = ind[k];
      edge[j][es[j]] = k;
      cost[j][es[j]] = C[i];
      es[j]++;
    }
  }
}
;
int N;
int A[2002];
int B[2002];
char C[2002];
char S[2002];
int len;
wgraph<char> g;
int nx[26][2002];
int pre[26][2002];
int x[2002][2002];
int y[2002][2002];
int xxx[2002];
int yyy[2002];
int res;
void dfs(int n){
  int i;
  int j;
  int k;
  int s = g.es[n];
  int c;
  for(i=(0);i<(s);i++){
    dfs(g.edge[n][i]);
  }
  for(i=(0);i<(s);i++){
    k = g.edge[n][i];
    c = g.cost[n][i] - 'a';
    for(j=(0);j<(len+1);j++){
      xxx[n] = yyy[n] = 0;
    }
    for(j=(0);j<(len+1);j++){
      chmax(xxx[j], x[k][j]);
      if(j != len && nx[c][j] < len){
        chmax(xxx[nx[c][j] + 1], x[k][j] + 1);
      }
    }
    for(j=(0);j<(len+1);j++){
      chmax(yyy[j], y[k][j]);
      if(j != len && pre[c][len-1-j] >= 0){
        chmax(yyy[j + ((len-1-j) - pre[c][len-1-j]) + 1], y[k][j] + 1);
      }
    }
    for(j=(1);j<(len+1);j++){
      chmax(xxx[j], xxx[j-1]);
    }
    for(j=(1);j<(len+1);j++){
      chmax(yyy[j], yyy[j-1]);
    }
    for(j=(0);j<(len+1);j++){
      chmax(res, xxx[j] + y[n][len-j]);
    }
    for(j=(0);j<(len+1);j++){
      chmax(res, yyy[j] + x[n][len-j]);
    }
    for(j=(0);j<(len+1);j++){
      chmax(x[n][j], xxx[j]);
    }
    for(j=(0);j<(len+1);j++){
      chmax(y[n][j], yyy[j]);
    }
  }
}
int main(){
  int i;
  wmem = memarr;
  rd(N);
  len = rd(S);
  {
    int AlM5nNnR;
    for(AlM5nNnR=(0);AlM5nNnR<(N-1);AlM5nNnR++){
      rd(A[AlM5nNnR]);A[AlM5nNnR] += (-1);
      rd(B[AlM5nNnR]);B[AlM5nNnR] += (-1);
      rd(C[AlM5nNnR]);
    }
  }
  g.setEdgeRootedTree(N,N-1,A,B,C);
  for(i=(0);i<(26);i++){
    int j;
    for(j=(0);j<(len+1);j++){
      nx[i][j] = len;
    }
    for(j=(len)-1;j>=(0);j--){
      if(j != len-1){
        nx[i][j] = nx[i][j + 1];
      }
      if(S[j]==i+'a'){
        nx[i][j] = j;
      }
    }
    for(j=(0);j<(len+1);j++){
      pre[i][j] = -1;
    }
    for(j=(0);j<(len);j++){
      if(j != 0){
        pre[i][j] = pre[i][j - 1];
      }
      if(S[j]==i+'a'){
        pre[i][j] = j;
      }
    }
  }
  dfs(0);
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210405-1

// --- original code ---
// int N, A[2002], B[]; char C[];
// char S[]; int len;
// wgraph<char> g;
// int nx[26][2002], pre[26][2002];
// int x[2002][2002], y[2002][2002];
// int xxx[2002], yyy[2002];
// int res;
// 
// void dfs(int n){
//   int i, j, k, s = g.es[n], c;
//   rep(i,s) dfs(g.edge[n][i]);
//   rep(i,s){
//     k = g.edge[n][i];
//     c = g.cost[n][i] - 'a';
// 
//     rep(j,len+1) xxx[n] = yyy[n] = 0;
// 
//     rep(j,len+1){
//       xxx[j] >?= x[k][j];
//       if(j != len && nx[c][j] < len) xxx[nx[c][j] + 1] >?= x[k][j] + 1;
//     }
//     rep(j,len+1){
//       yyy[j] >?= y[k][j];
//       if(j != len && pre[c][len-1-j] >= 0) yyy[j + ((len-1-j) - pre[c][len-1-j]) + 1] >?= y[k][j] + 1;
//     }
// 
//     rep(j,1,len+1) xxx[j] >?= xxx[j-1];
//     rep(j,1,len+1) yyy[j] >?= yyy[j-1];
//     rep(j,len+1) res >?= xxx[j] + y[n][len-j];
//     rep(j,len+1) res >?= yyy[j] + x[n][len-j];
//     rep(j,len+1) x[n][j] >?= xxx[j];
//     rep(j,len+1) y[n][j] >?= yyy[j];
//   }
// }
// 
// {
//   rd(N,S@len,(A--,B--,C)(N-1));
//   g.setEdgeRootedTree(N,N-1,A,B,C);
// 
//   rep(i,26){
//     rep(j,len+1) nx[i][j] = len;
//     rrep(j,len){
//       if(j != len-1) nx[i][j] = nx[i][j + 1];
//       if(S[j]==i+'a') nx[i][j] = j;
//     }
//     rep(j,len+1) pre[i][j] = -1;
//     rep(j,len){
//       if(j != 0) pre[i][j] = pre[i][j - 1];
//       if(S[j]==i+'a') pre[i][j] = j;
//     }
//   }
// 
//   dfs(0);
//   wt(res);
// }
0