#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50010;
int n;
char input[MAXN];
vector<int> adj[MAXN];
int stk_sz = 1;
char stk[2 * MAXN];
int pivot = 0;
int ans = 1;
int manacher() {
static int rad[2 * MAXN];
int x = 0;
for (int i = 1; i < stk_sz; ++i) {
int &r = rad[i] = 0;
if (i <= x + rad[x])
r = min(rad[2 * x - i], x + rad[x] - i);
while (i - r - 1 >= 0 && i + r + 1 < stk_sz &&
stk[i - r - 1] == stk[i + r + 1]) ++r;
if (i + r >= x + rad[x]) x = i;
}
int ret = 0;
for (int i = 0; i < stk_sz; ++i)
if (rad[i] > ret)
ret = rad[i];
return ret;
}
void dfs(int x, int prev = -1) {
stk[stk_sz++] = input[x];
stk[stk_sz++] = '*';
if (adj[x].size() == 1 && x < pivot)
ans = max(ans, manacher());
for (int y : adj[x]) {
if (y == prev) continue;
dfs(y, x);
}
stk_sz -= 2;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin >> n;
cin >> input;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
stk[0] = '*';
for (int i = 0; i < n; ++i) {
if (adj[i].size() != 1)
continue;
pivot = i;
dfs(i);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
21 ms |
1628 KB |
Output is correct |
3 |
Correct |
143 ms |
1676 KB |
Output is correct |
4 |
Correct |
511 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5468 KB |
Output is correct |
2 |
Correct |
13 ms |
5468 KB |
Output is correct |
3 |
Correct |
13 ms |
5468 KB |
Output is correct |
4 |
Correct |
14 ms |
5724 KB |
Output is correct |
5 |
Correct |
13 ms |
5980 KB |
Output is correct |
6 |
Correct |
13 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
529 ms |
4376 KB |
Output is correct |
2 |
Correct |
427 ms |
3896 KB |
Output is correct |
3 |
Correct |
829 ms |
4260 KB |
Output is correct |
4 |
Correct |
527 ms |
4444 KB |
Output is correct |
5 |
Correct |
349 ms |
3920 KB |
Output is correct |
6 |
Correct |
384 ms |
3612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
21 ms |
1628 KB |
Output is correct |
3 |
Correct |
143 ms |
1676 KB |
Output is correct |
4 |
Correct |
511 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
14 ms |
5468 KB |
Output is correct |
9 |
Correct |
13 ms |
5468 KB |
Output is correct |
10 |
Correct |
13 ms |
5468 KB |
Output is correct |
11 |
Correct |
14 ms |
5724 KB |
Output is correct |
12 |
Correct |
13 ms |
5980 KB |
Output is correct |
13 |
Correct |
13 ms |
5980 KB |
Output is correct |
14 |
Correct |
529 ms |
4376 KB |
Output is correct |
15 |
Correct |
427 ms |
3896 KB |
Output is correct |
16 |
Correct |
829 ms |
4260 KB |
Output is correct |
17 |
Correct |
527 ms |
4444 KB |
Output is correct |
18 |
Correct |
349 ms |
3920 KB |
Output is correct |
19 |
Correct |
384 ms |
3612 KB |
Output is correct |
20 |
Execution timed out |
5018 ms |
2908 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |