結果

問題 No.1494 LCS on Tree
ユーザー parukiparuki
提出日時 2021-05-03 22:11:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,635 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 213,084 KB
実行使用メモリ 129,096 KB
最終ジャッジ日時 2023-09-29 18:32:25
合計ジャッジ時間 19,380 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 489 ms
128,696 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 489 ms
128,700 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 486 ms
128,708 KB
testcase_12 WA -
testcase_13 AC 492 ms
128,688 KB
testcase_14 AC 496 ms
128,960 KB
testcase_15 AC 497 ms
129,012 KB
testcase_16 AC 497 ms
128,712 KB
testcase_17 AC 497 ms
128,752 KB
testcase_18 AC 500 ms
128,760 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 141 ms
40,000 KB
testcase_37 WA -
testcase_38 AC 96 ms
32,620 KB
testcase_39 AC 218 ms
60,292 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 234 ms
64,072 KB
testcase_45 AC 346 ms
128,696 KB
testcase_46 AC 349 ms
128,704 KB
testcase_47 AC 349 ms
128,716 KB
testcase_48 AC 348 ms
128,712 KB
testcase_49 AC 352 ms
128,756 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>, 2>;

using arr = array<pii, 2>;
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(-1000, -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, 2)rep(k, 2) {
                pii L = toL[sz(S) - i][v][j];
                pii R = toR[i][v][k];
                if (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