結果

問題 No.1494 LCS on Tree
ユーザー snow39snow39
提出日時 2021-05-01 10:15:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,326 ms / 2,000 ms
コード長 3,131 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 122,864 KB
実行使用メモリ 462,348 KB
最終ジャッジ日時 2023-09-06 10:03:28
合計ジャッジ時間 33,472 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1,085 ms
452,636 KB
testcase_04 AC 881 ms
41,744 KB
testcase_05 AC 894 ms
40,708 KB
testcase_06 AC 894 ms
41,912 KB
testcase_07 AC 878 ms
41,128 KB
testcase_08 AC 887 ms
40,024 KB
testcase_09 AC 867 ms
40,692 KB
testcase_10 AC 881 ms
42,996 KB
testcase_11 AC 886 ms
41,008 KB
testcase_12 AC 875 ms
41,252 KB
testcase_13 AC 881 ms
39,976 KB
testcase_14 AC 1,077 ms
462,348 KB
testcase_15 AC 1,044 ms
413,420 KB
testcase_16 AC 989 ms
261,840 KB
testcase_17 AC 1,086 ms
334,512 KB
testcase_18 AC 1,003 ms
304,252 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1,098 ms
452,488 KB
testcase_31 AC 1,053 ms
391,068 KB
testcase_32 AC 997 ms
286,320 KB
testcase_33 AC 994 ms
274,336 KB
testcase_34 AC 994 ms
274,060 KB
testcase_35 AC 50 ms
6,260 KB
testcase_36 AC 259 ms
14,572 KB
testcase_37 AC 50 ms
7,384 KB
testcase_38 AC 205 ms
15,236 KB
testcase_39 AC 400 ms
20,536 KB
testcase_40 AC 130 ms
10,500 KB
testcase_41 AC 649 ms
31,416 KB
testcase_42 AC 133 ms
11,436 KB
testcase_43 AC 600 ms
30,064 KB
testcase_44 AC 427 ms
22,672 KB
testcase_45 AC 1,326 ms
99,900 KB
testcase_46 AC 1,321 ms
100,000 KB
testcase_47 AC 1,278 ms
99,940 KB
testcase_48 AC 1,296 ms
99,940 KB
testcase_49 AC 1,304 ms
99,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  string S;
  cin>>S;
  int M=S.size();
  vector<vector<pair<int,int>>> g(N);
  rep(i,N-1){
    int a,b;
    char c;
    cin>>a>>b>>c;
    a--;b--;
    int d=c-'a';
    g[a].push_back(mkp(b,d));
    g[b].push_back(mkp(a,d));
  }

    vector<vector<vector<int>>> last(2,vector<vector<int>>(M+1,vector<int> (26,-1)));
    for(int i=0;i<M;i++){
      for(int j=0;j<26;j++){
        last[0][i+1][j]=last[0][i][j];
      }
      last[0][i+1][S[i]-'a']=i;
    }

    reverse(all(S));
    for(int i=0;i<M;i++){
      for(int j=0;j<26;j++){
        last[1][i+1][j]=last[1][i][j];
      }
      last[1][i+1][S[i]-'a']=i;
    }
    reverse(all(S));

  auto solve = [&](vector<vector<int>> last) -> vector<vector<int>> {
    vector<vector<int>> dp(N,vector<int> (M+1,0));

    auto dfs = [&](auto &&dfs,int now,int par)->void{
      for(auto [nex,d]:g[now]){
        if(nex==par) continue;
        dfs(dfs,nex,now);
        for(int k=0;k<=M;k++) chmax(dp[now][k],dp[nex][k]);
        for(int k=0;k<=M;k++){
          int lt=last[k][d];
          if(lt==-1) continue;
          chmax(dp[now][k],dp[nex][lt]+1);
        }
      }
      for(int k=0;k<M;k++) chmax(dp[now][k+1],dp[now][k]);
    };

    dfs(dfs,0,-1);

    return dp;
  };

  vector<vector<int>> sp=solve(last[0]);
  vector<vector<int>> tp=solve(last[1]);

  auto calc_ans = [&](auto &&calc_ans,int now,int par)->int{
    vector<vector<pair<int,int>>> ss(M+1),tt(M+1);
    rep(i,M+1){
      ss[i].push_back(mkp(0,-1));
      tt[i].push_back(mkp(0,-2));
    }

    int res=0;
    for(auto [nex,d]:g[now]){
      if(nex==par) continue;
      chmax(res,calc_ans(calc_ans,nex,now));
      for(int k=0;k<=M;k++){
          int slt=last[0][k][d];
          int tlt=last[1][k][d];
          int sv=sp[nex][k],tv=tp[nex][k];
          if(slt!=-1) chmax(sv,sp[nex][slt]+1);
          if(tlt!=-1) chmax(tv,tp[nex][tlt]+1);
          if(k>0){
              chmax(sv,ss[k-1].back().first);
              chmax(tv,tt[k-1].back().first);
          }
          ss[k].push_back(mkp(sv,nex));
          tt[k].push_back(mkp(tv,nex));
      }
    }
    rep(i,M+1){
      sort(all(ss[i]));
      reverse(all(ss[i]));
      sort(all(tt[i]));
      reverse(all(tt[i]));
    }

    for(int k=0;k<=M;k++){
      auto [f,ft]=ss[k][0];
      auto [s,st]=tt[M-k][0];
      if(ft==st){
        if(tt[M-k].size()>=2) chmax(res,f+tt[M-k][1].first);
        if(ss[k].size()>=2) chmax(res,s+ss[k][1].first);
      }else{
        chmax(res,f+s);
      }
    }
    return res;
  };

  int ans=calc_ans(calc_ans,0,-1);
  cout<<ans<<endl;

  return 0;
}
0