結果

問題 No.1494 LCS on Tree
ユーザー parukiparuki
提出日時 2021-05-03 22:17:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,672 bytes
コンパイル時間 2,540 ms
コンパイル使用メモリ 215,128 KB
実行使用メモリ 254,336 KB
最終ジャッジ日時 2024-04-30 17:50:04
合計ジャッジ時間 23,615 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 WA -
testcase_04 AC 751 ms
253,952 KB
testcase_05 AC 752 ms
253,952 KB
testcase_06 AC 754 ms
253,952 KB
testcase_07 AC 746 ms
253,952 KB
testcase_08 AC 759 ms
253,824 KB
testcase_09 AC 766 ms
253,952 KB
testcase_10 AC 757 ms
253,952 KB
testcase_11 AC 756 ms
253,824 KB
testcase_12 AC 757 ms
253,952 KB
testcase_13 AC 772 ms
253,952 KB
testcase_14 AC 633 ms
254,080 KB
testcase_15 AC 635 ms
254,080 KB
testcase_16 AC 632 ms
253,952 KB
testcase_17 AC 633 ms
253,824 KB
testcase_18 AC 641 ms
253,952 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 730 ms
254,336 KB
testcase_31 AC 754 ms
254,208 KB
testcase_32 AC 758 ms
254,080 KB
testcase_33 AC 755 ms
254,080 KB
testcase_34 AC 735 ms
254,208 KB
testcase_35 AC 34 ms
16,768 KB
testcase_36 AC 222 ms
76,800 KB
testcase_37 AC 31 ms
16,768 KB
testcase_38 AC 157 ms
61,696 KB
testcase_39 AC 336 ms
116,736 KB
testcase_40 AC 98 ms
40,192 KB
testcase_41 AC 547 ms
187,648 KB
testcase_42 AC 92 ms
39,296 KB
testcase_43 AC 482 ms
168,704 KB
testcase_44 AC 363 ms
124,800 KB
testcase_45 AC 545 ms
253,952 KB
testcase_46 AC 556 ms
253,952 KB
testcase_47 AC 555 ms
253,952 KB
testcase_48 AC 556 ms
253,952 KB
testcase_49 AC 567 ms
253,952 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>;

template<class T, class Cmp, int SIZE>
void  setTopK(array<T, SIZE> &A, T x) {
    for (int i = 0; i < SIZE; ++i) {
        if (Cmp()(x, A[i])) {
            copy(A.begin() + i, A.end() - 1, A.begin() + i + 1);
            A[i] = x;
            break;
        }
    }
}
constexpr auto setMin = &setTopK<int, less<int>, 2>;
constexpr auto setMax = &setTopK<pii, greater<pii>, 4>;

using arr = array<pii, 4>;
using Graph = vector<vector<pair<int, char>>>;
using vva = vector<vector<arr>>;

void f(int k, int u, int p, vva &X, const string &S, const Graph &G) {
    arr& res = X[k][u];
    if (res[0].first >= 0)return;
    setMax(res, pii(0, -1));
    if (k < sz(S)) {
        f(k + 1, u, p, X, S, G);
        rep(i,2) setMax(res, X[k+1][u][i]);
    }

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

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);
    }

    const pii DEF(-10000, -1);
    vva toR(sz(S) + 1, vector<arr>(N, { DEF,DEF })), toL = toR;

    f(0, 0, -1, toR, S, G);
    reverse(all(S));
    f(0, 0, -1, toL, S, G);

    int ans = max(toR[0][0][0].first, toL[0][0][0].first);
    for (int i = 1; i < sz(S); ++i) {
        rep(v, N) {
            rep(j, 4)rep(k, 4) {
                pii L = toL[sz(S) - i][v][j];
                pii R = toR[i][v][k];
                if (L.second != -1 && R.second != -1 && L.second != R.second) {
                    smax(ans, L.first + R.first);
                }
            }
        }
    }

    cout << ans << endl;
}

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