func_code_string: #include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     char c = getchar();     x = 0;     while (!isdigit(c) && !isalpha(c)) c = getchar();     if (isalpha(c)) return void(x = c -  a );     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();   }   const int N = 5e5 + 5, SZ = 22;   int n, vv, ww;   int e, head[N], to[N << 1], val[N << 1], nxt[N << 1];   inline void add(int u, int v, int w) {     to[++e] = v;     val[e] = w;     nxt[e] = head[u];     head[u] = e;   }   inline void add_edge(int u, int v, int w) {     add(u, v, w);     add(v, u, w);   }   int sz[N], son[N], f[1 << 22 | 5], dep[N], Son, Max, ans[N], cur[N], s[N];   inline void dfs1(int u, int fa) {     sz[u] = 1;     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) {         int v = to[i], w = val[i];         s[v] = s[u] ^ (1 << w);         dep[v] = dep[u] + 1;         dfs1(v, u);         sz[u] += sz[v];         if (sz[v] > sz[son[u]]) son[u] = v;       }   }   inline void calc(int u, int fa) {     if (f[s[u]]) Max = max(Max, f[s[u]] + dep[u]);     for (int i = 1; i <= (1 << 21); i <<= 1)       if (f[i ^ s[u]]) Max = max(Max, f[i ^ s[u]] + dep[u]);     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) calc(to[i], u);   }   inline void update(int u, int fa) {     f[s[u]] = max(f[s[u]], dep[u]);     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) update(to[i], u);   }   inline void get_Max(int u, int fa) {     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa && to[i] != Son) {         calc(to[i], u);         update(to[i], u);       }   }   inline void clear(int u, int fa) {     f[s[u]] = 0;     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) clear(to[i], u);   }   inline void dfs2(int u, int fa, bool keep) {     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa && to[i] != son[u]) dfs2(to[i], u, false);     if (son[u]) dfs2(son[u], u, true);     Son = son[u];     f[s[u]] = max(f[s[u]], dep[u]);     get_Max(u, fa);     ans[u] = max(f[s[u]] - dep[u], Max - 2 * dep[u]);     for (int i = 1; i <= (1 << 21); i <<= 1)       ans[u] = max(ans[u], f[i ^ s[u]] - dep[u]);     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) ans[u] = max(ans[u], ans[to[i]]);     Son = Max = 0;     if (!keep) clear(u, fa);   }   int main() {     read(n);     for (int uu = 2; uu <= n; uu++) {       read(vv);       read(ww);       add_edge(uu, vv, ww);     }     dfs1(1, 0);     dfs2(1, 0, 0);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }   