結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-26 00:41:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,374 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 213,492 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-17 14:18:46
合計ジャッジ時間 8,759 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,504 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 637 ms
4,376 KB
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for(ll i = ll(j); i < ll(n); i++)
#define REP(i, j, n) for(ll i = ll(j); i <= ll(n); i++)
#define per(i, j, n) for(ll i = ll(j); ll(n) <= i; i--)
#define ALL(a) (a).begin(),(a).end()
#define revALL(a) (a).rbegin(),(a).rend()
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
#define ln "\n"
using std::endl;
using std::cin;
using std::cout;
using ll = int;
using lint = __int128;
using std::vector;
using std::string;
using std::upper_bound;
using std::lower_bound;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll, ll>;
using vs = vector<int>;
using vss = vector<vs>;
using pss = std::pair<int, int>;
using vl = vector<lint>;
using vll = vector<vl>;
using pll = std::pair<lint, lint>;

//main
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::random_device rnd;
    std::mt19937 mt(rnd());
    int N; cin >> N;
    string S; cin >> S;
    vss edge(N), C(N);
    rep(i,1,N){
        int X, Y; char Z; cin >> X >> Y >> Z;
        edge[--X].pb(--Y);
        edge[Y].pb(X);
        C[X].pb(Z);
        C[Y].pb(Z);
    }
    int M = S.size();
    int ans = 0;
    int cnt = 50;
    while(cnt--){
        int i = mt()%N;
        if(edge[i].size() != 1) continue;
        cnt--;
        vs last(N, -1), c(N, -1);
        std::queue<int> que;
        que.push(i);
        last[i] = i;
        while(!que.empty()){
            int now = que.front(); que.pop();
            rep(j,0,edge[now].size()){
                int next = edge[now][j];
                if(last[next] == -1){
                    que.push(next);
                    last[next] = now;
                    c[next] = C[now][j];
                }
            }
            if(edge[now].size() == 1){
                vs dp(M+1);
                while(now != i){
                    per(j,M,1) dp[j] = std::max(dp[j], dp[j-1]+(c[now] == S[j-1]));
                    REP(j,1,M) dp[j] = std::max(dp[j], dp[j-1]);
                    now = last[now];
                }
                ans = std::max(ans, dp[M]);
            }
        }
    }
    cout << ans << ln;
}

0