結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-03 05:40:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,994 bytes
コンパイル時間 2,752 ms
コンパイル使用メモリ 221,796 KB
実行使用メモリ 66,432 KB
最終ジャッジ日時 2024-04-30 17:38:47
合計ジャッジ時間 6,915 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 79 ms
66,304 KB
testcase_04 AC 80 ms
66,304 KB
testcase_05 AC 82 ms
66,304 KB
testcase_06 AC 81 ms
66,304 KB
testcase_07 AC 81 ms
66,176 KB
testcase_08 AC 81 ms
66,176 KB
testcase_09 AC 79 ms
66,304 KB
testcase_10 AC 80 ms
66,304 KB
testcase_11 AC 82 ms
66,176 KB
testcase_12 AC 80 ms
66,176 KB
testcase_13 AC 82 ms
66,304 KB
testcase_14 AC 80 ms
66,432 KB
testcase_15 AC 78 ms
66,304 KB
testcase_16 AC 80 ms
66,304 KB
testcase_17 AC 78 ms
66,304 KB
testcase_18 AC 79 ms
66,304 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 80 ms
66,304 KB
testcase_31 AC 79 ms
66,304 KB
testcase_32 AC 79 ms
66,304 KB
testcase_33 AC 80 ms
66,304 KB
testcase_34 AC 80 ms
66,304 KB
testcase_35 AC 6 ms
6,940 KB
testcase_36 AC 26 ms
21,888 KB
testcase_37 AC 6 ms
6,940 KB
testcase_38 AC 20 ms
17,792 KB
testcase_39 AC 38 ms
31,744 KB
testcase_40 AC 14 ms
12,544 KB
testcase_41 AC 59 ms
49,664 KB
testcase_42 AC 14 ms
12,416 KB
testcase_43 AC 54 ms
44,800 KB
testcase_44 AC 41 ms
33,920 KB
testcase_45 AC 83 ms
66,304 KB
testcase_46 AC 80 ms
66,304 KB
testcase_47 AC 80 ms
66,304 KB
testcase_48 AC 81 ms
66,304 KB
testcase_49 AC 81 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(){
    ll max = 2000;
    cin >> N >> S;
    assert(N<=max && 2<=N && 1<=S.size() && S.size()<=max);
    rep(i,0,S.size()){
        assert('a'<=S[i] && S[i]<='z');
    }
    M = S.size();
    edge.resize(N);
    c.resize(N);
    rep(i,1,N){
        ll u, v;
        char c_;
        cin >> u >> v >> c_;
        assert(1<=u && u<v && v<=N);
        assert('a'<=c_ && c_<='z');
        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;
    rep(i,0,N) assert(flag[i]);
}
0