結果

問題 No.1494 LCS on Tree
ユーザー chocoruskchocorusk
提出日時 2021-04-30 22:35:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,640 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 194,324 KB
実行使用メモリ 67,144 KB
最終ジャッジ日時 2023-09-26 06:53:54
合計ジャッジ時間 20,831 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,660 KB
testcase_01 AC 2 ms
5,592 KB
testcase_02 AC 2 ms
5,584 KB
testcase_03 AC 370 ms
66,960 KB
testcase_04 AC 407 ms
55,208 KB
testcase_05 AC 408 ms
55,224 KB
testcase_06 AC 406 ms
55,176 KB
testcase_07 AC 406 ms
55,368 KB
testcase_08 AC 406 ms
55,608 KB
testcase_09 AC 411 ms
55,376 KB
testcase_10 AC 406 ms
55,376 KB
testcase_11 AC 408 ms
55,284 KB
testcase_12 AC 408 ms
55,648 KB
testcase_13 AC 409 ms
55,468 KB
testcase_14 AC 376 ms
66,928 KB
testcase_15 AC 372 ms
67,144 KB
testcase_16 AC 373 ms
66,908 KB
testcase_17 AC 372 ms
66,936 KB
testcase_18 AC 371 ms
66,988 KB
testcase_19 AC 2 ms
5,836 KB
testcase_20 AC 2 ms
5,556 KB
testcase_21 AC 2 ms
5,800 KB
testcase_22 AC 2 ms
5,600 KB
testcase_23 AC 2 ms
5,600 KB
testcase_24 AC 2 ms
5,696 KB
testcase_25 AC 2 ms
5,660 KB
testcase_26 AC 2 ms
5,732 KB
testcase_27 AC 2 ms
5,828 KB
testcase_28 AC 2 ms
5,544 KB
testcase_29 AC 2 ms
5,512 KB
testcase_30 AC 370 ms
67,004 KB
testcase_31 AC 372 ms
66,996 KB
testcase_32 AC 370 ms
66,952 KB
testcase_33 AC 371 ms
66,884 KB
testcase_34 AC 373 ms
66,964 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 25 ms
8,616 KB
testcase_38 AC 96 ms
17,380 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 305 ms
44,416 KB
testcase_42 AC 61 ms
13,032 KB
testcase_43 AC 278 ms
47,512 KB
testcase_44 AC 212 ms
42,464 KB
testcase_45 AC 1,136 ms
36,276 KB
testcase_46 AC 1,138 ms
36,348 KB
testcase_47 AC 1,136 ms
36,288 KB
testcase_48 AC 1,131 ms
36,224 KB
testcase_49 AC 1,135 ms
36,232 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 && 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