#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
template<typename Lhs, typename Rhs> inline void Max_self(Lhs &a, Rhs b) { a = (a > b ? a : b); }
template<typename Lhs, typename Rhs> inline void Min_self(Lhs &a, Rhs b) { a = (a < b ? a : b); }
const int N = 5e4 + 10;
const int MOD = 1e9 + 7;
const int BASE = 128;
int n;
string s;
vector<int> g[N];
int cnt_node, child[N];
bool rem[N];
int cap;
int pw[N], up[N], down[N];
vector<int> odd, even;
int res = 1;
void dfs_child(int u, int p) {
++ cnt_node;
child[u] = 1;
for(const auto& v : g[u]) {
if(v == p || rem[v]) continue;
dfs_child(v, u);
child[u] += child[v];
}
}
int dfs_centroid(int u, int p) {
for(const auto& v : g[u]) {
if(v == p || rem[v]) continue;
if(child[v] > cnt_node / 2) return dfs_centroid(v, u);
}
return u;
}
int get_hash(int l, int r) {
if(l == 0) return down[r];
return (0ll + down[r] - 1ll * down[l - 1] * pw[r - l + 1] + 1ll * MOD * MOD) % MOD;
}
bool split(int u, int p) {
cnt_node = 0; dfs_child(u, p);
u = dfs_centroid(u, p);
rem[u] = true;
down[0] = up[0] = s[u] - 'a' + 1;
unordered_set<int> stt = {};
function<void(int, int, int)> add = [&] (int u, int p, int len) {
if(len + 1 > cap) return;
down[len] = (1ll * down[len - 1] * BASE + s[u] - 'a' + 1) % MOD;
stt.insert(down[len]);
for(const auto& v : g[u]) {
if(v == p || rem[v]) continue;
add(v, u, len + 1);
}
};
bool have = false;
function<void(int, int, int)> calc = [&] (int u, int p, int len) {
if(have || len + 1 > cap) return;
down[len] = (1ll * down[len - 1] * BASE + s[u] - 'a' + 1) % MOD;
up[len] = (1ll * up[len - 1] + 1ll * (s[u] - 'a' + 1) * pw[len]) % MOD;
if(len + 1 == cap) {
if(up[len] == down[len]) {
have = true;
return;
}
}
if(2 * (len + 1) > cap) {
int use_palin = 2 * (len + 1) - cap - 1;
if(down[use_palin] == up[use_palin]) {
int hash = get_hash(use_palin, len);
if(stt.find(hash) != stt.end()) {
have = true;
return;
}
}
}
for(const auto& v : g[u]) {
if(v == p || rem[v]) continue;
calc(v, u, len + 1);
}
};
for(const auto& v : g[u]) {
if(rem[v]) continue;
calc(v, u, 1);
if(have) return true;
add(v, u, 1);
}
stt.clear();
reverse(g[u].begin(), g[u].end());
for(const auto& v : g[u]) {
if(rem[v]) continue;
calc(v, u, 1);
if(have) return true;
add(v, u, 1);
}
for(const auto& v : g[u]) {
if(rem[v]) continue;
if(split(v, u)) return true;
}
return false;
}
void solve(bool ODD) {
const auto& s = (ODD ? odd : even);
int l = 0, r = (int) s.size() - 1;
while(l <= r) {
int mid = (l + r) >> 1;
cap = s[mid];
memset(rem, false, sizeof(rem));
if(split(1, 0)) {
Max_self(res, cap);
l = mid + 1;
}
else {
r = mid - 1;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("COCI19_lampice.inp", "r")) {
freopen("COCI19_lampice.inp", "r", stdin);
freopen("COCI19_lampice.out", "w", stdout);
}
cin >> n >> s; s = ' ' + s;
for(int i = 2; i <= n; ++ i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pw[0] = 1;
for(int i = 1; i <= n; ++ i) {
pw[i] = 1ll * pw[i - 1] * BASE % MOD;
if(i & 1) {
if(i > 1) {
odd.push_back(i);
}
}
else {
even.push_back(i);
}
}
solve(0); solve(1);
cout << res;
return 0;
}
/// Code by vuavisao
Compilation message
lampice.cpp: In function 'int32_t main()':
lampice.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("COCI19_lampice.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen("COCI19_lampice.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1492 KB |
Output is correct |
2 |
Correct |
11 ms |
1620 KB |
Output is correct |
3 |
Correct |
42 ms |
1772 KB |
Output is correct |
4 |
Correct |
46 ms |
1744 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2068 ms |
10928 KB |
Output is correct |
2 |
Correct |
2073 ms |
11148 KB |
Output is correct |
3 |
Correct |
1347 ms |
11352 KB |
Output is correct |
4 |
Correct |
1658 ms |
11792 KB |
Output is correct |
5 |
Correct |
2512 ms |
12184 KB |
Output is correct |
6 |
Correct |
163 ms |
9036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3456 ms |
8852 KB |
Output is correct |
2 |
Correct |
4365 ms |
8588 KB |
Output is correct |
3 |
Correct |
3794 ms |
8968 KB |
Output is correct |
4 |
Correct |
3727 ms |
7776 KB |
Output is correct |
5 |
Correct |
3066 ms |
7696 KB |
Output is correct |
6 |
Correct |
2895 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1492 KB |
Output is correct |
2 |
Correct |
11 ms |
1620 KB |
Output is correct |
3 |
Correct |
42 ms |
1772 KB |
Output is correct |
4 |
Correct |
46 ms |
1744 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
2068 ms |
10928 KB |
Output is correct |
9 |
Correct |
2073 ms |
11148 KB |
Output is correct |
10 |
Correct |
1347 ms |
11352 KB |
Output is correct |
11 |
Correct |
1658 ms |
11792 KB |
Output is correct |
12 |
Correct |
2512 ms |
12184 KB |
Output is correct |
13 |
Correct |
163 ms |
9036 KB |
Output is correct |
14 |
Correct |
3456 ms |
8852 KB |
Output is correct |
15 |
Correct |
4365 ms |
8588 KB |
Output is correct |
16 |
Correct |
3794 ms |
8968 KB |
Output is correct |
17 |
Correct |
3727 ms |
7776 KB |
Output is correct |
18 |
Correct |
3066 ms |
7696 KB |
Output is correct |
19 |
Correct |
2895 ms |
7252 KB |
Output is correct |
20 |
Correct |
2108 ms |
5732 KB |
Output is correct |
21 |
Correct |
2502 ms |
6696 KB |
Output is correct |
22 |
Correct |
3581 ms |
8316 KB |
Output is correct |
23 |
Correct |
655 ms |
4552 KB |
Output is correct |
24 |
Correct |
2872 ms |
6708 KB |
Output is correct |
25 |
Correct |
2625 ms |
5960 KB |
Output is correct |
26 |
Correct |
3677 ms |
8764 KB |
Output is correct |
27 |
Correct |
3877 ms |
8628 KB |
Output is correct |
28 |
Correct |
2126 ms |
4500 KB |
Output is correct |
29 |
Correct |
2198 ms |
4776 KB |
Output is correct |
30 |
Correct |
2592 ms |
6984 KB |
Output is correct |
31 |
Correct |
2504 ms |
5476 KB |
Output is correct |
32 |
Correct |
2095 ms |
8488 KB |
Output is correct |
33 |
Correct |
1903 ms |
6968 KB |
Output is correct |