传送门
Luogu
解题思路
考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次。
然后后就是 dsu on the tree 了。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 500010;
int tot, head[_], nxt[_ << 1], ver[_ << 1], w[_ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
int n, dis[_], tg[1 << 22 | 2];
int dep[_], siz[_], son[_], ans[_];
int num, L[_], R[_], id[_];
inline void dfs(int u, int f) {
siz[u] = 1, dep[u] = dep[f] + 1;
L[u] = ++num, id[num] = u;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dis[v] = dis[u] ^ w[i];
dfs(v, u), siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
R[u] = num;
}
inline void dfss(int u, int f, int keep) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f || v == son[u]) continue;
dfss(v, u, 0), ans[u] = max(ans[u], ans[v]);
}
if (son[u]) dfss(son[u], u, 1), ans[u] = max(ans[u], ans[son[u]]);
if (tg[dis[u]]) ans[u] = max(ans[u], tg[dis[u]] - dep[u]);
for (rg int k = 0; k < 22; ++k)
if (tg[dis[u] ^ (1 << k)])
ans[u] = max(ans[u], tg[dis[u] ^ (1 << k)] - dep[u]);
tg[dis[u]] = max(tg[dis[u]], dep[u]);
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == son[u] || v == f) continue;
for (rg int x = L[v]; x <= R[v]; ++x) {
int p = id[x];
if (tg[dis[p]]) ans[u] = max(ans[u], tg[dis[p]] + dep[p] - 2 * dep[u]);
for (rg int k = 0; k < 22; ++k)
if (tg[dis[p] ^ (1 << k)]) ans[u] = max(ans[u], tg[dis[p] ^ (1 << k)] + dep[p] - 2 * dep[u]);
}
for (rg int x = L[v]; x <= R[v]; ++x)
tg[dis[id[x]]] = max(tg[dis[id[x]]], dep[id[x]]);
}
if (!keep) for (rg int x = L[u]; x <= R[u]; ++x) tg[dis[id[x]]] = 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int p, i = 2; i <= n; ++i) {
read(p); char c = getchar();
while (!isalpha(c)) c = getchar();
Add_edge(p, i, 1 << (c - 'a'));
Add_edge(i, p, 1 << (c - 'a'));
}
dfs(1, 0), dfss(1, 0, 1);
for (rg int i = 1; i <= n; ++i)
printf("%d%c", ans[i], "
"[i == n]);
return 0;
}
完结撒花 (qwq)