結果

問題 No.1494 LCS on Tree
ユーザー SSRSSSRS
提出日時 2021-04-30 22:42:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,043 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 183,684 KB
実行使用メモリ 34,984 KB
最終ジャッジ日時 2023-09-26 07:11:30
合計ジャッジ時間 6,277 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 62 ms
34,792 KB
testcase_04 AC 64 ms
34,984 KB
testcase_05 AC 64 ms
34,732 KB
testcase_06 AC 64 ms
34,728 KB
testcase_07 AC 64 ms
34,728 KB
testcase_08 AC 64 ms
34,772 KB
testcase_09 AC 65 ms
34,728 KB
testcase_10 AC 66 ms
34,788 KB
testcase_11 AC 64 ms
34,736 KB
testcase_12 AC 64 ms
34,728 KB
testcase_13 AC 64 ms
34,728 KB
testcase_14 AC 64 ms
34,732 KB
testcase_15 AC 64 ms
34,740 KB
testcase_16 AC 63 ms
34,728 KB
testcase_17 AC 63 ms
34,728 KB
testcase_18 AC 64 ms
34,732 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,388 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 63 ms
34,736 KB
testcase_31 AC 63 ms
34,780 KB
testcase_32 AC 64 ms
34,848 KB
testcase_33 AC 62 ms
34,740 KB
testcase_34 AC 63 ms
34,736 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 5 ms
4,676 KB
testcase_38 AC 16 ms
10,492 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 47 ms
26,556 KB
testcase_42 AC 10 ms
7,536 KB
testcase_43 AC 43 ms
24,140 KB
testcase_44 AC 33 ms
18,480 KB
testcase_45 AC 65 ms
34,740 KB
testcase_46 AC 65 ms
34,732 KB
testcase_47 AC 65 ms
34,736 KB
testcase_48 AC 64 ms
34,784 KB
testcase_49 AC 65 ms
34,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  int M = S.size();
  vector<vector<pair<char, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v;
    char c;
    cin >> u >> v >> c;
    u--;
    v--;
    E[u].push_back(make_pair(c, v));
    E[v].push_back(make_pair(c, u));
  }
  vector<int> p(N, -1);
  vector<char> p2(N);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  vector<int> bfs;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    bfs.push_back(v);
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        p2[w] = P.first;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<vector<int>> dp1(N, vector<int>(M + 1, 0));
  for (int v : bfs){
    for (int w : c[v]){
      for (int i = 0; i <= M; i++){
        dp1[v][i] = max(dp1[v][i], dp1[w][i]);
      }
    }
    for (int i = 0; i < M; i++){
      dp1[v][i + 1] = max(dp1[v][i + 1], dp1[v][i]);
    }
    if (v != 0){
      for (int i = M - 1; i >= 0; i--){
        if (S[i] == p2[v]){
          dp1[v][i + 1] = max(dp1[v][i + 1], dp1[v][i] + 1);
        }
      }
    }
  }
  vector<vector<int>> dp2(N, vector<int>(M + 1, 0));
  for (int v : bfs){
    for (int w : c[v]){
      for (int i = M; i >= 0; i--){
        dp2[v][i] = max(dp2[v][i], dp2[w][i]);
      }
    }
    for (int i = M - 1; i >= 0; i--){
      dp2[v][i] = max(dp2[v][i], dp2[v][i + 1]);
    }
    if (v != 0){
      for (int i = 0; i < M; i++){
        if (S[i] == p2[v]){
          dp2[v][i] = max(dp2[v][i], dp2[v][i + 1] + 1);
        }
      }
    }
  }
  int ans = 0;
  for (int v : bfs){
    vector<int> mx1(M + 1, 0);
    vector<int> mx2(M + 1, 0);
    for (int w : c[v]){
      for (int i = 0; i <= M; i++){
        ans = max(ans, mx1[i] + dp2[w][i]);
        mx1[i] = max(mx1[i], dp1[w][i]);
        ans = max(ans, mx2[i] + dp1[w][i]);
        mx2[i] = max(mx2[i], dp2[w][i]);
      }
    }
  }
  cout << ans << endl;
}
0