이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |