Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=2049
Bare LCT, save LCT template. Say a few places where the bug is:
①,rotate, there is no judge whether Y is the root, this is a little different from the ordinary splay.
②, the cyclic variable is I, not x!
#include <cstdio> #include <algorithm>const int maxn = 10005;int N, m, T1, T2;int ch[maxn][2], FA[MAXN], Stk[ma XN], Top;char opr[10], rev[maxn];inline bool IsRoot (int x) {return ch[fa[x]][0]! = x && ch[fa[x]][1]! = x;} inline void pushdown (int x) {if (rev[x]) {rev[x] = 0;rev[ch[x][0]] ^= 1;rev[ch[x][1]] ^= 1;std::swap (ch[x][0], ch[x][1]);} }inline void Rotate (int x) {int y = fa[x];if (!isroot (y)) {if (y = = Ch[fa[y]][0]) {ch[fa[y]][0] = x;} else {ch[fa[y]][1] = x;}} Fa[x] = Fa[y];int dir = x = = Ch[y][1];ch[y][dir] = ch[x][dir ^ 1];fa[ch[x][dir ^ 1]] = y;ch[x][dir ^ 1] = y;fa[y] = x;} inline void splay (int x) {int P;char flag1, flag2;top = 0;stk[top++] = x;for (int i = x;!isroot (i); i = Fa[i]) {stk[top++ ] = Fa[i];} for (int i = top-1; ~i;-I.) {pushdown (stk[i]);} while (!isroot (x)) {p = fa[x];if (IsRoot (p)) {rotate (x);} else {Flag1 = p = = Ch[fa[p]][1];flag2 = x = = Ch[p][1];if (flag1 ^ flag2) {rotate (x);} else {rotate (p);} Rotate (x);}}} inline void acc (int x) {int y = 0;while (x) {splay (x); ch[x][1] = Y;y = X;x = Fa[x];}} inline void make_root (int x) {ACC (x); splay (x); rev[x] ^= 1;} inline void link (int x, int y) {make_root (x); fa[x] = y;} inline void Cutt (int x, int y) {make_root (x); acc (y); splay (y); ch[y][0] = fa[x] = 0;} inline int fnd_root (int x) {ACC (x); splay (x); int i;for (i = x; ch[i][0]; i = ch[i][0]); return i;} int main (void) {//freopen ("In.txt", "R", stdin), scanf ("%d%d", &n, &m), while (m--) {scanf ("%s%d%d", OPR, &t1, &T2), if (opr[0] = = ' Q ') {make_root (T1);p UTS (fnd_root (t2) = = T1? "Yes": "No");} else if (opr[0] = = ' C ') {link (t1, t2);} else {Cutt (t1, T2);}} return 0;}
_bzoj2049 [Sdoi2008]cave Cave survey "LCT"