#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
inline bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
inline bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
const int mod = 1e9 + 9;
inline int fastPow(int a, int n){
int res = 1;
while(n){
if(n & 1)res = 1ll * res * a * mod;
a = 1ll * a * a % mod;
n >>= 1;
}
return res;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 5e4 + 5;
const int inf = 2e9;
const long long infll = 1e18;
char s[maxN];
int n;
vector<int> adj[maxN];
namespace subtask1{
bool check(){
return n <= 3000;
}
const int maxN = 3e3 + 1;
const int base = 333;
int d[maxN];
int power[maxN];
int Hash[maxN], revHash[maxN];
void dfs(int u, int p){
for(int v : adj[u]){
if(v != p){
d[v] = d[u] + 1;
Hash[v] = (1ll * Hash[u] * base + s[v]) % mod;
dfs(v, u);
}
}
}
void dfs2(int u, int p){
for(int v : adj[u]){
if(v != p){
revHash[v] = (1ll * s[v] * power[d[v] - 1] + revHash[u]) % mod;
dfs2(v, u);
}
}
}
int answer = 0;
void query(int centre){
FOR(i, 1, n)Hash[i] = 0;
d[centre] = 1;
Hash[centre] = s[centre];
revHash[centre] = s[centre];
dfs(centre, 0);
dfs2(centre, 0);
FOR(i, 1, n)if(Hash[i] == revHash[i])maximize(answer, d[i]);
}
void solve(){
power[0] = 1;
FOR(i, 1, n)power[i] = 1ll * power[i - 1] * base % mod;
FOR(i, 1, n)query(i);
cout << answer;
}
}
namespace subtask2{
bool check(){
FOR(i, 1, n){
for(int v : adj[i]){
if(abs(v - i) != 1) return false;
}
}
return true;
}
int D_odd[maxN], D_even[maxN];
void Calc_D_odd() {
int L = 1, R = 0;
for(int i = 1 ; i <= n ; i++) {
if(i > R) D_odd[i] = 0;
else D_odd[i] = min(R - i, D_odd[L + (R - i)]);
while(i - D_odd[i] - 1 > 0 && i + D_odd[i] + 1 <= n && s[i - D_odd[i] - 1] == s[i + D_odd[i] + 1]) {
D_odd[i]++;
}
if(i + D_odd[i] > R) {
R = i + D_odd[i];
L = i - D_odd[i];
}
}
}
void Calc_D_even() {
int L = 1, R = 0;
for(int i = 1 ; i < n ; i++) {
int j = i + 1;
if(j > R) D_even[i] = 0;
else D_even[i] = min(R - j + 1, D_even[L + (R - j)]);
while(i - D_even[i] > 0 && j + D_even[i] <= n && s[i - D_even[i]] == s[j + D_even[i]]) {
D_even[i]++;
}
if(i + D_even[i] > R) {
R = i + D_even[i];
L = j - D_even[i];
}
}
}
void solve(){
Calc_D_even();
Calc_D_odd();
int answer = 0;
for (int i = 1; i <= n; i++) {
answer = max(answer, 2 * D_odd[i] + 1);
answer = max(answer, 2 * D_even[i]);
}
cout << answer;
}
}
void process(){
cin >> n;
FOR(i, 1, n)cin >> s[i];
FOR(i, 2, n){
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
if(subtask1 :: check()) return subtask1 :: solve();
if(subtask2 :: check()) return subtask2 :: solve();
}
#define NAME ""
int main(){
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
// cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
Compilation message (stderr)
lampice.cpp: In function 'int main()':
lampice.cpp:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |