結果
問題 | No.1494 LCS on Tree |
ユーザー | LayCurse |
提出日時 | 2021-04-30 23:07:14 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 100 ms / 2,000 ms |
コード長 | 10,275 bytes |
コンパイル時間 | 2,932 ms |
コンパイル使用メモリ | 221,244 KB |
実行使用メモリ | 40,264 KB |
最終ジャッジ日時 | 2024-11-20 15:04:43 |
合計ジャッジ時間 | 6,347 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
9,540 KB |
testcase_01 | AC | 3 ms
7,752 KB |
testcase_02 | AC | 4 ms
7,624 KB |
testcase_03 | AC | 98 ms
37,220 KB |
testcase_04 | AC | 69 ms
27,076 KB |
testcase_05 | AC | 68 ms
26,316 KB |
testcase_06 | AC | 71 ms
25,956 KB |
testcase_07 | AC | 68 ms
26,212 KB |
testcase_08 | AC | 69 ms
26,696 KB |
testcase_09 | AC | 68 ms
26,084 KB |
testcase_10 | AC | 68 ms
26,212 KB |
testcase_11 | AC | 68 ms
26,188 KB |
testcase_12 | AC | 68 ms
26,344 KB |
testcase_13 | AC | 70 ms
26,696 KB |
testcase_14 | AC | 92 ms
37,476 KB |
testcase_15 | AC | 92 ms
38,980 KB |
testcase_16 | AC | 88 ms
37,580 KB |
testcase_17 | AC | 88 ms
38,856 KB |
testcase_18 | AC | 87 ms
37,348 KB |
testcase_19 | AC | 3 ms
7,620 KB |
testcase_20 | AC | 3 ms
7,500 KB |
testcase_21 | AC | 3 ms
7,504 KB |
testcase_22 | AC | 3 ms
7,496 KB |
testcase_23 | AC | 3 ms
7,632 KB |
testcase_24 | AC | 3 ms
7,624 KB |
testcase_25 | AC | 3 ms
7,628 KB |
testcase_26 | AC | 3 ms
7,492 KB |
testcase_27 | AC | 3 ms
7,624 KB |
testcase_28 | AC | 3 ms
7,504 KB |
testcase_29 | AC | 3 ms
7,624 KB |
testcase_30 | AC | 100 ms
37,216 KB |
testcase_31 | AC | 97 ms
37,832 KB |
testcase_32 | AC | 96 ms
40,264 KB |
testcase_33 | AC | 95 ms
37,708 KB |
testcase_34 | AC | 97 ms
38,852 KB |
testcase_35 | AC | 9 ms
9,800 KB |
testcase_36 | AC | 28 ms
18,240 KB |
testcase_37 | AC | 7 ms
8,776 KB |
testcase_38 | AC | 19 ms
12,360 KB |
testcase_39 | AC | 38 ms
18,372 KB |
testcase_40 | AC | 14 ms
11,972 KB |
testcase_41 | AC | 51 ms
22,468 KB |
testcase_42 | AC | 13 ms
12,740 KB |
testcase_43 | AC | 50 ms
22,724 KB |
testcase_44 | AC | 39 ms
20,832 KB |
testcase_45 | AC | 46 ms
7,624 KB |
testcase_46 | AC | 46 ms
7,500 KB |
testcase_47 | AC | 46 ms
7,496 KB |
testcase_48 | AC | 46 ms
7,496 KB |
testcase_49 | AC | 46 ms
7,492 KB |
ソースコード
#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); // }