给定一棵树。
定义一条路径的权值是,这条路径上的点(包括起点和终点)共有多少种不同的颜色。
要求支持三种操作:
对于操作 1 1 ,可以发现它与 LCT L C T 中的 access a c c e s s 很像,可以把一段重链视为颜色一样。因此在 access a c c e s s 操作时,为原重儿子的子树 +1 + 1 ,现重儿子的子树 −1 − 1 。用链剖+线段树维护即可。
而操作 2 2 的答案则是 f(x)+f(y)−f(lca)∗2+1 f ( x ) + f ( y ) − f ( l c a ) ∗ 2 + 1 ,其中 f f 是路径 (u,root) ( u , r o o t ) 的权值。
操作 3 3 的答案则是 x x 子树中的最大值。
#include
using namespace std;
const int maxn = 100005;
int n, m, dfn[maxn], low[maxn], Time;
inline int gi()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
#define mid ((l + r) >> 1)
#define lch (s << 1)
#define rch (s << 1 | 1)
int lazy[maxn * 4], Max[maxn * 4];
inline void pushdown(int s)
{
lazy[lch] += lazy[s]; Max[lch] += lazy[s];
lazy[rch] += lazy[s]; Max[rch] += lazy[s];
lazy[s] = 0;
}
void modify(int s, int l, int r, int x, int y, int val)
{
if (x <= l && r <= y) {
lazy[s] += val; Max[s] += val;
return;
}
pushdown(s);
if (x <= mid) modify(lch, l, mid, x, y, val);
if (y >= mid + 1) modify(rch, mid + 1, r, x, y, val);
Max[s] = max(Max[lch], Max[rch]);
}
int query(int s, int l, int r, int x, int y)
{
if (x <= l && r <= y) return Max[s];
pushdown(s);
int ret = 0;
if (x <= mid) ret = max(ret, query(lch, l, mid, x, y));
if (y >= mid + 1) ret = max(ret, query(rch, mid + 1, r, x, y));
return ret;
}
#define get(x) (ch[f[x]][1] == x)
#define isroot(x) (ch[f[x]][0] != x && ch[f[x]][1] != x)
int f[maxn], ch[maxn][2];
inline void rotate(int x)
{
int fa = f[x], gfa = f[fa], k = get(x);
if (!isroot(fa)) ch[gfa][get(fa)] = x;
ch[fa][k] = ch[x][k ^ 1]; f[ch[x][k ^ 1]] = fa;
ch[x][k ^ 1] = fa; f[fa] = x;
f[x] = gfa;
}
inline void splay(int x)
{
while (!isroot(x)) {
int fa = f[x];
if (!isroot(fa))
get(x) ^ get(fa) ? rotate(x) : rotate(fa);
rotate(x);
}
}
inline int findroot(int x)
{
while (ch[x][0]) x = ch[x][0];
return x;
}
inline void access(int x)
{
for (int y = 0, u; x; y = x, x = f[x]) {
splay(x);
if (ch[x][1])
u = findroot(ch[x][1]),
modify(1, 1, n, dfn[u], low[u], 1);
ch[x][1] = y;
if (ch[x][1])
u = findroot(ch[x][1]),
modify(1, 1, n, dfn[u], low[u], -1);
}
}
struct edge {
int to, next;
}e[maxn * 2];
int h[maxn], tot;
inline void add(int u, int v)
{
e[++tot] = (edge) {v, h[u]}; h[u] = tot;
e[++tot] = (edge) {u, h[v]}; h[v] = tot;
}
int son[maxn], siz[maxn], g[maxn], F[maxn], dep[maxn];
void dfs1(int u, int fa)
{
f[u] = F[u] = fa; dfn[u] = ++Time; siz[u] = 1;
dep[u] = dep[fa] + 1;
for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
if (v != fa) {
dfs1(v, u); siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
low[u] = Time;
}
void dfs2(int u, int fa)
{
if (son[u]) g[son[u]] = g[u], dfs2(son[u], u);
for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
if (v != son[u] && v != fa)
g[v] = v, dfs2(v, u);
}
int lca(int u, int v)
{
while (g[u] != g[v]) {
if (dep[g[u]] < dep[g[v]]) v = F[g[v]];
else u = F[g[u]];
}
return dep[u] < dep[v] ? u : v;
}
int main()
{
n = gi(); m = gi();
for (int u, v, i = 1; i < n; ++i) {
u = gi(); v = gi(); add(u, v);
}
dfs1(1, 0);
g[1] = 1; dfs2(1, 0);
for (int i = 1; i <= n; ++i)
modify(1, 1, n, dfn[i], dfn[i], dep[i]);
int opt, x, y, z, cnt = 0;
while (m--) {
opt = gi();
if (opt > 1) ++cnt;
if (opt == 1) x = gi(), access(x);
else if (opt == 2) {
x = gi(); y = gi(); z = lca(x, y);
printf("%d\n", query(1, 1, n, dfn[x], dfn[x]) + query(1, 1, n, dfn[y], dfn[y]) - query(1, 1, n, dfn[z], dfn[z]) * 2 + 1);
}
else if (opt == 3) {
x = gi();
printf("%d\n", query(1, 1, n, dfn[x], low[x]));
}
}
return 0;
}