結果

問題 No.1494 LCS on Tree
ユーザー chocoruskchocorusk
提出日時 2021-04-30 22:39:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 991 ms / 2,000 ms
コード長 2,710 bytes
コンパイル時間 3,266 ms
コンパイル使用メモリ 196,568 KB
実行使用メモリ 67,152 KB
最終ジャッジ日時 2024-04-30 17:41:44
合計ジャッジ時間 18,694 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 363 ms
67,152 KB
testcase_04 AC 382 ms
55,008 KB
testcase_05 AC 393 ms
55,768 KB
testcase_06 AC 382 ms
55,136 KB
testcase_07 AC 384 ms
55,264 KB
testcase_08 AC 383 ms
55,644 KB
testcase_09 AC 382 ms
55,128 KB
testcase_10 AC 385 ms
55,008 KB
testcase_11 AC 388 ms
55,392 KB
testcase_12 AC 375 ms
55,384 KB
testcase_13 AC 381 ms
55,900 KB
testcase_14 AC 387 ms
67,020 KB
testcase_15 AC 368 ms
67,028 KB
testcase_16 AC 367 ms
67,152 KB
testcase_17 AC 353 ms
67,152 KB
testcase_18 AC 358 ms
67,020 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,948 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 365 ms
67,028 KB
testcase_31 AC 385 ms
67,024 KB
testcase_32 AC 382 ms
67,020 KB
testcase_33 AC 385 ms
67,152 KB
testcase_34 AC 393 ms
67,128 KB
testcase_35 AC 26 ms
11,672 KB
testcase_36 AC 135 ms
37,708 KB
testcase_37 AC 24 ms
8,592 KB
testcase_38 AC 95 ms
17,320 KB
testcase_39 AC 200 ms
43,212 KB
testcase_40 AC 64 ms
17,700 KB
testcase_41 AC 278 ms
43,724 KB
testcase_42 AC 56 ms
13,216 KB
testcase_43 AC 254 ms
47,184 KB
testcase_44 AC 196 ms
42,056 KB
testcase_45 AC 974 ms
36,836 KB
testcase_46 AC 991 ms
35,816 KB
testcase_47 AC 989 ms
35,816 KB
testcase_48 AC 976 ms
36,840 KB
testcase_49 AC 988 ms
36,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
int n, m;
string s;
vector<P> g[2020];
int a[2020], b[2020];
char c[2020];
int dp[2][2020][2020];
int par[2020];
void dfs(int x, int p){
    for(auto q:g[x]){
        int y=q.first;
        if(y==p) continue;
        par[y]=q.second;
        dfs(y, x);
        for(int i=0; i<=m; i++){
            for(int j=0; j<2; j++){
                dp[j][x][i]=max(dp[j][x][i], dp[j][y][i]);
                if(i) dp[j][x][i]=max(dp[j][x][i], dp[j][x][i-1]);
            }
            if(i && s[i-1]==c[q.second]) dp[0][x][i]=max(dp[0][x][i], dp[0][y][i-1]+1);
            if(i && s[m-i]==c[q.second]) dp[1][x][i]=max(dp[1][x][i], dp[1][y][i-1]+1);
        }
    }
}
int dp1[2][2020][2020];
int main()
{
    cin>>n>>s;
    m=s.size();
    for(int i=0; i<n-1; i++){
        cin>>a[i]>>b[i];
        cin>>c[i];
        a[i]--; b[i]--;
        g[a[i]].push_back({b[i],i});
        g[b[i]].push_back({a[i],i});
    }
    dfs(0, -1);
    int ans=max(dp[0][0][m], dp[1][0][m]);
    for(int i=1; i<n; i++){
        for(int j=0; j<=m; j++){
            for(int k=0; k<2; k++){
                dp1[k][i][j]=max(dp1[k][i][j], dp[k][i][j]);
                if(j) dp1[k][i][j]=max(dp1[k][i][j], dp1[k][i][j-1]);
            }
            if(j && c[par[i]]==s[j-1]){
                dp1[0][i][j]=max(dp1[0][i][j], dp[0][i][j-1]+1);
            }
            if(j && c[par[i]]==s[m-j]){
                dp1[1][i][j]=max(dp1[1][i][j], dp[1][i][j-1]+1);
            }
        }
    }
    for(int x=0; x<n; x++){
        for(int j=0; j<=m; j++){
            multiset<int> st;
            for(auto q:g[x]){
                int y=q.first;
                if(x>0 && q.second==par[x]) continue;
                st.insert(dp1[0][y][j]);
            }
            for(auto q:g[x]){
                int y=q.first;
                if(x>0 && q.second==par[x]) continue;
                st.erase(st.lower_bound(dp1[0][y][j]));
                if(!st.empty()){
                    ans=max(ans, dp1[1][y][m-j]+*st.rbegin());
                }
                st.insert(dp1[0][y][j]);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
0