1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include<bits/stdc++.h> using namespace std; typedef long long s64;
const int ONE = 300005; const int MOD = 1e9+7;
int Type,n,Q; int opt,x,y; int fat[ONE]; int Ans;
int get() { int res=1,Q=1; char c; while( (c=getchar())<48 || c>57) if(c=='-')Q=-1; if(Q) res=c-48; while((c=getchar())>=48 && c<=57) res=res*10+c-48; return res*Q; }
int Find(int x) { if(fat[x]==x) return x; return fat[x]=Find(fat[x]); }
namespace LCT { int lc[ONE],rc[ONE],fa[ONE]; int hasRev[ONE]; int L[ONE],R[ONE],dis[ONE],size[ONE];
void pre() { for(int i=1;i<=n;i++) fat[i]=L[i]=R[i]=i, size[i]=1; }
void Update(int x) { size[x] = size[lc[x]] + size[rc[x]] + 1; }
bool is_real(int x) { return (lc[fa[x]]==x || rc[fa[x]]==x); }
void tag_rev(int x) { hasRev[x]^=1; swap(lc[x],rc[x]); }
void tag_down(int x) { if(hasRev[x]) { tag_rev(lc[x]); tag_rev(rc[x]); hasRev[x]=0; } }
void Turn(int x) { int y=fa[x],z=fa[y]; int b= x==lc[y]?rc[x]:lc[x];
fa[y]=x; fa[x]=z; if(b) fa[b]=y;
if(z) { if(y==lc[z]) lc[z]=x; else if(y==rc[z]) rc[z]=x; }
if(x==lc[y]) rc[x]=y,lc[y]=b; else lc[x]=y,rc[y]=b;
Update(y); Update(x); }
void Splay(int x) { static int anc[ONE]; int anc_num=0; anc[++anc_num] = x; for(int i=x; is_real(i); i=fa[i]) anc[++anc_num]=fa[i]; while(anc_num>0) tag_down(anc[anc_num--]); while(is_real(x)) { if(is_real(fa[x])) { if( (lc[fa[x]]==x) == (lc[fa[fa[x]]]==fa[x]) ) Turn(fa[x]); else Turn(x); } Turn(x); } }
void access(int x) { for(int p=x,q=0; p; q=p,p=fa[q]) { Splay(p); rc[p] = q; Update(p); } }
void make_root(int x) { access(x); Splay(x); tag_rev(x); }
int dist(int x,int y) { make_root(x); access(y); Splay(y); return size[y]-1; }
void link(int x,int y) { int lx,rx,ly,ry; int Fx=Find(x), Fy=Find(y); fat[Fy] = Fx; make_root(x); fa[x]=y; lx = L[Fx]; rx = R[Fx]; ly = L[Fy]; ry = R[Fy];
if(dist(lx,rx) >= dis[Fx]) dis[Fx]=dist(lx,rx), L[Fx]=lx, R[Fx]=rx; if(dist(ly,ry) >= dis[Fx]) dis[Fx]=dist(ly,ry), L[Fx]=ly, R[Fx]=ry;
if(dist(lx,ly) >= dis[Fx]) dis[Fx]=dist(lx,ly), L[Fx]=lx, R[Fx]=ly; if(dist(lx,ry) >= dis[Fx]) dis[Fx]=dist(lx,ry), L[Fx]=lx, R[Fx]=ry; if(dist(rx,ly) >= dis[Fx]) dis[Fx]=dist(rx,ly), L[Fx]=rx, R[Fx]=ly; if(dist(rx,ry) >= dis[Fx]) dis[Fx]=dist(rx,ry), L[Fx]=rx, R[Fx]=ry;
}
void Query(int x) { int Fx=Find(x); Ans = max( dist(L[Fx],x),dist(R[Fx],x) ); printf("%d\n",Ans); } }
int main() { Type=get(); n=get(); Q=get(); LCT::pre(); while(Q--) { opt = get(); if(opt == 1) { x=get(); y=get(); if(Type==1) x^=Ans, y^=Ans; LCT::link(x,y); } else { x=get(); if(Type==1) x^=Ans; LCT::Query(x); } }
}
|