結果

問題 No.1494 LCS on Tree
ユーザー snow39snow39
提出日時 2021-04-30 23:11:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,013 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 122,456 KB
実行使用メモリ 462,432 KB
最終ジャッジ日時 2023-09-26 07:54:59
合計ジャッジ時間 34,097 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1,128 ms
452,584 KB
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 1,128 ms
462,432 KB
testcase_15 AC 1,097 ms
413,252 KB
testcase_16 AC 1,005 ms
261,760 KB
testcase_17 AC 1,046 ms
334,300 KB
testcase_18 AC 1,033 ms
304,160 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 1,108 ms
452,436 KB
testcase_31 AC 1,074 ms
390,564 KB
testcase_32 AC 1,020 ms
286,508 KB
testcase_33 AC 1,013 ms
274,196 KB
testcase_34 AC 1,004 ms
274,008 KB
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 1,341 ms
99,904 KB
testcase_46 AC 1,352 ms
99,836 KB
testcase_47 AC 1,342 ms
100,076 KB
testcase_48 AC 1,342 ms
99,832 KB
testcase_49 AC 1,355 ms
99,832 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<int>> last(M+1,vector<int>(26,-1));
    for(int i=0;i<M;i++){
      for(int j=0;j<26;j++){
        last[i+1][j]=last[i][j];
      }
      last[i+1][S[i]-'a']=i;
    }

    reverse(all(S));
    vector<vector<int>> Rlast(M+1,vector<int>(26,-1));
    for(int i=0;i<M;i++){
      for(int j=0;j<26;j++){
        Rlast[i+1][j]=Rlast[i][j];
      }
      Rlast[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);
  reverse(all(S));
  vector<vector<int>> tp=solve(Rlast);

  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));
    }
    for(auto [nex,d]:g[now]){
      if(nex==par) continue;
      calc_ans(calc_ans,nex,now);
      for(int k=0;k<=M;k++){
          int slt=last[k][d];
          int tlt=Rlast[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);
          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]));
    }

    int res=0;
    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