結果

問題 No.1494 LCS on Tree
ユーザー parukiparuki
提出日時 2021-05-03 17:47:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,370 bytes
コンパイル時間 2,009 ms
コンパイル使用メモリ 218,120 KB
実行使用メモリ 35,472 KB
最終ジャッジ日時 2023-09-29 14:27:58
合計ジャッジ時間 16,694 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 233 ms
35,284 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 WA -
testcase_23 WA -
testcase_24 AC 1 ms
4,376 KB
testcase_25 WA -
testcase_26 AC 1 ms
4,380 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 233 ms
35,360 KB
testcase_31 AC 244 ms
35,472 KB
testcase_32 AC 233 ms
35,192 KB
testcase_33 AC 230 ms
35,188 KB
testcase_34 AC 230 ms
35,208 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 608 ms
35,160 KB
testcase_46 AC 608 ms
35,168 KB
testcase_47 AC 607 ms
35,156 KB
testcase_48 AC 618 ms
35,164 KB
testcase_49 AC 602 ms
35,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

using Graph = vector<vector<pair<int, char>>>;
using vvi = vector<vi>;

int f(int k, int u, int p, vvi &X, const string &S, const Graph &G) {
    int& res = X[k][u];
    if (res != -1)return res;
    res = 0;
    if (k < sz(S))smax(res, f(k + 1, u, p, X, S, G));

    each(e, G[u]) {
        if (e.first != p) {
            smax(res, f(k, e.first, u, X, S, G));
            if (k < sz(S) && S[k] == e.second) {
                smax(res, f(k + 1, e.first, u, X, S, G) + 1);
            }
        }
    }
    return res;
}

int g(int u, int p, vvi& toL, vvi& toR, Graph& G) {
    int res = 0;
    int n = sz(toL) - 1;
    // [0, i), [i, n)
    FOR(i, 1, n) {
        vector<pii> L, R;
        each(e, G[u]) if (e.first != p) {
            L.emplace_back(toL[n - i][e.first], e.first);
            R.emplace_back(toR[i][e.first], e.first);
        }
        if (sz(L) < 2)break;
        sort(rbegin(L), rend(L));
        sort(rbegin(R), rend(R));
        rep(j, 2)rep(k, 2) {
            if (L[j].second != R[k].second) {
                smax(res, L[j].first + R[k].first);
            }
        }
    }
    each(e, G[u])if (e.first != p) {
        smax(res, g(e.first, u, toL, toR, G));
    }
    return res;
}

void solve() {
    int N;
    string S;
    cin >> N >> S;
    Graph G(N);
    rep(i, N - 1) {
        int u, v;
        char c;
        cin >> u >> v >> c;
        --u; --v;
        G[u].emplace_back(v, c);
        G[v].emplace_back(u, c);
    }

    vvi toR(sz(S) + 1, vi(N, -1)), toL = toR;
    f(0, 0, -1, toR, S, G);
    reverse(all(S));
    f(0, 0, -1, toL, S, G);

    int ans = smax(toR[0][0], g(0, -1, toL, toR, G));
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}
0