結果

問題 No.1494 LCS on Tree
ユーザー chocoruskchocorusk
提出日時 2021-04-30 22:39:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,088 ms / 2,000 ms
コード長 2,710 bytes
コンパイル時間 3,818 ms
コンパイル使用メモリ 194,424 KB
実行使用メモリ 67,172 KB
最終ジャッジ日時 2023-08-12 22:40:29
合計ジャッジ時間 20,847 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,644 KB
testcase_01 AC 2 ms
5,512 KB
testcase_02 AC 2 ms
5,604 KB
testcase_03 AC 388 ms
67,016 KB
testcase_04 AC 413 ms
55,280 KB
testcase_05 AC 415 ms
55,188 KB
testcase_06 AC 413 ms
55,076 KB
testcase_07 AC 414 ms
55,332 KB
testcase_08 AC 413 ms
55,300 KB
testcase_09 AC 413 ms
55,340 KB
testcase_10 AC 414 ms
55,248 KB
testcase_11 AC 414 ms
55,316 KB
testcase_12 AC 411 ms
55,496 KB
testcase_13 AC 413 ms
55,380 KB
testcase_14 AC 386 ms
66,980 KB
testcase_15 AC 384 ms
67,160 KB
testcase_16 AC 387 ms
66,888 KB
testcase_17 AC 386 ms
66,868 KB
testcase_18 AC 386 ms
66,872 KB
testcase_19 AC 2 ms
5,552 KB
testcase_20 AC 2 ms
5,740 KB
testcase_21 AC 2 ms
5,504 KB
testcase_22 AC 2 ms
5,508 KB
testcase_23 AC 2 ms
5,568 KB
testcase_24 AC 2 ms
5,784 KB
testcase_25 AC 2 ms
5,440 KB
testcase_26 AC 2 ms
5,488 KB
testcase_27 AC 2 ms
5,604 KB
testcase_28 AC 2 ms
5,516 KB
testcase_29 AC 2 ms
5,416 KB
testcase_30 AC 385 ms
67,172 KB
testcase_31 AC 387 ms
66,832 KB
testcase_32 AC 387 ms
67,064 KB
testcase_33 AC 387 ms
66,772 KB
testcase_34 AC 391 ms
66,852 KB
testcase_35 AC 29 ms
11,844 KB
testcase_36 AC 144 ms
37,872 KB
testcase_37 AC 24 ms
8,712 KB
testcase_38 AC 99 ms
17,344 KB
testcase_39 AC 208 ms
43,428 KB
testcase_40 AC 69 ms
17,916 KB
testcase_41 AC 308 ms
44,364 KB
testcase_42 AC 62 ms
12,916 KB
testcase_43 AC 286 ms
47,580 KB
testcase_44 AC 217 ms
42,428 KB
testcase_45 AC 1,086 ms
36,220 KB
testcase_46 AC 1,088 ms
36,132 KB
testcase_47 AC 1,077 ms
36,104 KB
testcase_48 AC 1,082 ms
36,112 KB
testcase_49 AC 1,087 ms
36,436 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