// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n; vector<int> g[MAXN];
string s;
ll p[MAXN];
int sz[MAXN]; ll h[MAXN]; bool del[MAXN];
void dfsSize(int u, int par){
sz[u] = 1;
Forv(v, g[u]) if(v != par && !del[v]) dfsSize(v, u), sz[u] += sz[v];
}
int find(int u, int par, int n){
Forv(v, g[u]) if(v != par && !del[v] && sz[v] * 2 > n) return find(v, u, n);
return u;
}
ll mxhigh = 0, mxlen;
ll hashup[MAXN], hashdown[MAXN], a[MAXN];
map<ll, bool> mp[MAXN]; bool op;
vector<int> List;
void add(int u, int par){
if(op) return;
h[u] = h[par] + 1;
if (h[u] + 1 > mxlen) return;
maximize(mxhigh, h[u]);
hashup[u] = (hashup[par] * base + s[u] - 'a' + 1) % mod;
hashdown[u] = (hashdown[par] + p[h[u]] * (s[u] - 'a' + 1)) % mod;
ll cur = (1LL * p[mxlen - h[u] - 1] * hashdown[u] - hashup[u] + mod) % mod;
if (mp[mxlen - h[u] - 1].find(cur) != mp[mxlen - h[u] - 1].end()){
op = true;
return;
}
if (h[u] + 1 == mxlen && hashdown[u] == (hashup[u] + p[h[u]] * (s[u] - 'a' + 1)) % mod){
op = true;
return;
}
a[u] = cur, List.push_back(u);
Forv(v, g[u]) if(v != par && !del[v]) add(v, u);
}
void solve(int u, int par){
if(op) return;
dfsSize(u, par);
int centroid = find(u, par, sz[u]);
del[centroid] = true;
mxhigh = 0, h[centroid] = 0, hashup[centroid] = 0, hashdown[centroid] = s[centroid] - 'a' + 1;
Forv(v, g[centroid]) if(v != par && !del[v]){
add(v, centroid);
Forv(i, List) mp[h[i]][a[i]] = true;
List.clear();
}
For(i, 0, mxhigh) mp[i].clear();
Forv(v, g[centroid]) if(v != par && !del[v]) solve(v, centroid);
}
bool check(int val){
reset(del, 0);
op = false; mxlen = val;
solve(1, 0);
return op;
}
void Solve() {
cin >> n >> s;
s = ' ' + s;
For(i, 1, n - 1){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
p[0] = 1;
For(i, 1, n) p[i] = p[i - 1] * base % mod;
int ans = 1;
For(_, 0, 1){
int l = 0, r = n / 2 + 1;
while(r - l > 1){
int mid = l + r >> 1;
if(check(mid * 2 + _)) l = mid;
else r = mid;
}
maximize(ans, _ + l * 2);
}
cout << ans << '\n';
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("noel.inp", "r")) {
freopen("noel.inp", "r", stdin);
freopen("noel.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
Compilation message
lampice.cpp: In function 'void Solve()':
lampice.cpp:116:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
116 | int mid = l + r >> 1;
| ~~^~~
lampice.cpp: In function 'int main()':
lampice.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen("noel.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen("noel.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
82000 KB |
Output is correct |
2 |
Correct |
19 ms |
82348 KB |
Output is correct |
3 |
Correct |
29 ms |
82264 KB |
Output is correct |
4 |
Correct |
28 ms |
82000 KB |
Output is correct |
5 |
Correct |
15 ms |
73808 KB |
Output is correct |
6 |
Correct |
15 ms |
82000 KB |
Output is correct |
7 |
Correct |
13 ms |
82000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
89424 KB |
Output is correct |
2 |
Correct |
670 ms |
89460 KB |
Output is correct |
3 |
Correct |
467 ms |
89808 KB |
Output is correct |
4 |
Correct |
586 ms |
90304 KB |
Output is correct |
5 |
Correct |
881 ms |
90444 KB |
Output is correct |
6 |
Correct |
110 ms |
89024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1477 ms |
88148 KB |
Output is correct |
2 |
Correct |
1709 ms |
88000 KB |
Output is correct |
3 |
Correct |
1430 ms |
88024 KB |
Output is correct |
4 |
Correct |
1260 ms |
86804 KB |
Output is correct |
5 |
Correct |
1191 ms |
87504 KB |
Output is correct |
6 |
Correct |
1115 ms |
86876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
82000 KB |
Output is correct |
2 |
Correct |
19 ms |
82348 KB |
Output is correct |
3 |
Correct |
29 ms |
82264 KB |
Output is correct |
4 |
Correct |
28 ms |
82000 KB |
Output is correct |
5 |
Correct |
15 ms |
73808 KB |
Output is correct |
6 |
Correct |
15 ms |
82000 KB |
Output is correct |
7 |
Correct |
13 ms |
82000 KB |
Output is correct |
8 |
Correct |
681 ms |
89424 KB |
Output is correct |
9 |
Correct |
670 ms |
89460 KB |
Output is correct |
10 |
Correct |
467 ms |
89808 KB |
Output is correct |
11 |
Correct |
586 ms |
90304 KB |
Output is correct |
12 |
Correct |
881 ms |
90444 KB |
Output is correct |
13 |
Correct |
110 ms |
89024 KB |
Output is correct |
14 |
Correct |
1477 ms |
88148 KB |
Output is correct |
15 |
Correct |
1709 ms |
88000 KB |
Output is correct |
16 |
Correct |
1430 ms |
88024 KB |
Output is correct |
17 |
Correct |
1260 ms |
86804 KB |
Output is correct |
18 |
Correct |
1191 ms |
87504 KB |
Output is correct |
19 |
Correct |
1115 ms |
86876 KB |
Output is correct |
20 |
Correct |
1061 ms |
85492 KB |
Output is correct |
21 |
Correct |
1466 ms |
86568 KB |
Output is correct |
22 |
Correct |
1756 ms |
86916 KB |
Output is correct |
23 |
Correct |
381 ms |
84304 KB |
Output is correct |
24 |
Correct |
1142 ms |
85840 KB |
Output is correct |
25 |
Correct |
1031 ms |
85488 KB |
Output is correct |
26 |
Correct |
1353 ms |
87500 KB |
Output is correct |
27 |
Correct |
1682 ms |
87324 KB |
Output is correct |
28 |
Correct |
769 ms |
84556 KB |
Output is correct |
29 |
Correct |
792 ms |
84584 KB |
Output is correct |
30 |
Correct |
998 ms |
86344 KB |
Output is correct |
31 |
Correct |
837 ms |
85228 KB |
Output is correct |
32 |
Correct |
919 ms |
87368 KB |
Output is correct |
33 |
Correct |
1035 ms |
86856 KB |
Output is correct |