# 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree

### Solution

IOI 2011 Race是一道著名的点分治模板题，要求统计链信息，也可以用DSU on Tree来做，题目链接在这里：(✿◕‿◕✿)

### Code

#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int _w;

int n, ans[MAXN];

namespace G {
int head[MAXN], nxt[MAXN], to[MAXN], val[MAXN], eid;
void init() {
}
void adde( int u, int v, int w ) {
to[eid] = v, val[eid] = w;
}
}

int sz[MAXN], dep[MAXN], pa[MAXN], son[MAXN], str[MAXN];
int prelude( int u, int fa, int d, int s ) {
using namespace G;
sz[u] = 1, dep[u] = d, pa[u] = fa, str[u] = s;
for( int i = head[u]; ~i; i = nxt[i] ) {
int v = to[i], t = 1<<val[i];
sz[u] += prelude(v, u, d+1, s^t);
if( sz[v] > sz[son[u]] )
son[u] = v;
}
return sz[u];
}

int maxd[1<<22];

void ans_node( int s, int d, int &ans ) {
ans = max(ans, d + maxd[s]);
for( int i = 0; i < 22; ++i )
ans = max(ans, d + maxd[s^(1<<i)]);
}
void dfs_ans( int u, int &ans ) {
using namespace G;
ans_node(str[u], dep[u], ans);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_ans(to[i], ans);
}

void ins_node( int s, int d ) {
maxd[s] = max(maxd[s], d);
}
void dfs_ins( int u ) {
using namespace G;
ins_node(str[u], dep[u]);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_ins(to[i]);
}

void del_node( int s ) {
maxd[s] = -INF;
}
void dfs_del( int u ) {
using namespace G;
del_node(str[u]);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_del(to[i]);
}

void solve( int u, bool clr ) {
using namespace G;
for( int i = head[u]; ~i; i = nxt[i] )
if( to[i] != son[u] )
solve(to[i], 1);
if( son[u] ) solve(son[u], 0);
ans_node(str[u], dep[u], ans[u]);
ins_node(str[u], dep[u]);
for( int i = head[u]; ~i; i = nxt[i] )
if( to[i] != son[u] ) {
dfs_ans(to[i], ans[u]);
dfs_ins(to[i]);
}
if( clr ) dfs_del(u);
ans[u] -= dep[u] + dep[u];
for( int i = head[u]; ~i; i = nxt[i] )
ans[u] = max(ans[u], ans[to[i]]);
ans[u] = max(ans[u], 0);
}

int main() {
_w = scanf( "%d", &n );
G::init();
for( int i = 2; i <= n; ++i ) {
int pa;
char ch;
_w = scanf( "%d %c", &pa, &ch );
}