結果

問題 No.1494 LCS on Tree
ユーザー snow39snow39
提出日時 2021-04-30 23:27:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,157 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 124,224 KB
実行使用メモリ 462,252 KB
最終ジャッジ日時 2023-09-26 08:34:53
合計ジャッジ時間 34,510 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1,139 ms
452,616 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,141 ms
462,252 KB
testcase_15 AC 1,109 ms
413,276 KB
testcase_16 AC 1,015 ms
261,736 KB
testcase_17 AC 1,062 ms
334,352 KB
testcase_18 AC 1,044 ms
304,148 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1,144 ms
452,388 KB
testcase_31 AC 1,102 ms
390,588 KB
testcase_32 AC 1,038 ms
286,276 KB
testcase_33 AC 1,038 ms
274,320 KB
testcase_34 AC 1,031 ms
274,060 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,366 ms
99,932 KB
testcase_46 AC 1,381 ms
99,864 KB
testcase_47 AC 1,355 ms
99,876 KB
testcase_48 AC 1,352 ms
99,912 KB
testcase_49 AC 1,375 ms
99,876 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]);
  reverse(all(S));
  vector<vector<int>> tp=solve(last[1]);
  reverse(all(S));

  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[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]));
    }

    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