#include<bits/stdc++.h> #define reg register int #define il inline #define fi first #define se second #define mk(a,b) make_pair(a,b) #define numb (ch^'0') using namespace std; typedef long long ll; template<class T>il void rd(T &x){ char ch;x=0;bool fl=false; while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true); for(x=numb;isdigit(ch=getchar());x=x*10+numb); (fl==true)&&(x=-x); } template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');} template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');} template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{ const int N=100000+5; const int inf=0x3f3f3f3f; int n,q; struct node{ int ch[2],fa; int lmn,rmn; int co;//1:white 0:black int sz; multiset<int>s; multiset<int>::iterator it; void top(){ if(s.size()) return *s.begin(); return inf; } void ins(int c){ s.insert(c); } void dele(int c){ it=s.lower_bound(c); if(it!=s.end()) s.erase(it); } }t[N]; void nrt(int x){ return t[t[x].fa].ch[0]==x||t[t[x].fa].ch[1]==x; } void pushup(int x){ if(!x) return; t[x].sz=t[t[x].ch[0]].sz+t[t[x].ch[1]].sz+1; t[x].lmn=min(t[t[x].ch[0]].lmn,t[t[x].ch[0]].sz+min(t[x].co?0:inf,min(t[x].top(),t[t[x].ch[1]].lmn+1))); t[x].rmn=min(t[t[x].ch[1]].rmn,t[t[x].ch[1]].sz+min(t[x].co?0:inf,min(t[x].top(),t[t[x].ch[0]].rmn+1))); } void rotate(int x){ int y=t[x].fa,d=t[y].ch[1]==x; t[t[y].ch[d]=t[x].ch[!d]].fa=y; if(nrt(y)) t[t[x].fa=t[y].fa].ch[t[t[y].fa].ch[1]==y]=x; else t[x].fa=t[y].fa; t[t[x].ch[!d]=y].fa=x; pushup(y); } void splay(int x){ int y,z; while(nrt(x)){ y=t[x].fa,z=t[y].fa; if(nrt(y)){ rotate((t[y].ch[0]==x)==(t[z].ch[0]==y)?y:x); } rotate(x); } pushup(x); } void access(int x){ for(reg y=0;x;x=t[x].fa){ splay(x); if(y) t[x].dele(t[y].lmn+1); if(t[x].ch[1]) t[x].ins(t[t[x].ch[1]].lmn+1); t[x].ch[1]=y; pushup(x); } } struct edge{ int nxt,to; }e[2*N]; int hd[N],cnt; void add(int x,int y){ e[++cnt].nxt=hd[x]; e[cnt].to=y; hd[x]=cnt; } void dfs(int x,int fa){ t[x].sz=1; t[x].lmn=inf;t[x].rmn=inf; t[x].co=0; for(reg i=hd[x];i;i=e[i].nxt){ int y=e[i].to; if(y==fa) continue; t[y].fa=x; dfs(y,x); } } int main(){ rd(n); int x,y; for(reg i=1;i<n;++i){ rd(x);rd(y); add(x,y);add(y,x); } dfs(1,0); rd(q); int op; while(q--){ rd(op);rd(x); if(!op){ }else{ access(x);splay(x); } } return 0; } } signed main(){ Miracle::main(); return 0; } /* Author: *Miracle* Date: 2019/3/14 21:49:23 */
Qt5
猜你喜欢
转载自www.cnblogs.com/Miracevin/p/10534054.html
今日推荐
周排行