Description
Bob有一棵n个点的有根树,其中1号点是根节点。Bob在每个点上涂了颜色,并且每个点上的颜色不同。定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色。Bob可能会进行这几种操作:
1 x:把点x到根节点的路径上所有的点染上一种没有用过的新颜色。
2 x y:求x到y的路径的权值。
3 x y:在以x为根的子树中选择一个点,使得这个点到根节点的路径权值最大,求最大权值。
Bob一共会进行m次操作。
Solution
感觉很妙啊
其实第一个操作其实就是LCT中的access操作。
嗯,那么我们来考虑怎么解决第2、3个操作。
其实一条路径的权值就是路径上偏爱路径的条数(一个点也算一条偏爱路径)
设
fi
f
i
为
i
i
到根的权值,用线段树维护。
那么查询节点之间的权值也就是
fx+fy−2flca+1
f
x
+
f
y
−
2
f
l
c
a
+
1
,第三个询问也就是线段树求最值了。
Code
/* * * * * * * * * *
* Au: Hany01
* Date: Apr 4th, 2018
* Prob: [BZOJ4817][SDOI2017] 树点涂色
* Email: hany01@foxmail.com
* * * * * * * * * */
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, szof(a))
#define Cpy(a, b) memcpy(a, b, szof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).sz())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 100005;
int n, dfn[maxn], fa[maxn][19], sz[maxn], beg[maxn], nex[maxn << 1], e, v[maxn << 1], dep[maxn], tim;
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
struct SegmentTree
{
int tr[maxn << 2], tag[maxn << 2];
#define lc (t << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
void pushdown(int t) {
if (tag[t])
tag[lc] += tag[t], tag[rc] += tag[t], tr[lc] += tag[t], tr[rc] += tag[t], tag[t] = 0;
}
void update(int t, int l, int r, int x, int y, int dt)
{
if (x <= l && r <= y) {
tr[t] += dt, tag[t] += dt;
return;
}
pushdown(t);
if (x <= mid) update(lc, l, mid, x, y, dt);
if (y > mid) update(rc, mid + 1, r, x, y, dt);
tr[t] = max(tr[lc], tr[rc]);
}
int query(int t, int l, int r, int x, int y)
{
if (x <= l && r <= y) return tr[t];
pushdown(t);
if (y <= mid) return query(lc, l, mid, x, y);
if (x > mid) return query(rc, mid + 1, r, x, y);
return max(query(lc, l, mid, x, y), query(rc, mid + 1, r, x, y));
}
inline int query(int x) { return query(1, 1, n, x, x); }
inline int query(int x, int y) { return query(1, 1, n, x, y); }
}st;
struct LCT
{
int ch[maxn][2], fa[maxn], rev[maxn];
#define dir(x) (ch[fa[x]][1] == x)
#define isrt(x) (ch[fa[x]][0] != x && ch[fa[x]][1] != x)
inline void rotate(int u) {
int f = fa[u], gf = fa[f], d = dir(u);
fa[ch[f][d] = ch[u][d ^ 1]] = f;
fa[u] = gf;
if (!isrt(f)) ch[gf][dir(f)] = u;
ch[fa[f] = u][d ^ 1] = f;
}
inline void splay(int o) {
for ( ; !isrt(o); rotate(o)) if (!isrt(fa[o])) rotate(dir(o) == dir(fa[o]) ? fa[o] : o);
}
inline int getleft(int o) {
while (ch[o][0]) o = ch[o][0];
return o;
}
inline void access(int o) {
for (register int t = 0, tmp; o; t = o, o = fa[o]) {
splay(o);
if (ch[o][1]) tmp = getleft(ch[o][1]), st.update(1, 1, n, dfn[tmp], dfn[tmp] + sz[tmp] - 1, 1);
ch[o][1] = t;
if (t) tmp = getleft(t), st.update(1, 1, n, dfn[tmp], dfn[tmp] + sz[tmp] - 1, -1);
}
}
}lct;
void dfs(int u, int pa)
{
dfn[u] = ++ tim, dep[u] = dep[pa] + 1, sz[u] = 1, fa[u][0] = pa, lct.fa[u] = pa;
st.update(1, 1, n, tim, tim, dep[u]);
for (register int i = beg[u]; i; i = nex[i])
if (pa != v[i]) dfs(v[i], u), sz[u] += sz[v[i]];
}
inline int LCA(int u, int v)
{
register int dis = dep[u] - dep[v];
if (dis < 0) swap(u, v), dis = -dis;
Fordown(i, 18, 0) if (dis >= (1 << i)) dis -= (1 << i), u = fa[u][i];
if (u == v) return u;
Fordown(i, 18, 0) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int main()
{
#ifdef hany01
File("bzoj4817");
#endif
static int m, uu, vv;
n = read(), m = read();
For(i, 2, n) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
dfs(1, 0);
for (register int j = 1; (1 << j) <= n; ++ j)
For(i, 1, n) fa[i][j] = fa[fa[i][j - 1]][j - 1];
while (m --)
{
register int type = read(), x, y;
if (type == 1) lct.access(read());
else if (type == 2) {
x = read(), y = read();
printf("%d\n", st.query(dfn[x]) + st.query(dfn[y]) - (st.query(dfn[LCA(x, y)]) << 1) + 1);
} else {
x = read();
printf("%d\n", st.query(dfn[x], dfn[x] + sz[x] - 1));
}
}
return 0;
}
//天生我材必有用,千金散尽还复来。
// -- 李白《将进酒》