loj#6038 「雅礼集训 2017 Day5」远行

分析

代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define _(x) int x
#define mp make_pair
#define pr pair<int,int>
int fa[300100],son[300100][2],a[300100],siz[300100],r[300100];
inline void up(int x){siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;return;}
inline void rev(int x){swap(son[x][0],son[x][1]);r[x]^=1;return;}
inline void pd(int x){if(r[x]){for(_(i)=0;i<2;i++)if(son[x][i])rev(son[x][i]);r[x]=0;}return;}
inline int notroot(int x){return x==son[fa[x]][0]||x==son[fa[x]][1];}
inline void pushall(int x){if(notroot(x))pushall(fa[x]);pd(x);return;}
inline int gs(int x){return x==son[fa[x]][1];}
inline void rot(int x){
    int y=fa[x],z=fa[y],b=gs(x),c=gs(y),d=son[x][!b];
    if(notroot(y))son[z][c]=x;
    fa[x]=z;if(d)fa[d]=y;
    son[y][b]=d,son[x][!b]=y;
    fa[y]=x;up(y),up(x);return;
}
inline void splay(int x){
    pushall(x);
    while(notroot(x)){
      int y=fa[x],z=fa[y];
      if(notroot(y)){
        if(gs(x)==gs(y))rot(y);
          else rot(x);
      }
      rot(x);
    }
    return;
}
inline void access(int x){for(int y=0;x;y=x,x=fa[x])splay(x),son[x][1]=y,up(x);return;}
inline void makeroot(int x){access(x),splay(x),rev(x);return;}
inline void spt(int x,int y){makeroot(x),access(y),splay(y);return;}
inline void link(int x,int y){makeroot(x);fa[x]=y;return;}
pr d[300100];
int n,m,q,f[300100],p1,p2,mx;
inline int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
inline void work(int x,int y){
    spt(x,y);int res=siz[y]-1;
    if(res>mx)mx=res,p1=x,p2=y;
    return;
}
inline void mer(int x,int y){
    pr a=d[sf(x)],b=d[sf(y)];
    mx=0;link(x,y);
    work(a.fi,a.se),work(b.fi,b.se);
    work(a.fi,b.fi),work(a.fi,b.se);
    work(a.se,b.fi),work(a.se,b.se);
    f[sf(x)]=sf(y);d[sf(y)]=mp(p1,p2);
    return;
}
inline int que(int x){
    pr a=d[sf(x)];
    int res=0;
    spt(a.fi,x),res=max(res,siz[x]-1);
    spt(a.se,x),res=max(res,siz[x]-1);
    return res;
}
int main(){
    int i,j,k,la=0,opt,x,y;
    scanf("%d%d%d",&opt,&n,&q);
    for(i=1;i<=n;i++)f[i]=i,d[i]=mp(i,i),siz[i]=1;
    while(q--){
      scanf("%d",&k);
      if(k==1)scanf("%d%d",&x,&y);
        else scanf("%d",&x);
      x^=(la*opt),y^=(la*opt);
      if(k==1)mer(x,y);
        else printf("%d\n",la=que(x));
    }
    return 0;
}

 

posted @ 2019-10-30 14:42  水题收割者  阅读(206)  评论(0编辑  收藏  举报