結果

問題 No.1494 LCS on Tree
ユーザー 沙耶花沙耶花
提出日時 2021-04-30 22:40:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,478 bytes
コンパイル時間 4,663 ms
コンパイル使用メモリ 273,852 KB
実行使用メモリ 160,112 KB
最終ジャッジ日時 2023-09-26 07:04:05
合計ジャッジ時間 21,029 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 905 ms
157,156 KB
testcase_04 AC 213 ms
36,320 KB
testcase_05 AC 211 ms
36,324 KB
testcase_06 AC 210 ms
36,376 KB
testcase_07 AC 211 ms
36,260 KB
testcase_08 AC 210 ms
35,804 KB
testcase_09 AC 210 ms
36,068 KB
testcase_10 AC 208 ms
36,804 KB
testcase_11 AC 207 ms
35,996 KB
testcase_12 AC 209 ms
36,120 KB
testcase_13 AC 211 ms
35,920 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 889 ms
157,144 KB
testcase_31 AC 803 ms
139,068 KB
testcase_32 AC 737 ms
108,508 KB
testcase_33 AC 744 ms
104,908 KB
testcase_34 AC 737 ms
104,824 KB
testcase_35 AC 17 ms
5,404 KB
testcase_36 AC 78 ms
13,236 KB
testcase_37 AC 16 ms
5,664 KB
testcase_38 AC 52 ms
11,664 KB
testcase_39 AC 148 ms
18,840 KB
testcase_40 AC 49 ms
9,064 KB
testcase_41 AC 160 ms
27,644 KB
testcase_42 AC 34 ms
8,772 KB
testcase_43 AC 169 ms
25,848 KB
testcase_44 AC 144 ms
19,972 KB
testcase_45 AC 177 ms
34,888 KB
testcase_46 AC 178 ms
34,900 KB
testcase_47 AC 180 ms
34,880 KB
testcase_48 AC 179 ms
34,924 KB
testcase_49 AC 180 ms
34,880 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]);
			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);
	dfs(0,-1,dp,rdp);
	
	cout<<ans<<endl;
	
    return 0;
}
0