結果

問題 No.1494 LCS on Tree
ユーザー LayCurseLayCurse
提出日時 2021-04-30 23:07:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 10,275 bytes
コンパイル時間 3,140 ms
コンパイル使用メモリ 218,324 KB
実行使用メモリ 40,520 KB
最終ジャッジ日時 2023-08-12 22:46:55
合計ジャッジ時間 6,934 ms
ジャッジサーバーID
(参考情報)
judge10 / judge7
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,696 KB
testcase_01 AC 2 ms
9,500 KB
testcase_02 AC 3 ms
9,700 KB
testcase_03 AC 100 ms
40,324 KB
testcase_04 AC 68 ms
28,708 KB
testcase_05 AC 67 ms
28,632 KB
testcase_06 AC 68 ms
28,536 KB
testcase_07 AC 69 ms
28,820 KB
testcase_08 AC 69 ms
28,780 KB
testcase_09 AC 68 ms
28,652 KB
testcase_10 AC 68 ms
28,932 KB
testcase_11 AC 69 ms
28,712 KB
testcase_12 AC 69 ms
29,088 KB
testcase_13 AC 69 ms
28,908 KB
testcase_14 AC 92 ms
40,420 KB
testcase_15 AC 91 ms
40,520 KB
testcase_16 AC 88 ms
40,300 KB
testcase_17 AC 87 ms
40,264 KB
testcase_18 AC 88 ms
40,248 KB
testcase_19 AC 3 ms
9,744 KB
testcase_20 AC 3 ms
9,476 KB
testcase_21 AC 3 ms
9,452 KB
testcase_22 AC 3 ms
9,428 KB
testcase_23 AC 3 ms
9,484 KB
testcase_24 AC 3 ms
9,568 KB
testcase_25 AC 3 ms
9,424 KB
testcase_26 AC 3 ms
9,584 KB
testcase_27 AC 3 ms
9,544 KB
testcase_28 AC 3 ms
9,488 KB
testcase_29 AC 3 ms
9,500 KB
testcase_30 AC 100 ms
40,344 KB
testcase_31 AC 97 ms
40,268 KB
testcase_32 AC 96 ms
40,328 KB
testcase_33 AC 96 ms
40,432 KB
testcase_34 AC 95 ms
40,312 KB
testcase_35 AC 8 ms
11,840 KB
testcase_36 AC 28 ms
20,288 KB
testcase_37 AC 7 ms
10,796 KB
testcase_38 AC 19 ms
14,112 KB
testcase_39 AC 38 ms
22,500 KB
testcase_40 AC 15 ms
13,712 KB
testcase_41 AC 52 ms
24,268 KB
testcase_42 AC 14 ms
12,612 KB
testcase_43 AC 49 ms
24,804 KB
testcase_44 AC 39 ms
22,704 KB
testcase_45 AC 46 ms
9,424 KB
testcase_46 AC 46 ms
9,584 KB
testcase_47 AC 46 ms
9,728 KB
testcase_48 AC 46 ms
9,664 KB
testcase_49 AC 46 ms
9,544 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[j] = yyy[j] = 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);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);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[j] = yyy[j] = 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) 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) 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