#include <bits/stdc++.h>
using namespace std;
#define task ""
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int MOD = 1e9 + 7;
const int MAX = 50005;
const int BASE = 256;
int n;
int len;
char cc;
int maxH;
int sz[MAX];
char a[MAX];
int pw[MAX];
bool rem[MAX];
vector<int> adj[MAX];
unordered_map<int, bool> mp[MAX];
int dfsSZ(int u, int p) {
sz[u] = 1;
for (int v: adj[u]) if (v != p && !rem[v]) {
sz[u] += dfsSZ(v, u);
}
return sz[u];
}
int findCentroid(int u, int p, int n) {
for (int v: adj[u]) if (v != p && !rem[v]) {
if ((sz[v] << 1) > n)
return findCentroid(v, u, n);
}
return u;
}
void dfs(int u, int p, bool upd, int h, int up, int dw, bool &found) {
up = (1LL * up * BASE + (a[u] - 'a' + 1)) % MOD; dw = (dw + 1LL * pw[h - 1] * (a[u] - 'a' + 1)) % MOD;
int value = (1LL * pw[len - h] * dw - up + MOD) % MOD;
if (upd) mp[h][value] = true;
else {
if (h + 1 == len && (1LL * dw * BASE + (cc - 'a' + 1)) % MOD == up) {
found = true;
return;
}
if (len > h && mp[len - h - 1].find(value) != mp[len - h - 1].end()) {
found = true;
return;
}
}
if (h < len) {
for (int v: adj[u]) if (v != p && !rem[v]) {
dfs(v, u, upd, h + 1, up, dw, found);
maxH = max(maxH, h + 1);
if (found) return;
}
}
}
void solve(int u, int p, bool &found) {
int c = findCentroid(u, p, dfsSZ(u, p)); rem[c] = true;
maxH = 1;
cc = a[c];
for (int v: adj[c]) if (v != p && !rem[v]) {
dfs(v, c, 0, 1, a[c] - 'a' + 1, 0, found);
if (found) return;
dfs(v, c, 1, 1, a[c] - 'a' + 1, 0, found);
}
for (int i = 0; i <= maxH; ++i) {
mp[i].clear();
}
for (int v: adj[c]) if (v != p && !rem[v]) {
solve(v, c, found);
if (found) return;
}
}
bool ok(int x) {
for (int i = 0; i <= n; ++i) mp[i].clear();
memset(rem, false, sizeof rem);
bool found = false; len = x;
solve(1, -1, found);
return found;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n;
cin >> (a + 1);
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
pw[0] = 1; for (int i = 1; i <= n; ++i) {
pw[i] = 1LL * pw[i - 1] * BASE % MOD;
}
int ans = 1;
for (int i = 0; i < 2; ++i) {
int l = 0, r = n / 2 + 1;
while (r - l > 1) {
int mid = l + r >> 1;
if (ok(i + 2 * mid)) l = mid; else r = mid;
}
ans = max(ans, i + 2 * l);
}
cout << ans;
return 0;
}
Compilation message
lampice.cpp: In function 'int main()':
lampice.cpp:110:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | int mid = l + r >> 1;
| ~~^~~
lampice.cpp:92:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
6 ms |
4796 KB |
Output is correct |
3 |
Correct |
22 ms |
5056 KB |
Output is correct |
4 |
Correct |
19 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
808 ms |
12616 KB |
Output is correct |
2 |
Correct |
732 ms |
13340 KB |
Output is correct |
3 |
Correct |
510 ms |
13592 KB |
Output is correct |
4 |
Correct |
652 ms |
14028 KB |
Output is correct |
5 |
Correct |
1066 ms |
14456 KB |
Output is correct |
6 |
Correct |
152 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1406 ms |
9980 KB |
Output is correct |
2 |
Correct |
1774 ms |
9936 KB |
Output is correct |
3 |
Correct |
1535 ms |
10660 KB |
Output is correct |
4 |
Correct |
1529 ms |
10240 KB |
Output is correct |
5 |
Correct |
1174 ms |
9560 KB |
Output is correct |
6 |
Correct |
1121 ms |
9248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
6 ms |
4796 KB |
Output is correct |
3 |
Correct |
22 ms |
5056 KB |
Output is correct |
4 |
Correct |
19 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
808 ms |
12616 KB |
Output is correct |
9 |
Correct |
732 ms |
13340 KB |
Output is correct |
10 |
Correct |
510 ms |
13592 KB |
Output is correct |
11 |
Correct |
652 ms |
14028 KB |
Output is correct |
12 |
Correct |
1066 ms |
14456 KB |
Output is correct |
13 |
Correct |
152 ms |
13660 KB |
Output is correct |
14 |
Correct |
1406 ms |
9980 KB |
Output is correct |
15 |
Correct |
1774 ms |
9936 KB |
Output is correct |
16 |
Correct |
1535 ms |
10660 KB |
Output is correct |
17 |
Correct |
1529 ms |
10240 KB |
Output is correct |
18 |
Correct |
1174 ms |
9560 KB |
Output is correct |
19 |
Correct |
1121 ms |
9248 KB |
Output is correct |
20 |
Correct |
908 ms |
8020 KB |
Output is correct |
21 |
Correct |
1051 ms |
8532 KB |
Output is correct |
22 |
Correct |
1520 ms |
9096 KB |
Output is correct |
23 |
Correct |
379 ms |
7252 KB |
Output is correct |
24 |
Correct |
1251 ms |
9040 KB |
Output is correct |
25 |
Correct |
1211 ms |
8276 KB |
Output is correct |
26 |
Correct |
1535 ms |
10452 KB |
Output is correct |
27 |
Correct |
1565 ms |
9160 KB |
Output is correct |
28 |
Correct |
1012 ms |
7260 KB |
Output is correct |
29 |
Correct |
1096 ms |
7308 KB |
Output is correct |
30 |
Correct |
1189 ms |
9748 KB |
Output is correct |
31 |
Correct |
1014 ms |
8232 KB |
Output is correct |
32 |
Correct |
967 ms |
10888 KB |
Output is correct |
33 |
Correct |
742 ms |
8884 KB |
Output is correct |