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
| struct LinkCutTree { int rev[N + 5], val[N + 5], Son[N + 5][2], Fa[N + 5], Stack[N + 5], top; inline void up(int u) { val[u] = val[Son[u][0]] ^ val[Son[u][1]] ^ X[u]; } inline void down(int u) { if (!rev[u]) return; rev[Son[u][0]] ^= 1, rev[Son[u][1]] ^= 1, rev[u] ^= 1; swap(Son[u][0], Son[u][1]); } inline int isroot(int u) { return (Son[Fa[u]][0] != u && Son[Fa[u]][1] != u); }
void Rotate(int x) { int y = Fa[x], z = Fa[y]; int l = Son[y][1] == x, r = l ^ 1; if (!isroot(y)) Son[z][Son[z][1] == y] = x; Son[y][l] = Son[x][r], Fa[Son[x][r]] = y; Son[x][r] = y, Fa[y] = x, Fa[x] = z; up(y), up(x); } void Splay(int x) { Stack[top = 1] = x; for (int y = x; !isroot(y); y = Fa[y]) Stack[++top] = Fa[y]; per(i, top, 1) down(Stack[i]); while (!isroot(x)) { int y = Fa[x], z = Fa[y]; if (!isroot(y)) { if ((Son[y][1] == x) ^ (Son[z][1] == y)) Rotate(x); else Rotate(y); } Rotate(x); } }
inline void Access(int u) { for (int last = 0; u; last = u, u = Fa[u]) Splay(u), Son[u][1] = last, up(u); } inline void Make_Root(int u) { Access(u), Splay(u), rev[u] ^= 1; } inline int Find_Root(int u) { Access(u), Splay(u); while (Son[u][0]) u = Son[u][0]; return u; }
inline void Split(int x, int y) { Make_Root(x), Access(y), Splay(y); } inline void Link(int x, int y) { Make_Root(x); if (Find_Root(y) == x) return; Fa[x] = y; } inline void Cut(int x, int y) { Make_Root(x); if (Find_Root(y) != x || Fa[x] != y || Son[x][1]) return; Fa[x] = Son[y][0] = 0; } }LCT;
|