#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
inline bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
inline bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
const int mod = 1e9 + 9;
inline int fastPow(int a, int n){
int res = 1;
while(n){
if(n & 1)res = 1ll * res * a * mod;
a = 1ll * a * a % mod;
n >>= 1;
}
return res;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 5e4 + 5;
const int inf = 2e9;
const long long infll = 1e18;
char s[maxN];
int n;
vector<int> adj[maxN];
namespace subtask1{
bool check(){
return n <= 3000;
}
const int maxN = 3e3 + 1;
const int base = 333;
int d[maxN];
int power[maxN];
int Hash[maxN], revHash[maxN];
void dfs(int u, int p){
for(int v : adj[u]){
if(v != p){
d[v] = d[u] + 1;
Hash[v] = (1ll * Hash[u] * base + s[v]) % mod;
dfs(v, u);
}
}
}
void dfs2(int u, int p){
for(int v : adj[u]){
if(v != p){
revHash[v] = (1ll * s[v] * power[d[v] - 1] + revHash[u]) % mod;
dfs2(v, u);
}
}
}
int answer = 0;
void query(int centre){
FOR(i, 1, n)Hash[i] = 0;
d[centre] = 1;
Hash[centre] = s[centre];
revHash[centre] = s[centre];
dfs(centre, 0);
dfs2(centre, 0);
FOR(i, 1, n)if(Hash[i] == revHash[i])maximize(answer, d[i]);
}
void solve(){
power[0] = 1;
FOR(i, 1, n)power[i] = 1ll * power[i - 1] * base % mod;
FOR(i, 1, n)query(i);
cout << answer;
}
}
void process(){
cin >> n;
FOR(i, 1, n)cin >> s[i];
FOR(i, 2, n){
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
if(subtask1 :: check()) return subtask1 :: solve();
}
#define NAME ""
int main(){
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
// cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
lampice.cpp: In function 'int main()':
lampice.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |