#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int N = 5e4 + 10;
const int mod = 1e9 + 7;
const int base = 29;
int n;
char c[N];
int big[N], sz[N], del[N];
vector <int> g[N];
void dfs(int u, int p) {
big[u] = 0, sz[u] = 1;
for (auto v: g[u]) {
if (v == p || del[v]) continue;
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > sz[big[u]]) big[u] = v;
}
}
int centroid(int u) {
int num = sz[u] / 2;
while (sz[big[u]] > num) u = big[u];
return u;
}
// 2len + 2
int H[N], power[N];
int rH[N];
int get_hash(int L, int R) {
return (0LL + H[R] - 1LL * H[L - 1] * power[R - L + 1] + 1LL * mod * mod) % mod;
}
int check(int u, int len) {
dfs(u, 0);
u = centroid(u);
del[u] = 1;
// cout << u << ' ' << len << '\n';
H[1] = c[u] - 'a' + 1;
unordered_set <int> s;
function <void(int, int, int)> add = [&](int u, int p, int d) {
if (d > len) return;
// cout << "add: " << u << ' ' << p << ' ' << d << '\n';
H[d] = (1LL * H[d - 1] * base + c[u] - 'a' + 1) % mod;
s.insert(H[d]);
// if (u == 3) cout << H[d] << "aa\n";
for (auto v: g[u]) {
if (v == p || del[v]) continue;
add(v, u, d + 1);
}
};
rH[1] = c[u] - 'a' + 1;
s.insert(H[1]);
int ok = 0;
function <void(int, int, int)> calc = [&](int u, int p, int d) {
if (d > len || ok) return;
H[d] = (1LL * H[d - 1] * base + c[u] - 'a' + 1) % mod;
int rem = len - d + 1;
rH[d] = (1LL * power[d - 1] * (c[u] - 'a' + 1) + rH[d - 1]) % mod;
// len - d + 1 ... d - (len - d + 1) + 1 ... len - d + 1
// 2 * d - len
if (d * 2 - len > 0 && rH[d * 2 - len] == H[d * 2 - len]) {
if (s.find(get_hash(d * 2 - len, d)) != s.end()) {
ok = 1;
return;
}
}
for (auto v: g[u]) {
if (v == p || del[v]) continue;
calc(v, u, d + 1);
}
};
for (auto v: g[u]) {
if (del[v]) continue;
calc(v, u, 2);
if (ok) return 1;
add(v, u, 2);
}
s.clear();
reverse(g[u].begin(), g[u].end());
for (auto v: g[u]) {
if (del[v]) continue;
calc(v, u, 2);
if (ok) return 1;
add(v, u, 2);
}
for (auto v: g[u]) {
if (del[v]) continue;
if (check(v, len)) return 1;
}
return 0;
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> c[i];
power[0] = 1;
for (int i = 1; i <= n; ++i) power[i] = 1LL * power[i - 1] * base % mod;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
// cout << check(1, 3);
int ans = 0;
int L = 0, R = n / 2;
while (R - L > 1) {
int mid = L + R >> 1;
memset(del, 0, sizeof(del));
if (check(1, mid * 2)) L = mid;
else R = mid;
}
memset(del, 0, sizeof(del));
if (check(1, R * 2)) ans = R * 2;
else ans = L * 2;
if (!ans) ans = 1;
L = 1, R = n / 2 + 1;
while (R - L > 1) {
int mid = L + R >> 1;
memset(del, 0, sizeof(del));
if (check(1, mid * 2 - 1)) L = mid;
else R = mid;
}
memset(del, 0, sizeof(del));
if (check(1, R * 2 - 1)) ans = max(ans, R * 2 - 1);
else ans = max(ans, L * 2 - 1);
cout << ans;
}
Compilation message
lampice.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("O3")
|
lampice.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization ("unroll-loops")
|
lampice.cpp: In lambda function:
lampice.cpp:66:13: warning: unused variable 'rem' [-Wunused-variable]
66 | int rem = len - d + 1;
| ^~~
lampice.cpp: In function 'int main()':
lampice.cpp:118:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
118 | int mid = L + R >> 1;
| ~~^~~
lampice.cpp:129:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
129 | int mid = L + R >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1744 KB |
Output is correct |
2 |
Correct |
14 ms |
1788 KB |
Output is correct |
3 |
Correct |
52 ms |
1900 KB |
Output is correct |
4 |
Correct |
59 ms |
1892 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
1 ms |
1616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2401 ms |
11728 KB |
Output is correct |
2 |
Correct |
2388 ms |
11960 KB |
Output is correct |
3 |
Correct |
1843 ms |
12180 KB |
Output is correct |
4 |
Correct |
2219 ms |
12624 KB |
Output is correct |
5 |
Correct |
2927 ms |
13076 KB |
Output is correct |
6 |
Correct |
274 ms |
10696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4075 ms |
9288 KB |
Output is correct |
2 |
Correct |
4663 ms |
8724 KB |
Output is correct |
3 |
Correct |
4339 ms |
9308 KB |
Output is correct |
4 |
Correct |
4109 ms |
7820 KB |
Output is correct |
5 |
Correct |
3346 ms |
8072 KB |
Output is correct |
6 |
Correct |
3212 ms |
7500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1744 KB |
Output is correct |
2 |
Correct |
14 ms |
1788 KB |
Output is correct |
3 |
Correct |
52 ms |
1900 KB |
Output is correct |
4 |
Correct |
59 ms |
1892 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
1 ms |
1616 KB |
Output is correct |
8 |
Correct |
2401 ms |
11728 KB |
Output is correct |
9 |
Correct |
2388 ms |
11960 KB |
Output is correct |
10 |
Correct |
1843 ms |
12180 KB |
Output is correct |
11 |
Correct |
2219 ms |
12624 KB |
Output is correct |
12 |
Correct |
2927 ms |
13076 KB |
Output is correct |
13 |
Correct |
274 ms |
10696 KB |
Output is correct |
14 |
Correct |
4075 ms |
9288 KB |
Output is correct |
15 |
Correct |
4663 ms |
8724 KB |
Output is correct |
16 |
Correct |
4339 ms |
9308 KB |
Output is correct |
17 |
Correct |
4109 ms |
7820 KB |
Output is correct |
18 |
Correct |
3346 ms |
8072 KB |
Output is correct |
19 |
Correct |
3212 ms |
7500 KB |
Output is correct |
20 |
Correct |
2320 ms |
5620 KB |
Output is correct |
21 |
Correct |
2756 ms |
6868 KB |
Output is correct |
22 |
Correct |
3880 ms |
8388 KB |
Output is correct |
23 |
Correct |
968 ms |
4680 KB |
Output is correct |
24 |
Correct |
3248 ms |
7112 KB |
Output is correct |
25 |
Correct |
3045 ms |
6240 KB |
Output is correct |
26 |
Correct |
4113 ms |
8680 KB |
Output is correct |
27 |
Correct |
4141 ms |
8952 KB |
Output is correct |
28 |
Correct |
2538 ms |
4816 KB |
Output is correct |
29 |
Correct |
2672 ms |
4800 KB |
Output is correct |
30 |
Correct |
3049 ms |
7312 KB |
Output is correct |
31 |
Correct |
2859 ms |
5616 KB |
Output is correct |
32 |
Correct |
2500 ms |
8928 KB |
Output is correct |
33 |
Correct |
2231 ms |
7048 KB |
Output is correct |