結果

問題 No.1494 LCS on Tree
ユーザー SSRSSSRS
提出日時 2021-04-30 22:34:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,105 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 183,248 KB
実行使用メモリ 34,992 KB
最終ジャッジ日時 2023-09-26 06:53:10
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 65 ms
34,752 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 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 68 ms
34,724 KB
testcase_31 AC 66 ms
34,800 KB
testcase_32 AC 67 ms
34,744 KB
testcase_33 AC 66 ms
34,748 KB
testcase_34 AC 66 ms
34,728 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 65 ms
34,760 KB
testcase_46 AC 65 ms
34,716 KB
testcase_47 AC 66 ms
34,804 KB
testcase_48 AC 66 ms
34,796 KB
testcase_49 AC 66 ms
34,780 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 i = 0; i < M; i++){
      dp1[v][i + 1] = max(dp1[v][i + 1], dp1[v][i]);
    }
    if (v != 0){
      for (int i = 0; i <= M; i++){
        dp1[p[v]][i] = max(dp1[p[v]][i], dp1[v][i]);
      }
      for (int i = 0; i < M; i++){
        if (S[i] == p2[v]){
          dp1[p[v]][i + 1] = max(dp1[p[v]][i + 1], dp1[v][i] + 1);
        }
      }
    }
  }
  vector<vector<int>> dp2(N, vector<int>(M + 1, 0));
  for (int v : bfs){
    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 = M; i >= 0; i--){
        dp2[p[v]][i] = max(dp2[p[v]][i], dp2[v][i]);
      }
      for (int i = M - 1; i >= 0; i--){
        if (S[i] == p2[v]){
          dp2[p[v]][i] = max(dp2[p[v]][i], dp2[v][i + 1] + 1);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= M; i++){
    ans = max(ans, dp1[0][i]);
    ans = max(ans, dp2[0][i]);
  }
  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