結果

問題 No.1494 LCS on Tree
ユーザー SSRSSSRS
提出日時 2021-04-30 22:45:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,228 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 183,872 KB
実行使用メモリ 35,028 KB
最終ジャッジ日時 2023-08-12 22:45:56
合計ジャッジ時間 5,836 ms
ジャッジサーバーID
(参考情報)
judge8 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 68 ms
34,740 KB
testcase_04 AC 65 ms
34,784 KB
testcase_05 AC 67 ms
34,844 KB
testcase_06 AC 66 ms
34,776 KB
testcase_07 AC 67 ms
34,836 KB
testcase_08 AC 66 ms
34,740 KB
testcase_09 AC 66 ms
34,744 KB
testcase_10 AC 65 ms
34,844 KB
testcase_11 AC 67 ms
34,796 KB
testcase_12 AC 66 ms
34,844 KB
testcase_13 AC 65 ms
34,744 KB
testcase_14 AC 66 ms
34,784 KB
testcase_15 AC 63 ms
34,848 KB
testcase_16 AC 64 ms
34,760 KB
testcase_17 AC 64 ms
34,796 KB
testcase_18 AC 64 ms
34,760 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 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,384 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 65 ms
34,796 KB
testcase_31 AC 67 ms
34,740 KB
testcase_32 AC 67 ms
34,796 KB
testcase_33 AC 68 ms
35,028 KB
testcase_34 AC 65 ms
34,708 KB
testcase_35 AC 5 ms
5,136 KB
testcase_36 AC 21 ms
12,972 KB
testcase_37 AC 5 ms
4,596 KB
testcase_38 AC 17 ms
10,508 KB
testcase_39 AC 32 ms
17,548 KB
testcase_40 AC 11 ms
7,896 KB
testcase_41 AC 48 ms
26,292 KB
testcase_42 AC 11 ms
7,792 KB
testcase_43 AC 45 ms
24,036 KB
testcase_44 AC 32 ms
18,372 KB
testcase_45 AC 68 ms
34,800 KB
testcase_46 AC 66 ms
34,860 KB
testcase_47 AC 67 ms
34,860 KB
testcase_48 AC 67 ms
34,844 KB
testcase_49 AC 68 ms
34,704 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);
        }
      }
    }
    for (int i = 0; i < M; i++){
      dp1[v][i + 1] = max(dp1[v][i + 1], dp1[v][i]);
    }
  }
  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);
        }
      }
    }
    for (int i = M - 1; i >= 0; i--){
      dp2[v][i] = max(dp2[v][i], dp2[v][i + 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