Description
Bob有一棵n个点的有根树,其中
定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色。
Bob可能会进行这几种操作:
- 1 x:把点x到根节点的路径上所有的点染上一种没有用过的新颜色。
2 x y :求x到y的路径的权值。- 3 x:在以x为根的子树中选择一个点,使得这个点到根节点的路径权值最大,求最大权值。
Bob一共会进行m次操作。
Solution
后面两个操作维护dfs序之后直接线段树查询一下就好了。
第一个操作好像就是access吧。
虚边相连的两个点颜色不同,实边相连的两个点颜色相同。这样的话每次access的时候都在splay之后对相应节点+1,−1就好了。
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
const int N = 101010;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
template<typename T>
inline T Max(T a, T b) {
return a < b ? b : a;
}
int n, m, Gcnt, x, y, z, clc, opt;
struct edge {
int to, next;
edge (int t = 0, int n = 0):to(t), next(n) {}
};
edge G[N << 1];
int head[N];
namespace Tree {
int pre[N], post[N], son[N], size[N], dep[N], top[N], fa[N];
inline void dfs1(int u) {
int to; size[u] = 1;
for (int i = head[u]; i; i = G[i].next) {
to = G[i].to; if (to == fa[u]) continue;
dep[to] = dep[u] + 1; fa[to] = u;
dfs1(to); size[u] += size[to];
if (size[son[u]] < size[to]) son[u] = to;
}
}
inline void dfs2(int u, int t) {
top[u] = t; pre[u] = ++clc;
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; i; i = G[i].next)
if (G[i].to != son[u] && G[i].to != fa[u])
dfs2(G[i].to, G[i].to);
post[u] = clc;
}
inline int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
y = fa[top[y]];
}
return dep[x] < dep[y] ? x : y;
}
}
namespace Seg {
int mx[N << 2], add[N << 2];
inline void PushDown(int o) {
if (add[o]) {
mx[o << 1] += add[o]; mx[o << 1 | 1] += add[o];
add[o << 1] += add[o]; add[o << 1 | 1] += add[o];
add[o] = 0;
}
}
inline void Add(int o, int l, int r, int L, int R, int x) {
if (l >= L && r <= R) {
add[o] += x; mx[o] += x;
return;
}
int mid = (l + r) >> 1;
PushDown(o);
if (L <= mid) Add(o << 1, l, mid, L, R, x);
if (R > mid) Add(o << 1 | 1, mid + 1, r, L, R, x);
mx[o] = Max(mx[o << 1], mx[o << 1 | 1]);
}
inline int Query(int o, int l, int r, int L, int R) {
if (l >= L && r <= R) return mx[o];
int mid = (l + r) >> 1, res = 0;
PushDown(o);
if (L <= mid) res = Max(res, Query(o << 1, l, mid, L, R));
if (R > mid) res = Max(res, Query(o << 1 | 1, mid + 1, r, L, R));
return res;
}
inline void Add(int u, int x) {
Add(1, 1, n, Tree::pre[u], Tree::post[u], x);
}
inline int Query(int u) {
return Query(1, 1, n, Tree::pre[u], Tree::post[u]);
}
inline int Dist(int u) {
return Query(1, 1, n, Tree::pre[u], Tree::pre[u]);
}
}
namespace LCT {
struct node {
node *ch[2];
node *fa;
int id;
};
node *null;
node mem[N];
node *T[N];
inline void Init(int n) {
for (int i = 0; i <= n; i++) T[i] = mem + i;
null = T[0];
null->ch[0] = null->ch[1] = null;
null->fa = null;
for (int i = 1; i <= n; i++) {
T[i]->ch[0] = T[i]->ch[1] = null;
T[i]->fa = T[Tree::fa[i]]; T[i]->id = i;
}
}
inline bool IsRoot(node *x) {
return x->fa == null || (x->fa->ch[0] != x && x->fa->ch[1] != x);
}
inline void Rotate(node* &x) {
node *y = x->fa, *z = y->fa;
int l = (y->ch[0] != x), r = l ^ 1;
if (!IsRoot(y) && z != null) {
if (z->ch[0] == y) z->ch[0] = x;
else z->ch[1] = x;
}
x->fa = z; y->fa = x; x->ch[r]->fa = y;
y->ch[l] = x->ch[r]; x->ch[r] = y;
}
inline void Splay(node* &x) {
while (!IsRoot(x)) {
node *y = x->fa, *z = y->fa;
if (!IsRoot(y)) {
if (y->ch[0] == x ^ z->ch[0] == y) Rotate(x);
else Rotate(y);
}
Rotate(x);
}
}
inline void Access(node* x) {
node *t, *y;
for (y = null; x != null; x = x->fa) {
Splay(x);
for (t = x->ch[1]; t->ch[0] != null; t = t->ch[0]);
if (t != null) Seg::Add(t->id, 1);
for (t = y; t->ch[0] != null; t = t->ch[0]);
if (t != null) Seg::Add(t->id, -1);
x->ch[1] = y; y = x;
}
}
}
inline void AddEdge(int from, int to) {
G[++Gcnt] = edge(to, head[from]); head[from] = Gcnt;
G[++Gcnt] = edge(from, head[to]); head[to] = Gcnt;
}
int main(void) {
read(n); read(m);
for (int i = 1; i < n; i++) {
read(x); read(y);
AddEdge(x, y);
}
Tree::dfs1(1); Tree::dfs2(1, 1);
LCT::Init(n);
for (int i = 1; i <= n; i++)
Seg::Add(Tree::pre[i], 1);
while (m--) {
read(opt); read(x);
if (opt == 1) {
LCT::Access(LCT::T[x]);
} else if (opt == 2) {
read(y); z = Tree::LCA(x, y);
printf("%d\n", Seg::Dist(x) + Seg::Dist(y) - 2 * Seg::Dist(z) + 1);
} else {
printf("%d\n", Seg::Query(x));
}
}
return 0;
}