結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-03 05:43:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,016 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 219,632 KB
実行使用メモリ 66,552 KB
最終ジャッジ日時 2023-08-25 20:05:45
合計ジャッジ時間 15,914 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 RE -
testcase_28 AC 1 ms
4,376 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 AC 81 ms
65,892 KB
testcase_46 AC 81 ms
66,152 KB
testcase_47 AC 83 ms
65,924 KB
testcase_48 AC 81 ms
65,964 KB
testcase_49 AC 81 ms
65,896 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]);
    assert(ans == 2);
}
0