LCT template question ....
#include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include < iostream> #include <cctype> #include <cmath> #define REP (i, L, R) for (int i=l; i<=r; i++) #define CLR (x, C) memset (x, C, sizeof (x)) #define Travel (x) for (Edge *p=fir[x]; p; p=p->n) #define L (x) c[x][0] #define R (x) c[x][1] #define F (x) father[x] #define H (x) head[x] #define MAXN 10009#define inf 0x7fffffffusing namespace std;inline int read () {int x=0, f=1; Char Ch=getchar (), while (!isdigit (CH)) {if (ch== '-') f=-1; Ch=getchar ();} while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar (); return x*f;} int n, c[maxn][2], HEAD[MAXN], father[maxn];bool rev[maxn];inline void pushdown (int x) {rev[x]^=1, rev[l (x)]^=1, Rev[r (x) ]^=1;swap (L (L (x)), R (L (x))), Swap (L (r (x)), R (r (x)));} inline void rotate (int x) {int y=f (x), Z=f (y), l= (c[y][1]==x), r=l^1;if (z) c[z][c[z][1]==y]=x;f (c[x][r]) =y, f (y) =x, f (x) = Z;C[Y][L]=C[X][R]; C[x][r]=y;} inline void splay (int x) {if (!x) return, if (Rev[x]) pushdown (x); int Y;while (f (x)) {if (Rev[y=f (x)]) pushdown (y), pushdown (x), if (!f (y)) h (x) =h (y), H (Y) =0;rotate (x);}} inline void Acc (int x) {int cmp=x, y; Splay (x); F (r (x)) =0, H (r (x)) =x, R (x) =0;while (H (x)) splay (Y=h (x)), F (r (Y)) =0, H (r (Y)) =y, R (y) =x, f (x) =y, h (x) =0, x=y; Splay (CMP);} inline void Eve (int x) {Acc (x); rev[x]^=1; swap (L (x), R (x));} inline int Top (int x) {Acc (x); int now=x; while (now) now=l (now); return now;} inline void Build () {Rep (i, 1, N) L (i) =r (i) =h (i) =f (i) = 0;} inline void Connect (int x, int y) {Eve (x); Eve (y); Splay (y); H (x) =y;} inline void Destroy (int x, int y) {Eve (x); ACC (y); F (x) =l (y) = 0;} inline void Query (int x, int y) {if (Top (x) ==top (y)) puts ("Yes"), Else puts ("No");} int main () {n=read (); int m=read (); Build (); Rep (I, 1, m) {char ch[5]; scanf ("%s", ch), int x=read (), Y=read (), if (ch[0]== ' C ') Connect (x, y); else if (ch[0]== ' D ') Destroy (x, y); else Query (x, y);} return 0;}
BZOJ-2049 [Sdoi2008] Cave survey