#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const ll base = 31;
const int N = 18;
int n, len, root, used[maxn], h[maxn], spt[maxn][20];
string s;
vector<int> adj[maxn];
ll Pow[maxn], Inv[maxn], up[maxn], down[maxn];
map<ll,bool> L, cur;
bool flag;
ll add(ll a, ll b) {
a += b; if (a > mod) a -= mod;
return a;
}
ll del(ll a, ll b) {
a -= b; if (a < 0) a += mod;
return a;
}
void pre(int u, int p) {
if (p)
down[u] = add(down[p], (s[u] - 'a' + 1) * Pow[h[u] - 1]);
if (p)
up[u] = add(up[p] * base % mod, s[u] - 'a' + 1);
spt[u][0] = p;
for (int j = 1; j <= N; j++)
spt[u][j] = spt[spt[u][j - 1]][j - 1];
for (int v : adj[u])
if (v != p && !used[v]) {
h[v] = h[u] + 1;
pre(v, u);
}
}
int jump(int u, int k) {
for (int j = N; j >= 0; j--)
if (k >> j & 1)
u = spt[u][j];
return u;
}
void dfs(int u, int p) {
if (len < h[u]) return;
int k = len - h[u] - 1, c = jump(u, k);
// if (u == 5 && h[u] == 7) clog << db(c);
// if (u == 5 && h[u] == 7) clog << add(up[c], (s[root] - 'a' + 1) * Pow[h[c]] % mod) << ' ' << down[c] * base + (s[root] - 'a' + 1) << '\n';
if (c && add(up[c], (s[root] - 'a' + 1) * Pow[h[c]] % mod) ==
add(down[c] * base % mod, (s[root] - 'a' + 1))) {
if (k == 0)
return flag = true, void();
ll val = del(up[u], up[c] * Pow[k] % mod) ;
// if (u == 5 && h[u] == 7) clog << db(val);
if (L.find(val) != L.end()) {
return flag = true, void();
}
}
cur[up[u]] = 1;
for (int v : adj[u])
if (v != p && !used[v])
dfs(v, u);
}
void Solve(int c) {
h[c] = 0;
up[c] = down[c] = 0;
pre(c, 0);
L.clear();
for (int v : adj[c])
if (!used[v]) {
dfs(v, c);
for (auto it : cur)
L[it.fi] = 1;
cur.clear();
}
reverse(adj[c].begin(), adj[c].end());
L.clear();
for (int v : adj[c])
if (!used[v]) {
dfs(v, c);
for (auto it : cur)
L[it.fi] = 1;
cur.clear();
}
}
struct Centroid_Decomposition {
int sz[maxn];
int dfs(int u, int p) {
sz[u] = 1;
for (int v : adj[u])
if (!used[v] && v != p)
sz[u] += dfs(v, u);
return sz[u];
}
int Centroid(int u, int p, int tot) {
for (int v : adj[u])
if (!used[v] && v != p && sz[v] > tot / 2)
return Centroid(v, u, tot);
return u;
}
void build(int u) {
int c = Centroid(u, 0, dfs(u, 0));
root = c;
Solve(c);
used[c] = 1;
for (int v : adj[c])
if (!used[v])
build(v);
}
} Cen;
bool ok(int m, int sign) {
flag = false;
memset(used, 0, sizeof used);
len = 2 * m + sign;
Cen.build(1);
return flag;
}
ll Expo(ll a, int b) {
if (b == 0) return 1;
ll t = Expo(a, b / 2);
(t *= t) %= mod;
if (b & 1) (t *= a) %= mod;
return t;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
Pow[0] = Inv[0] = 1;
for (int i = 1; i <= n; i++) {
Pow[i] = Pow[i - 1] * base % mod;
Inv[i] = Expo(Pow[i], mod - 2);
}
// return cout << ok(4, 1), 0;
int L = 1, R = n / 2, res = 1;
while (L <= R) {
int mid = (L + R) / 2;
if (ok(mid, 1)) {
res = 2 * mid + 1;
L = mid + 1;
}
else R = mid - 1;
}
L = 1, R = n / 2;
while (L <= R) {
int mid = (L + R) / 2;
if (ok(mid, 0)) {
res = max(res, 2 * mid);
L = mid + 1;
}
else R = mid - 1;
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3156 KB |
Output is correct |
2 |
Correct |
32 ms |
3156 KB |
Output is correct |
3 |
Correct |
124 ms |
3448 KB |
Output is correct |
4 |
Correct |
146 ms |
3472 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5051 ms |
14924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5085 ms |
15000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3156 KB |
Output is correct |
2 |
Correct |
32 ms |
3156 KB |
Output is correct |
3 |
Correct |
124 ms |
3448 KB |
Output is correct |
4 |
Correct |
146 ms |
3472 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
3076 KB |
Output is correct |
8 |
Execution timed out |
5051 ms |
14924 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |