1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| #include <bits/stdc++.h> #define N 100005 using namespace std;
inline int read() { int x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); return x * f; }
int n, m, head[N], pre[N<<1], to[N<<1], sz;
inline void addedge(int u, int v) { pre[++sz] = head[u]; head[u] = sz; to[sz] = v; pre[++sz] = head[v]; head[v] = sz; to[sz] = u; }
int fa[N], dfn[N], ed[N], rnk[N], tme, d[N], top[N], son[N], siz[N];
void dfs(int x) { siz[x] = 1; for (int i = head[x]; i; i = pre[i]) { int y = to[i]; if (y == fa[x]) continue; fa[y] = x; d[y] = d[x] + 1; dfs(y); siz[x] += siz[y]; if (!son[x] || siz[son[x]] < siz[y]) son[x] = y; } }
void dfs2(int x, int tp) { top[x] = tp; dfn[x] = ++tme; rnk[tme] = x; if (son[x]) dfs2(son[x], tp); for (int i = head[x]; i; i = pre[i]) { int y = to[i]; if (y == fa[x] || y == son[x]) continue; dfs2(y, y); } ed[x] = tme; }
inline int LCA(int x, int y) { while (top[x] != top[y]) { if (d[top[x]] < d[top[y]]) swap(x, y); x = fa[top[x]]; } if (d[x] > d[y]) swap(x, y); return x; }
namespace Segtree{ struct tree{ int l, r, mx, tag; } tr[N<<2]; #define lson ind<<1 #define rson ind<<1|1 void build(int ind, int l, int r) { tr[ind].l = l; tr[ind].r = r; tr[ind].tag = 0; if (l == r) { tr[ind].mx = d[rnk[l]]; return; } int mid = (l + r) >> 1; build(lson, l, mid); build(rson, mid+1, r); tr[ind].mx = max(tr[lson].mx, tr[rson].mx); } inline void pushdown(int ind) { if (!tr[ind].tag) return; int v = tr[ind].tag; tr[ind].tag = 0; tr[lson].mx += v; tr[lson].tag += v; tr[rson].mx += v; tr[rson].tag += v; } void update(int ind, int x, int y, int v) { int l = tr[ind].l, r = tr[ind].r; if (x <= l && r <= y) { tr[ind].mx += v; tr[ind].tag += v; return; } pushdown(ind); int mid = (l + r) >> 1; if (x <= mid) update(lson, x, y, v); if (mid < y) update(rson, x, y, v); tr[ind].mx = max(tr[lson].mx, tr[rson].mx); } int query(int ind, int x, int y) { int l = tr[ind].l, r = tr[ind].r; if (x <= l && r <= y) return tr[ind].mx; pushdown(ind); int mid = (l + r) >> 1, ret = 0; if (x <= mid) ret = max(ret, query(lson, x, y)); if (mid < y) ret = max(ret, query(rson, x, y)); return ret; } #undef lson #undef rson }
namespace LCT{ int ch[N][2], fa[N], tag[N];
#define lson ch[x][0] #define rson ch[x][1] inline void rev(int x) { swap(lson, rson); tag[x] ^= 1; } inline void pushdown(int x) { if (!tag[x]) return; if (lson) rev(lson); if (rson) rev(rson); tag[x] = 0; } inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } inline void rotate(int x) { int y = fa[x], z = fa[y], k = (ch[y][1]==x); if (!isroot(y)) ch[z][ch[z][1]==y] = x; fa[x] = z; ch[y][k] = ch[x][k^1]; fa[ch[x][k^1]] = y; ch[x][k^1] = y; fa[y] = x; } int q[N], top; inline void splay(int x) { q[top=1] = x; for (int i = x; !isroot(i); i = fa[i]) q[++top] = i; while (!top) pushdown(q[top--]); while (!isroot(x)) { int y = fa[x], z = fa[y]; if (!isroot(y)) ((ch[z][1]==y) ^ (ch[y][1]==x)) ? rotate(x) : rotate(y); rotate(x); } } inline int pre(int x) { while (lson) x = lson; return x; } inline void access(int x) { for (int i = 0; x; i = x, x = fa[x]) { splay(x); if (ch[x][1]) { int y = pre(ch[x][1]); Segtree::update(1, dfn[y], ed[y], 1); } ch[x][1] = i; if (ch[x][1]) { int y = pre(ch[x][1]); Segtree::update(1, dfn[y], ed[y], -1); } } } #undef lson #undef rson }
int main() { n = read(), m = read(); for (int i = 1, u, v; i < n; i++) { u = read(), v = read(); addedge(u, v); } d[1] = 1; dfs(1); dfs2(1, 1); Segtree::build(1, 1, n); for (int i = 1; i <= n; i++) { LCT::ch[i][0] = LCT::ch[i][1] = LCT::tag[i] = 0; LCT::fa[i] = fa[i]; } for (int i = 1, tp, x, y; i <= m; i++) { tp = read(); if (tp == 1) { x = read(); LCT::access(x); } else if (tp == 2) { x = read(), y = read(); int lca = LCA(x, y); printf("%d\n", Segtree::query(1, dfn[x], dfn[x]) + Segtree::query(1, dfn[y], dfn[y]) - 2 * Segtree::query(1, dfn[lca], dfn[lca]) + 1); } else { x = read(); printf("%d\n", Segtree::query(1, dfn[x], ed[x])); } } return 0; }
|