///*** Sown_Vipro ***///
/// ->TUYEN QUOC GIA<- ///
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
void maxi(int &x, int y){ if(x < y) x = y; }
const int N = 50000 + 5, MAX = 1e6, oo = 1e9 + 5, MOD = 1e9 + 7, base = 311;
int n, ans, maxDepth;
int s[N], del[N], pw[N];
string str;
vector<int> e[N];
map<int, bool> f[N];
void count_child(int u, int p){
s[u] = 1;
for(int v : e[u]){
if(v != p && !del[v]){
count_child(v, u);
s[u] += s[v];
}
}
}
int find_centroid(int u, int p, int n){
for(int v : e[u]){
if(v != p && !del[v] && s[v] > n) return find_centroid(v, u, n);
}
return u;
}
vector<pi> b;
bool dfs(int u, int p, int k, int h, int up, int down){
if(k < h) return false;
maxi(maxDepth, h);
down = (1ll * down * base + str[u]) % MOD;
up = (1ll * str[u] * pw[h - 1] + up) % MOD;
int x = (1ll * up * pw[k - h] - down + MOD) % MOD;
if(f[k - h].find(x) != f[k - h].end()) return true;
if(!p) b.clear();
for(int v : e[u]){
if(v != p && !del[v]){
if(dfs(v, u, k, h + 1, up, down)) return true;
}
}
b.pb({h - 1, x});
if(!p){
for(auto [h, x] : b) f[h][x] = true;
}
return false;
}
bool build(int u, int k){
count_child(u, 0);
int cen = find_centroid(u, 0, s[u] / 2);
del[cen] = 1;
int x = (-str[cen] + MOD) % MOD;
f[0][x] = 1;
for(int v : e[cen]){
if(del[v]) continue;
if(dfs(v, 0, k, 2, 0, str[cen])){
FOR(d, 0, maxDepth) f[d].clear();
maxDepth = 0;
return true;
}
}
FOR(d, 0, maxDepth) f[d].clear();
maxDepth = 0;
for(int v : e[cen]){
if(!del[v]) if(build(v, k)) return true;
}
return false;
}
void solve(){
cin >> n;
cin >> str;
str = "#" + str;
FOR(i, 2, n){
int u, v; cin >> u >> v;
e[u].pb(v);
e[v].pb(u);
}
pw[0] = 1;
FOR(i, 1, n) pw[i] = (1ll * pw[i - 1] * base) % MOD;
int l = 1, r = n; ans = 1;
while(l <= r){
int mid = (l + r) / 2;
FOR(u, 1, n) del[u] = 0;
if(build(1, mid * 2)){
l = mid + 1;
ans = max(ans, mid * 2);
}else r = mid - 1;
}
l = 1; r = n;
while(l <= r){
int mid = (l + r) / 2;
FOR(u, 1, n) del[u] = 0;
if(build(1, mid * 2 + 1)){
l = mid + 1;
ans = max(ans, mid * 2 + 1);
}else r = mid - 1;
}
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
inp("in.txt");
// out("out.txt");
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
lampice.cpp: In function 'int main()':
lampice.cpp:17:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
lampice.cpp:133:5: note: in expansion of macro 'inp'
133 | inp("in.txt");
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |