結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-03 05:37:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,741 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 221,360 KB
実行使用メモリ 66,432 KB
最終ジャッジ日時 2024-04-30 17:39:02
合計ジャッジ時間 5,536 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 76 ms
66,304 KB
testcase_04 AC 74 ms
66,176 KB
testcase_05 AC 75 ms
66,304 KB
testcase_06 AC 74 ms
66,304 KB
testcase_07 AC 73 ms
66,176 KB
testcase_08 AC 75 ms
66,304 KB
testcase_09 AC 75 ms
66,176 KB
testcase_10 AC 80 ms
66,304 KB
testcase_11 AC 75 ms
66,304 KB
testcase_12 AC 75 ms
66,304 KB
testcase_13 AC 73 ms
66,176 KB
testcase_14 AC 75 ms
66,432 KB
testcase_15 AC 73 ms
66,304 KB
testcase_16 AC 72 ms
66,176 KB
testcase_17 AC 73 ms
66,304 KB
testcase_18 AC 73 ms
66,176 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 74 ms
66,432 KB
testcase_31 AC 77 ms
66,176 KB
testcase_32 AC 76 ms
66,304 KB
testcase_33 AC 75 ms
66,176 KB
testcase_34 AC 73 ms
66,176 KB
testcase_35 AC 6 ms
6,944 KB
testcase_36 AC 23 ms
22,016 KB
testcase_37 AC 5 ms
6,940 KB
testcase_38 AC 17 ms
17,792 KB
testcase_39 AC 33 ms
31,872 KB
testcase_40 AC 11 ms
12,672 KB
testcase_41 AC 53 ms
49,664 KB
testcase_42 AC 12 ms
12,288 KB
testcase_43 AC 47 ms
44,800 KB
testcase_44 AC 35 ms
33,792 KB
testcase_45 AC 73 ms
66,304 KB
testcase_46 AC 72 ms
66,304 KB
testcase_47 AC 78 ms
66,304 KB
testcase_48 AC 75 ms
66,176 KB
testcase_49 AC 75 ms
66,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;
using std::vector;
using std::string;
#define rep(i, j, n) for(int i = int(j); i < int(n); i++)
#define REP(i, j, n) for(int i = int(j); i <= int(n); i++)
#define per(i, j, n) for(int i = int(j); int(n) <= i; i--)
ll N, M;
string S;
vector<vector<ll>> edge, dp, rev;
vector<vector<char>> c;
vector<bool> flag;
ll ans = 0;

void dfs(ll now){
    flag[now] = true;
    rep(i,0,edge[now].size()){
        ll next = edge[now][i];
        ll ch = c[now][i];
        if(flag[next]) continue;
        dfs(next);
        per(j,M,1){
            dp[next][j] = std::max(dp[next][j], dp[next][j-1]+(S[j-1]==ch));
        }
        REP(j,1,M){
            dp[next][j] = std::max(dp[next][j], dp[next][j-1]);
        }
        REP(j,1,M){
            rev[next][j-1] = std::max(rev[next][j-1], rev[next][j]+(S[j-1]==ch));
        }
        per(j,M,1){
            rev[next][j-1] = std::max(rev[next][j-1], rev[next][j]);
        }
        REP(j,0,M){
            ans = std::max(ans, dp[now][j]+rev[next][j]);
            ans = std::max(ans, dp[next][j]+rev[now][j]);
        }
        REP(j,0,M){
            dp[now][j] = std::max(dp[now][j], dp[next][j]);
            rev[now][j] = std::max(rev[now][j], rev[next][j]);
        }
    }
}

int main(){
    cin >> N >> S;
    M = S.size();
    edge.resize(N);
    c.resize(N);
    rep(i,1,N){
        ll u, v;
        char c_;
        cin >> u >> v >> c_;
        edge[--u].push_back(--v);
        edge[v].push_back(u);
        c[u].push_back(c_);
        c[v].push_back(c_);
    }
    dp.resize(N, vector<ll>(M+1));
    rev.resize(N, vector<ll>(M+1));
    flag.resize(N);
    dfs(0);
    cout << ans << endl;
}
0