#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 5e4+5, base = 137;
const string NAME = "";
int n,a[MAXN],sz[MAXN];
ll hsh1[MAXN],hsh2[MAXN],basepow[MAXN]={1};
bool del[MAXN],ok;
vector<int> adj[MAXN],vertices;
vector<pair<int,bool>> upd;
unordered_map<int,bool> need,vis;
void GetSize(int u, int par){
sz[u]=1;
for(int& v : adj[u])
if(v!=par&&!del[v]) GetSize(v,u), sz[u]+=sz[v];
}
int Centroid(int u, int par, const int SZ){
for(int& v : adj[u])
if(v!=par&&!del[v]&&sz[v]>SZ) return Centroid(v,u,SZ);
return u;
}
void dfs(int u, int par, const int len){
vertices.pb(u);
hsh1[sz(vertices)-1]=(hsh1[sz(vertices)-2]*base+a[u])%MOD,
hsh2[sz(vertices)-1]=(a[u]*basepow[sz(vertices)-1]+hsh2[sz(vertices)-2])%MOD;
if(!ok){
if(sz(vertices)>=(len+1)/2){
int pos=sz(vertices)-(len+1)/2-1, Start=sz(vertices)-len/2;
bool check=(pos<0);
if(pos>=0&&hsh2[pos]==(hsh1[Start+pos]-hsh1[Start-1]*basepow[pos+1]+1ll*MOD*MOD)%MOD) check=1, Start+=pos+1;
if(Start>=sz(vertices)) ok=1;
else if(Start<sz(vertices)&&check){
int val=(hsh1[sz(vertices)-1]-hsh1[Start-1]*basepow[sz(vertices)-Start]+1ll*MOD*MOD)%MOD;
ok|=vis[val], upd.pb(val,0);
}
}
if(sz(vertices)<=len/2+1){
int val=(hsh1[sz(vertices)-1]-hsh1[0]*basepow[sz(vertices)-1]+1ll*MOD*MOD)%MOD;
ok|=need[val], upd.pb(val,1);
}
}
if(sz(vertices)<len)
for(int& v : adj[u])
if(v!=par&&!del[v]&&!ok) dfs(v,u,len);
vertices.pob();
}
bool CentroidDecompostion(int u, const int len){
GetSize(u,0);
int root=Centroid(u,0,sz[u]>>1);
hsh1[0]=hsh2[0]=a[root];
vertices.pb(u), need.clear(), vis.clear(), ok=0;
for(int& v : adj[root])
if(!del[v]&&!ok){
dfs(v,root,len);
while(!upd.empty()){
if(upd.back().se==0) need[upd.back().fi]=1;
else vis[upd.back().fi]=1;
upd.pob();
}
}
vertices.pob(), del[root]=1;
if(ok) return 1;
for(int& v : adj[root])
if(!del[v]&&CentroidDecompostion(v,len)) return 1;
return 0;
}
inline int bs(int l, int r, int odd){
int rs=1;
while(l<=r){
int mid=(l+r)>>1;
memset(del,0,sizeof(del));
if(CentroidDecompostion(1,mid*2+odd)) rs=mid*2+odd, l=mid+1;
else r=mid-1;
}
return rs;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
basepow[i]=basepow[i-1]*base%MOD;
for(int i = 1; i<=n; ++i){
char c;
cin >> c;
a[i]=c-'a'+1;
}
for(int i = 1; i<n; ++i){
int x,y;
cin >> x >> y;
adj[x].pb(y), adj[y].pb(x);
}
cout << max(bs(1,n/2,0),bs(1,(n-1)/2,1));
}
Compilation message (stderr)
lampice.cpp: In function 'int main()':
lampice.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:96:45: note: in expansion of macro 'fo'
96 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
lampice.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:96:45: note: in expansion of macro 'fo'
96 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |