This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int maxn = 1e5 + 5;
const int oo = 1e9 + 7;
const ll inf = 1e18;
const int base = 37;
const int mod = 1e9 + 7;
int ans = 1;
int p[maxn];
int n;
string s;
vector<int> adj[maxn];
void dfs(int u, int par, int hash_up = 0, int hash_down = 0, int dep = 0) {
hash_down = (1LL * base * hash_down + (s[u] - 'a' + 1)) % mod;
hash_up = (1LL * hash_up + 1LL * (s[u] - 'a' + 1) * p[dep]) % mod;
// cout << "Check: " << hash_down << ' ' << hash_up << '\n';
if (hash_down == hash_up)
ans = max(ans, dep + 1);
for (auto j : adj[u]) {
if (j == par)
continue;
dfs(j, u, hash_up, hash_down, dep + 1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
p[0] = 1;
for (int i = 1; i < maxn; i++)
p[i] = (1LL * p[i - 1] * base) % mod;
cin >> n;
cin >> s;
s = ' ' + s;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
dfs(i, i);
}
cout << ans;
}
# | 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... |