这题是真的智障,个个都说什么启发式合并,其实就是暴力把小的那棵splay拆掉重新插入,不过就是每次判断一下那棵树的节点数少拆那棵,这样就O(nlogn^2).....
#include#include #include #include #include #include using namespace std;int fa[110000];int findfa(int x){ if(fa[x]==x)return x; fa[x]=findfa(fa[x]);return fa[x];}//------f f--------struct node{ int x,f,c,son[2];}tr[110000];void update(int x){ int lc=tr[x].son[0],rc=tr[x].son[1]; tr[x].c=tr[lc].c+tr[rc].c+1;}void rotate(int x,int w){ int f=tr[x].f,ff=tr[f].f; int R,r; R=f,r=tr[x].son[w]; tr[R].son[1-w]=r; if(r!=0)tr[r].f=R; R=ff,r=x; if(tr[R].son[0]==f)tr[R].son[0]=r; else if(tr[R].son[1]==f)tr[R].son[1]=r; tr[r].f=R; R=x;r=f; tr[R].son[w]=r; tr[r].f=R; update(f); update(x);}void splay(int x,int rt){ while(tr[x].f!=rt) { int f=tr[x].f,ff=tr[f].f; if(ff==rt) { if(tr[f].son[0]==x)rotate(x,1); else rotate(x,0); } else { if(tr[ff].son[0]==f&&tr[f].son[0]==x){rotate(f,1);rotate(x,1);} else if(tr[ff].son[1]==f&&tr[f].son[1]==x){rotate(f,0);rotate(x,0);} else if(tr[ff].son[0]==f&&tr[f].son[1]==x){rotate(x,0);rotate(x,1);} else if(tr[ff].son[1]==f&&tr[f].son[0]==x){rotate(x,1);rotate(x,0);} } }}//-------splay i--------------int findip(int x,int y){ while(1) { if(tr[x].x =k)x=lc; else if(tr[lc].c+1 tr[fy].c)swap(fx,fy); if(fx!=fy)fa[fx]=fy, dfs(fx,fy), splay(fy,0); } scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%s%d%d",ss+1,&x,&y); if(x==0||y==0)continue; if(ss[1]=='B') { int fx=findfa(x),fy=findfa(y); if(tr[fx].c>tr[fy].c)swap(fx,fy); if(fx!=fy)fa[fx]=fy, dfs(fx,fy), splay(fy,0); } else printf("%d\n",findshuzi(fa[x],y)); } return 0;}