結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 270 ms
35,416 KB
testcase_04 AC 664 ms
35,176 KB
testcase_05 AC 671 ms
35,240 KB
testcase_06 AC 677 ms
35,120 KB
testcase_07 AC 681 ms
35,056 KB
testcase_08 AC 657 ms
35,084 KB
testcase_09 AC 680 ms
35,084 KB
testcase_10 AC 660 ms
35,064 KB
testcase_11 AC 649 ms
35,124 KB
testcase_12 AC 662 ms
35,120 KB
testcase_13 AC 667 ms
35,312 KB
testcase_14 AC 194 ms
35,380 KB
testcase_15 AC 192 ms
35,272 KB
testcase_16 AC 190 ms
35,176 KB
testcase_17 AC 191 ms
35,192 KB
testcase_18 AC 191 ms
35,176 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 270 ms
35,360 KB
testcase_31 AC 272 ms
35,268 KB
testcase_32 AC 271 ms
35,272 KB
testcase_33 AC 271 ms
35,260 KB
testcase_34 AC 274 ms
35,172 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 19 ms
5,280 KB
testcase_38 AC 91 ms
11,092 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 450 ms
26,988 KB
testcase_42 AC 53 ms
8,200 KB
testcase_43 AC 392 ms
24,408 KB
testcase_44 AC 252 ms
18,828 KB
testcase_45 AC 743 ms
35,148 KB
testcase_46 AC 751 ms
35,160 KB
testcase_47 AC 741 ms
35,140 KB
testcase_48 AC 750 ms
35,140 KB
testcase_49 AC 747 ms
35,176 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, string &S, Graph& G) {
    int res = 0;
    int n = sz(S);
    // [0, i), [i, n)
    FOR(i, 1, n) {
        vector<pii> L, R;
        each(e, G[u]) if (e.first != p) {
            if (e.second == S[i - 1]) {
                L.emplace_back(toL[n - i + 1][e.first] + 1, e.first);
            } else {
                L.emplace_back(toL[n - i][e.first], e.first);
            }
            if (e.second == S[i]) {
                R.emplace_back(toR[i + 1][e.first] + 1, e.first);
            } else {
                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, S, 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);
    reverse(all(S));

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

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