#include <bits/stdc++.h>
#define debug cout << "fuck\n";
#define ll long long
#define ull unsigned long long
#define pii pair<long long, long long>
#define st first
#define nd second
#define file "test"
using namespace std;
const long long oo = 1e18;
const long long N = 2e5 + 5;
const long long base = 311;
const long long MOD = 2e9 + 7;
ll H[N], rH[N], pw[N], h[N], up[20][N];
vector <int> d[N], centroid;
string SS;
int n, numchild[N], s[N];
bool dead[N];
// find centroid
int get_size(int p, int u) {
int ans = 1;
for (int v: d[u]) if (v != p && !dead[v])
ans += get_size(u, v);
return ans;
}
int find_centroid(int S, int p, int u, int ma = 0) {
numchild[u] = 1;
for (int v: d[u]) if (v != p && !dead[v]) {
int get = find_centroid(S, u, v);
if (get != -1) return get;
numchild[u] += numchild[v];
ma = max(ma, numchild[v]);
}
ma = max(ma, S - numchild[u]);
return ma <= S/2 ? u: -1;
}
void FIND_CENTROID() {
queue <int> q; q.push(1);
while (q.size()) {
int u = q.front(); q.pop();
dead[u = find_centroid(get_size(0, u), 0, u)] = true;
centroid.push_back(u);
for (int v: d[u]) if (!dead[v]) q.push(v);
}
}
vector <int> save[N];
void dfs(int p, int u, int vi) {
for (int v: d[u]) if (v != p && !dead[v]) {
h[v] = h[u] + 1;
H[v] = (H[u]*base + s[v]) % MOD;
rH[v] = (rH[u] + pw[h[v]]*s[v]) % MOD;
up[0][v] = u;
for (int i = 1; (1 << i) <= h[v]; i ++)
up[i][v] = up[i - 1][up[i - 1][v]];
save[vi == 0 ? v : vi].push_back(v);
dfs(u, v, vi == 0 ? v : vi);
}
}
int kanc(int k, int u) {
int x = u;
for (int i = 19; i >= 0; i --)
if (k & 1 << i) u = up[i][u];
return u;
}
ll get(int u, int v) {return (H[v] - H[u] * pw[h[v] - h[u]] + MOD * MOD) % MOD;}
bool solve_len(int len, int u) {
// return true;
if (len <= 1) return true;
H[u] = s[u]; rH[u] = s[u]; h[u] = 0;
dfs(u, u, 0);
unordered_set <int> st, st2; st.insert(0); st2.insert(0);
// cout << "--------- " << u << " ----------\n";
for (int vi: d[u]) if (!dead[vi]) {
for (int v: save[vi]) {
// cout << v << ' ';
int mis = len - h[v] - 1; if (mis < 0) continue;
// cout << u << ' ' << v << ' ' << get(u, v) << '\n';
if (mis >= h[v]) {
if (st2.count(get(u, v))) return true;
} else {
int anc = kanc(mis, v);
if (H[anc] == rH[anc] && st.count(get(anc, v))) return true;
}
}
// cout << '\n';
for (int v: save[vi]) {
int mis = len - h[v] - 1; if (mis < 0) continue;
if (mis > h[v]) st.insert(get(u, v)); else {
int anc = kanc(mis, v);
if (H[anc] == rH[anc]) st2.insert(get(anc, v));
}
}
}
return false;
}
int solve(int len) {
for (int i = 1; i <= n; i ++) dead[i] = false;
for (int u: centroid) {
int tmp = solve_len(len, u);
dead[u] = true;
for (int v: d[u]) save[v].clear();
if (tmp) return true;
}
return false;
}
int bs(int x) {
int l = 0, r = n/2 + 1, ans = x;
while (l <= r) {
int mid = l + r >> 1;
// cout << mid*2 + x << '\n';
if (solve(2*mid + x))
ans = mid * 2 + x, l = mid + 1;
else
r = mid - 1;
}
return ans;
}
ll SOLVE_CENTROID() {
return max(bs(0), bs(1));
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen(file".inp","r",stdin); freopen(file".out","w",stdout);
// #endif
cin >> n >> SS;
// s = ' ' + s;
for (int i = 1; i <= n; i ++)
s[i] = SS[i - 1] - 'a' + 1;
pw[0] = 1;
for (int i = 1; i < N; i ++)
pw[i] = pw[i - 1]*base % MOD;
for (int i = 1, u, v; i < n; i ++) {
cin >> u >> v;
d[u].push_back(v);
d[v].push_back(u);
}
FIND_CENTROID();
cout << SOLVE_CENTROID();// << ' ' << 1;
return 0;
}
/** /\_/\
(= ._.)
/ >🍵 \>🍮
________________________
/ Brainstorming section /
/=======================/
---
===
g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 somerandomthings.cpp -o somerandomthings.exe
12780
===
**/
// Before submit: spot the visible bug by reading the code.
Compilation message
lampice.cpp: In function 'int kanc(int, int)':
lampice.cpp:75:9: warning: unused variable 'x' [-Wunused-variable]
75 | int x = u;
| ^
lampice.cpp: In function 'int bs(int)':
lampice.cpp:134:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11388 KB |
Output is correct |
2 |
Correct |
13 ms |
11480 KB |
Output is correct |
3 |
Correct |
32 ms |
11596 KB |
Output is correct |
4 |
Correct |
34 ms |
11776 KB |
Output is correct |
5 |
Correct |
7 ms |
11264 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
7 ms |
11212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1582 ms |
27940 KB |
Output is correct |
2 |
Correct |
1396 ms |
28296 KB |
Output is correct |
3 |
Correct |
1037 ms |
29904 KB |
Output is correct |
4 |
Correct |
1033 ms |
30752 KB |
Output is correct |
5 |
Correct |
1707 ms |
30416 KB |
Output is correct |
6 |
Correct |
358 ms |
30136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3821 ms |
27420 KB |
Output is correct |
2 |
Correct |
3074 ms |
26976 KB |
Output is correct |
3 |
Correct |
2995 ms |
26964 KB |
Output is correct |
4 |
Correct |
3340 ms |
26548 KB |
Output is correct |
5 |
Correct |
2323 ms |
25692 KB |
Output is correct |
6 |
Correct |
2404 ms |
24008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11388 KB |
Output is correct |
2 |
Correct |
13 ms |
11480 KB |
Output is correct |
3 |
Correct |
32 ms |
11596 KB |
Output is correct |
4 |
Correct |
34 ms |
11776 KB |
Output is correct |
5 |
Correct |
7 ms |
11264 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
7 ms |
11212 KB |
Output is correct |
8 |
Correct |
1582 ms |
27940 KB |
Output is correct |
9 |
Correct |
1396 ms |
28296 KB |
Output is correct |
10 |
Correct |
1037 ms |
29904 KB |
Output is correct |
11 |
Correct |
1033 ms |
30752 KB |
Output is correct |
12 |
Correct |
1707 ms |
30416 KB |
Output is correct |
13 |
Correct |
358 ms |
30136 KB |
Output is correct |
14 |
Correct |
3821 ms |
27420 KB |
Output is correct |
15 |
Correct |
3074 ms |
26976 KB |
Output is correct |
16 |
Correct |
2995 ms |
26964 KB |
Output is correct |
17 |
Correct |
3340 ms |
26548 KB |
Output is correct |
18 |
Correct |
2323 ms |
25692 KB |
Output is correct |
19 |
Correct |
2404 ms |
24008 KB |
Output is correct |
20 |
Correct |
1362 ms |
19740 KB |
Output is correct |
21 |
Correct |
1677 ms |
20544 KB |
Output is correct |
22 |
Correct |
2391 ms |
24580 KB |
Output is correct |
23 |
Correct |
641 ms |
18804 KB |
Output is correct |
24 |
Correct |
2346 ms |
25176 KB |
Output is correct |
25 |
Correct |
2259 ms |
24848 KB |
Output is correct |
26 |
Correct |
2929 ms |
27060 KB |
Output is correct |
27 |
Correct |
3310 ms |
25704 KB |
Output is correct |
28 |
Correct |
1676 ms |
22836 KB |
Output is correct |
29 |
Correct |
1944 ms |
23052 KB |
Output is correct |
30 |
Correct |
2394 ms |
26496 KB |
Output is correct |
31 |
Correct |
2142 ms |
24556 KB |
Output is correct |
32 |
Correct |
2120 ms |
27576 KB |
Output is correct |
33 |
Correct |
1469 ms |
22912 KB |
Output is correct |