模板—LCT
#include<iostream> #include<cstring> #include<cstdio> #define LL long long using namespace std; int f[100000],c[100000][2],st[100000]; bool r[100000]; #define l(x) c[x][0] #define r(x) c[x][1] inline bool nroot(int x){return c[f[x]][0]==x||c[f[x]][1]==x;} void pushup(int x){;} void pushr(int x){int t=l(x);l(x)=r(x);r(x)=t;r[x]^=1;} void pushdown(int x) { if(!r[x])return; if(l(x))pushr(l(x)); if(r(x))pushr(r(x)); r[x]=0; } void rotate(int x)// { int y=f[x],z=f[y],k=r(y)==x,w=c[x][!k]; if(nroot(y))c[z][r(z)==y]=x;c[x][!k]=y;c[y][k]=w; if(w)f[w]=y;f[y]=x;f[x]=z; pushup(y); } void splay(int x)// { int y=x,z=0;st[++z]=y; while(nroot(y))st[++z]=y=f[y]; while(z)pushdown(st[z--]); while(nroot(x)) { y=f[x];z=f[y]; if(nroot(y))rotate((l(y)==x)^(l(z)==y)?x:y); rotate(x); } pushup(x); } void access(int x){for(int y=0;x;x=f[y=x])splay(x),r(x)=y,pushup(x);}// void makeroot(int x){access(x);splay(x);pushr(x);}// int findroot(int x){access(x);splay(x);while(l(x))pushdown(x),x=l(x);splay(x);return x;}// void split(int x,int y){makeroot(x);access(y);splay(y);}// void link(int x,int y){makeroot(x);if(findroot(y)!=x)f[x]=y;}// void cut(int x,int y) { makeroot(x); if(findroot(y)==x&&f[y]==x&&!l(y)) f[y]=r(x)=0,pushup(x); } int n,m; signed main() { cin>>n>>m; char opt[10];int u,v; for(int i=1;i<=m;i++) { cin>>opt; if(opt[0]=='C')cin>>u>>v,link(u,v); if(opt[0]=='D')cin>>u>>v,cut(u,v); if(opt[0]=='Q') { cin>>u>>v; if(findroot(u)==findroot(v))puts("Yes"); else puts("No"); } } }
波澜前,面不惊。