#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<long long, long long> hash_t;
hash_t operator+(hash_t a, hash_t b) { return {a.x + b.x, a.y + b.y}; }
hash_t operator*(hash_t a, hash_t b) { return {a.x * b.x, a.y * b.y}; }
hash_t operator+(hash_t a, char b) { return {a.x + b, a.y + b}; }
hash_t operator*(hash_t a, char b) { return {a.x * b, a.y * b}; }
const int MAXN = 100100;
const hash_t BASE = {31337, 10007};
int n;
char input[MAXN];
vector<int> adj[MAXN];
hash_t P[MAXN];
int ans = 1;
void rec(int x, int prev = -1, hash_t down = {0, 0}, hash_t up = {0, 0}, int len = 1) {
down = down * BASE + input[x];
up = up + P[len - 1] * input[x];
if (up == down && len > ans)
ans = len;
for (int y : adj[x]) {
if (y == prev) continue;
rec(y, x, down, up, len + 1);
}
}
int main(void) {
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);
}
P[0] = {1, 1};
for (int i = 1; i < n; ++i)
P[i] = P[i - 1] * BASE;
for (int i = 0; i < n; ++i)
rec(i);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
21 ms |
4264 KB |
Output is correct |
4 |
Correct |
55 ms |
4188 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5065 ms |
10328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5014 ms |
7508 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
6 ms |
4216 KB |
Output is correct |
3 |
Correct |
21 ms |
4264 KB |
Output is correct |
4 |
Correct |
55 ms |
4188 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Execution timed out |
5065 ms |
10328 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |