#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,m,n) for(int i=m; i<=n; i++)
#define reb(i,m,n) for(int i=m; i>=n; i--)
#define iter(id, v) for (auto id : v)
#define ii pair<int,int>
#define fs first
#define se second
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
#define el cout <<"\n"
template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B> &v) { out << "(" << v.fs <<"," << v.se << ") "; return out; }
//#ifndef ONLINE_JUDGE
// #include "debug.h"
//#else
// #define deb(...) 23
// #define ____
//#endif
mt19937 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const ll N = 5e4 + 7;
const ll Mod = 1e9 + 7;
const ll bse = 137;
const ll szBL = 650;
const ll INF = 2e9;
int n, m;
int a[N];
int nChild[N];
bool dd[N];
vector<int> ke[N];
ll hshdown[N], hshup[N], par[N], pw[N], dist[N];
unordered_map<ll, bool> S[N];
ll mxHigh = 0;
bool res = 0;
void calc_dfs (ll u, ll pa) {
nChild[u] = 1;
iter (&v, ke[u]) {
if (v != pa && !dd[v]) {
calc_dfs(v, u);
nChild[u] += nChild[v];
}
}
}
ll get_cen (ll u, ll pa, ll sz_Tree) {
iter (&v, ke[u]) {
if (v != pa && !dd[v] && nChild[v] > sz_Tree / 2) {
return get_cen(v, u, sz_Tree);
}
}
return u;
}
void dfs1 (ll u, ll pa, ll centroid, ll Len) {
dist[u] = dist[pa] + 1;
mxHigh = max(mxHigh, dist[u]);
if (dist[u] > Len || res)
return;
hshup[u] = (a[u] * pw[dist[u] - 2] % Mod + hshup[pa]) % Mod;
hshdown[u] = (hshdown[pa] * bse % Mod + a[u]) % Mod;
ll curHsh = (hshup[u] * pw[Len - dist[u] + 1] % Mod - hshdown[u] + Mod) % Mod;
if (S[Len - dist[u] + 1].find(curHsh) != S[Len - dist[u] + 1].end()) {
res = 1;
return;
}
iter (&v, ke[u]) {
if (v == pa || dd[v])
continue;
dfs1(v, u, centroid, Len);
}
}
void dfs2 (ll u, ll pa, ll centroid, ll Len) {
if (dist[u] > Len || res)
return;
ll curHsh = (hshup[u] * pw[Len - dist[u] + 1] % Mod - hshdown[u] + Mod) % Mod;
S[dist[u]][curHsh] = 1;
iter (&v, ke[u]) {
if (v == pa || dd[v])
continue;
dfs2(v, u, centroid, Len);
}
}
void decompose (ll u, ll Len) {
if (res)
return;
calc_dfs(u, 0);
ll centroid = get_cen(u, 0, nChild[u]);
dd[centroid] = 1;
mxHigh = 0;
hshup[centroid] = 0;
hshdown[centroid] = a[centroid];
dist[centroid] = 1;
ll curHsh = (hshup[centroid] * pw[Len] % Mod - hshdown[centroid] + Mod) % Mod;
S[1][curHsh] = 1;
iter (&v, ke[centroid]) {
if (!dd[v]) {
dfs1(v, centroid, centroid, Len);
dfs2(v, centroid, centroid, Len);
}
}
rep (i, 1, mxHigh) S[i].clear();
iter (&v, ke[centroid]) {
if (!dd[v]) {
decompose(v, Len);
}
}
}
bool check (ll Len) {
res = 0;
rep (i, 1, n) {
dd[i] = 0;
}
decompose(1, Len);
return res;
}
ll BS (ll l, ll r) {
while (l < r) {
ll mid = l + r + 1 >> 1;
if (check(mid * 2)) l = mid;
else r = mid - 1;
}
return l * 2;
}
ll BS1 (ll l, ll r) {
while (l < r) {
ll mid = l + r + 1 >> 1;
if (check(mid * 2 + 1)) l = mid;
else r = mid - 1;
}
return l * 2 + 1;
}
void chloe_solution() {
cin >> n;
string s;
cin >> s;
rep (i, 1, n) {
a[i] = s[i - 1] - '`';
}
rep (i, 2, n) {
ll u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
pw[0] = 1;
rep (i, 1, n) {
pw[i] = (pw[i - 1] * bse) % Mod;
}
cout << max(BS(0, n / 2), BS1(0, (n - 1) / 2));
}
int main() {
file("lampice");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
chloe_solution();
}
/*
10
bcaabbbbac
2 1
3 1
4 1
5 2
6 5
7 2
8 5
9 4
10 8
*/
Compilation message
lampice.cpp: In function 'long long int BS(long long int, long long int)':
lampice.cpp:143:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
143 | ll mid = l + r + 1 >> 1;
| ~~~~~~^~~
lampice.cpp: In function 'long long int BS1(long long int, long long int)':
lampice.cpp:152:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
152 | ll mid = l + r + 1 >> 1;
| ~~~~~~^~~
lampice.cpp: In function 'int main()':
lampice.cpp:20:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | #define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:181:6: note: in expansion of macro 'file'
181 | file("lampice");
| ^~~~
lampice.cpp:20:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | #define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:181:6: note: in expansion of macro 'file'
181 | file("lampice");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |