結果

問題 No.1494 LCS on Tree
ユーザー tokusakuraitokusakurai
提出日時 2021-04-30 22:51:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 821 ms / 2,000 ms
コード長 3,581 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 215,732 KB
実行使用メモリ 82,912 KB
最終ジャッジ日時 2023-08-12 22:46:42
合計ジャッジ時間 24,445 ms
ジャッジサーバーID
(参考情報)
judge8 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
49,760 KB
testcase_01 AC 39 ms
49,784 KB
testcase_02 AC 39 ms
50,300 KB
testcase_03 AC 819 ms
50,740 KB
testcase_04 AC 641 ms
50,208 KB
testcase_05 AC 641 ms
50,200 KB
testcase_06 AC 639 ms
50,312 KB
testcase_07 AC 642 ms
50,208 KB
testcase_08 AC 647 ms
50,264 KB
testcase_09 AC 644 ms
50,312 KB
testcase_10 AC 640 ms
50,248 KB
testcase_11 AC 641 ms
50,304 KB
testcase_12 AC 649 ms
49,756 KB
testcase_13 AC 649 ms
50,300 KB
testcase_14 AC 812 ms
50,736 KB
testcase_15 AC 812 ms
50,788 KB
testcase_16 AC 811 ms
50,532 KB
testcase_17 AC 813 ms
50,564 KB
testcase_18 AC 812 ms
50,708 KB
testcase_19 AC 40 ms
49,892 KB
testcase_20 AC 40 ms
50,248 KB
testcase_21 AC 39 ms
50,276 KB
testcase_22 AC 39 ms
49,760 KB
testcase_23 AC 40 ms
49,760 KB
testcase_24 AC 40 ms
49,876 KB
testcase_25 AC 39 ms
50,008 KB
testcase_26 AC 40 ms
50,292 KB
testcase_27 AC 39 ms
50,356 KB
testcase_28 AC 39 ms
49,932 KB
testcase_29 AC 39 ms
49,872 KB
testcase_30 AC 820 ms
50,708 KB
testcase_31 AC 820 ms
50,476 KB
testcase_32 AC 821 ms
50,448 KB
testcase_33 AC 819 ms
50,472 KB
testcase_34 AC 820 ms
50,516 KB
testcase_35 AC 72 ms
50,276 KB
testcase_36 AC 217 ms
50,212 KB
testcase_37 AC 72 ms
50,252 KB
testcase_38 AC 178 ms
50,300 KB
testcase_39 AC 313 ms
50,200 KB
testcase_40 AC 129 ms
50,268 KB
testcase_41 AC 486 ms
50,128 KB
testcase_42 AC 127 ms
49,768 KB
testcase_43 AC 440 ms
50,212 KB
testcase_44 AC 336 ms
49,940 KB
testcase_45 AC 322 ms
82,912 KB
testcase_46 AC 320 ms
82,644 KB
testcase_47 AC 319 ms
82,660 KB
testcase_48 AC 322 ms
82,740 KB
testcase_49 AC 319 ms
82,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

vector<string> S(2);
int N, M;
vector<vector<vector<int>>> dp(2, vector<vector<int>>(2000, vector<int>(2001, 0)));
int ans = 0;

template<typename T, bool directed = false>
struct Weighted_Graph{
    struct edge{
        int to; T cost; int id;
        edge(int to, T cost, int id) : to(to), cost(cost), id(id) {}
    };

    vector<vector<edge>> es;
    const T INF_T;
    const int n;
    int m;

    Weighted_Graph(int n) : es(n), INF_T(numeric_limits<T>::max()/2), n(n), m(0) {}

    void add_edge(int from, int to, T cost){
        es[from].emplace_back(to, cost, m);
        if(!directed) es[to].emplace_back(from, cost, m);
        m++;
    }

    void dfs(int now, int pre = -1){
        each(e, es[now]){
            if(e.to != pre) dfs(e.to, now);
        }

        vector<vector<int>> L(M+1), R(M+1), C(M+1);

        rep(t, 2){
            rep2(i, 0, M){
                int k = 0;
                each(e, es[now]){
                    if(e.to == pre) continue;

                    int tmp = dp[t][e.to][i];
                    if(i > 0){
                        if(t == 0) chmax(tmp, C[i-1][k]);
                        else chmax(tmp, L[i-1][k]);
                    }
                    if(i > 0 && S[t][M-i] == e.cost){
                        chmax(tmp, dp[t][e.to][i-1]+1);
                    }

                    if(t == 0) C[i].eb(tmp);
                    else L[i].eb(tmp), R[i].eb(tmp);

                    chmax(dp[t][now][i], tmp);
                    k++;
                }

                if(i > 0) chmax(dp[t][now][i], dp[t][now][i-1]);
            }
        }

        int K = sz(L[0]);

        rep2(i, 0, M){
            rep2(j, 1, K-1){
                chmax(L[i][j], L[i][j-1]);
            }
            rep3(j, K-2, 0){
                chmax(R[i][j], R[i][j+1]);
            }
        }

        chmax(ans, dp[0][now][M]), chmax(ans, dp[1][now][M]);

        rep2(i, 0, M){
            rep(j, K){
                int x = 0;
                if(j-1 >= 0) chmax(x, L[i][j-1]);
                if(j+1 < K) chmax(x, R[i][j+1]);
                chmax(ans, C[M-i][j]+x);
            }
        }
    }

    void solve(){
        dfs(0);

        cout << ans << '\n';
        /*
        rep(i, N){
            rep(j, M+1){
                cout << dp[0][i][j] << ' ';
            }
            cout << '\n';
        }
        */
    }
};

int main(){
    cin >> N >> S[0];

    S[1] = S[0];
    reverse(all(S[1]));
    M = sz(S[0]);

    Weighted_Graph<char> G(N);

    rep(i, N-1){
        int u, v; char c; cin >> u >> v >> c; u--, v--;
        G.add_edge(u, v, c);
    }

    G.solve();
}
0