結果

問題 No.1494 LCS on Tree
ユーザー 沙耶花沙耶花
提出日時 2021-04-30 22:43:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,393 ms / 2,000 ms
コード長 2,566 bytes
コンパイル時間 4,846 ms
コンパイル使用メモリ 274,808 KB
実行使用メモリ 160,056 KB
最終ジャッジ日時 2023-08-12 22:46:17
合計ジャッジ時間 20,691 ms
ジャッジサーバーID
(参考情報)
judge11 / judge7
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 890 ms
157,164 KB
testcase_04 AC 208 ms
36,396 KB
testcase_05 AC 208 ms
36,488 KB
testcase_06 AC 208 ms
36,344 KB
testcase_07 AC 207 ms
36,204 KB
testcase_08 AC 208 ms
35,824 KB
testcase_09 AC 209 ms
36,128 KB
testcase_10 AC 208 ms
37,012 KB
testcase_11 AC 207 ms
36,056 KB
testcase_12 AC 208 ms
36,080 KB
testcase_13 AC 210 ms
35,932 KB
testcase_14 AC 1,393 ms
160,056 KB
testcase_15 AC 1,194 ms
145,848 KB
testcase_16 AC 888 ms
101,292 KB
testcase_17 AC 914 ms
122,612 KB
testcase_18 AC 929 ms
113,696 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 888 ms
157,104 KB
testcase_31 AC 798 ms
139,068 KB
testcase_32 AC 727 ms
108,428 KB
testcase_33 AC 728 ms
104,900 KB
testcase_34 AC 724 ms
104,828 KB
testcase_35 AC 17 ms
5,440 KB
testcase_36 AC 77 ms
13,496 KB
testcase_37 AC 15 ms
5,692 KB
testcase_38 AC 52 ms
11,736 KB
testcase_39 AC 148 ms
18,832 KB
testcase_40 AC 51 ms
9,196 KB
testcase_41 AC 159 ms
27,608 KB
testcase_42 AC 33 ms
8,804 KB
testcase_43 AC 169 ms
25,912 KB
testcase_44 AC 145 ms
19,884 KB
testcase_45 AC 179 ms
34,944 KB
testcase_46 AC 178 ms
35,172 KB
testcase_47 AC 177 ms
34,904 KB
testcase_48 AC 178 ms
35,012 KB
testcase_49 AC 177 ms
34,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

int N;
string S;

vector<vector<pair<int,char>>> E;

void dfs(int cur,int p,vector<vector<int>> &dp){
	dp[cur][0] = 0;
	char c = '0';
	rep(i,E[cur].size()){
		int to = E[cur][i].first;
		if(to==p){
			c = E[cur][i].second;
			continue;
		}
		dfs(to,cur,dp);
		
		rep(j,S.size()+1){
			dp[cur][j] = min(dp[cur][j],dp[to][j]);
		}
	}
	
	if(c!='0'){
		for(int i=S.size()-1;i>=0;i--){
			if(dp[cur][i]==Inf)continue;
			for(int j=dp[cur][i];j<S.size();j++){
				if(S[j]==c){
					dp[cur][i+1] = min(dp[cur][i+1],j+1);
					break;
				}
			}			
		}
	}
}



int ans = 0;

using P = pair<int,int>;
using PP = pair<P,P>;
PP op(PP a,PP b){
	PP ret;
	array<P,5> A = {a.first,a.second,b.first,b.second,make_pair(-Inf,-1)};
	
	sort(A.rbegin(),A.rend());
	
	ret.first = A[0];
	for(int i=1;i<5;i++){
		if(A[0].second!=A[i].second){
			ret.second = A[i];
			break;
		}
	}
	return ret;
}

PP e(){
	return make_pair(make_pair(-Inf,-1),make_pair(-Inf,-1));	
}

void dfs(int cur,int p,vector<vector<int>> &dp,vector<vector<int>> &rdp){
	segtree<PP,op,e> seg(S.size()+1);
	
	rep(i,E[cur].size()){
		int to = E[cur][i].first;
		if(to==p)continue;
		dfs(to,cur,dp,rdp);
	}
	
	rep(i,E[cur].size()){
		int to = E[cur][i].first;
		if(to==p)continue;
		rep(j,dp[to].size()){
			if(dp[to][j]==Inf)continue;
			auto ret = seg.get(dp[to][j]);
			ret = op(ret,make_pair(make_pair(j,to),make_pair(-Inf,-1)));
			seg.set(dp[to][j],ret);
			ans = max(ans,j);
		}
	}
	
	rep(i,E[cur].size()){
		int to = E[cur][i].first;
		if(to==p)continue;
		rep(j,dp[to].size()){
			if(rdp[to][j]==Inf)continue;
			ans = max(ans,j);
			auto ret = seg.prod(0,(int)S.size() - rdp[to][j]+1);
			if(ret.first.second != to){
				ans = max(ans,j + ret.first.first);
			}
			if(ret.second.second != to){
				ans = max(ans,j + ret.second.first);
			}
		}
	}
	
}

int main(){
	
	cin>>N>>S;
	
	E.resize(N);
	
	rep(i,N-1){
		int u,v;
		char c;
		cin>>u>>v>>c;
		u--;v--;
		
		E[u].emplace_back(v,c);
		E[v].emplace_back(u,c);
	}
	
	vector dp(N,vector<int>(S.size()+1,Inf));
	vector rdp(N,vector<int>(S.size()+1,Inf));
	
	dfs(0,-1,dp);
	/*
	rep(i,N){
		rep(j,S.size()+1){
			cout<<dp[i][j]<<',';
		}
		cout<<endl;
	}*/
	
	
	reverse(S.begin(),S.end());
	
	dfs(0,-1,rdp);
	/*
	rep(i,N){
		rep(j,S.size()+1){
			cout<<rdp[i][j]<<',';
		}
		cout<<endl;
	}*/
	
	dfs(0,-1,dp,rdp);
	
	cout<<ans<<endl;
	
    return 0;
}
0