# 参考程序

#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=50000+10;
int tree[2][maxn][2],father[2][maxn],pp[2][maxn],sta[maxn],cal[2][maxn];
int size[2][maxn],siz[maxn],num[maxn];
bool bz[2][maxn],fz[maxn];
int h[maxn],go[maxn*2],next[maxn*2];
int i,j,k,l,r,u,v,w,x,y,t,n,m,tot,top,root;
ll ans,da,xi,p,q;
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
go[++tot]=y;
next[tot]=h[x];
h[x]=tot;
}
void dfs(int x,int y){
pp[0][x]=pp[1][x]=y;
cal[0][x]=cal[1][x]=x;
int t=h[x];
while (t){
if (go[t]!=y){
dfs(go[t],x);
siz[x]+=siz[go[t]]+1;
}
t=next[t];
}
}
void update(int p,int x){
size[p][x]=size[p][tree[p][x][0]]+size[p][tree[p][x][1]]+1;
if (p==0){
num[x]=num[tree[0][x][0]]+num[tree[0][x][1]]+siz[x];
}
else{
mx[x]=key[x];
if (tree[1][x][0]) mx[x]=max(mx[x],mx[tree[1][x][0]]);
if (tree[1][x][1]) mx[x]=max(mx[x],mx[tree[1][x][1]]);
mi[x]=key[x];
if (tree[1][x][0]) mi[x]=min(mi[x],mi[tree[1][x][0]]);
if (tree[1][x][1]) mi[x]=min(mi[x],mi[tree[1][x][1]]);
sum[x]=sum[tree[1][x][0]]+sum[tree[1][x][1]]+key[x];
}
}
int pd(int p,int x){
return tree[p][father[p][x]][1]==x;
}
void rotate(int p,int x){
int y=father[p][x],z=pd(p,x);
father[p][x]=father[p][y];
if (father[p][y]) tree[p][father[p][y]][pd(p,y)]=x;
tree[p][y][z]=tree[p][x][1-z];
if (tree[p][x][1-z]) father[p][tree[p][x][1-z]]=y;
tree[p][x][1-z]=y;
father[p][y]=x;
update(p,y);
update(p,x);
if (pp[p][y]) pp[p][x]=pp[p][y],pp[p][y]=0;
if (cal[p][y]) cal[p][x]=cal[p][y],cal[p][y]=0;
}
void markbz(int p,int x){
swap(tree[p][x][0],tree[p][x][1]);
bz[p][x]^=1;
}
void markst(int x,ll v){
fz[x]=1;
st[x]=mx[x]=mi[x]=key[x]=v;
sum[x]=(ll)v*size[1][x];
}
sum[x]+=(ll)v*size[1][x];
}
void clear(int p,int x){
if (bz[p][x]){
if (tree[p][x][0]) markbz(p,tree[p][x][0]);
if (tree[p][x][1]) markbz(p,tree[p][x][1]);
bz[p][x]=0;
}
if (p==1){
if (fz[x]){
if (tree[1][x][0]) markst(tree[1][x][0],st[x]);
if (tree[1][x][1]) markst(tree[1][x][1],st[x]);
fz[x]=0;
}
}
}
}
void remove(int p,int x,int y){
top=0;
while (x!=y){
sta[++top]=x;
x=father[p][x];
}
while (top) clear(p,sta[top--]);
}
void splay(int p,int x,int y){
remove(p,x,y);
while (father[p][x]!=y){
if (father[p][father[p][x]]!=y)
if (pd(p,x)==pd(p,father[p][x])) rotate(p,father[p][x]);else rotate(p,x);
rotate(p,x);
}
}
int kth(int p,int x,int y){
if (!x) return 0;
if (size[p][tree[p][x][0]]+1==y) return x;
clear(p,x);
if (size[p][tree[p][x][0]]+1>y) return kth(p,tree[p][x][0],y);
else return kth(p,tree[p][x][1],y-size[p][tree[p][x][0]]-1);
}
int findfr(int p,int x){
splay(p,x,0);
int k=size[p][tree[p][x][0]]+1;
int u=cal[p][x];
splay(1-p,u,0);
int v=kth(1-p,u,k);
splay(1-p,v,0);
return v;
}
int getsize(int x){
splay(0,x,0);
return siz[x]+num[tree[0][x][1]]+size[0][tree[0][x][1]]+1;
}
void real_empty(int p,int x,int y){
if (p==0){
splay(p,y,0);
siz[y]+=getsize(x);
update(0,y);
}
splay(p,y,0);
splay(p,x,y);
tree[p][y][1]=0;
father[p][x]=0;
pp[p][x]=y;
update(p,y);
}
void empty_real(int p,int x,int y){
if (p==0){
splay(p,y,0);
siz[y]-=getsize(x);
update(0,y);
}
splay(p,y,0);
splay(p,x,0);
cal[p][x]=0;
tree[p][y][1]=x;
father[p][x]=y;
pp[p][x]=0;
update(p,y);
}
void access(int x){
int j,k,y,z,u,v,w;
splay(0,x,0);
z=kth(0,tree[0][x][1],1);
if (z){
splay(0,z,x);
v=findfr(0,x);w=findfr(0,z);
real_empty(0,z,x);real_empty(1,w,v);
splay(0,z,0);splay(1,w,0);
cal[0][z]=w;cal[1][w]=z;
splay(0,x,0);splay(1,v,0);
cal[0][x]=v;cal[1][v]=x;
}
while (pp[0][x]){
y=pp[0][x];
splay(0,y,0);
z=kth(0,tree[0][y][1],1);
if (z){
splay(0,z,y);
v=findfr(0,y);w=findfr(0,z);
real_empty(0,z,y);real_empty(1,w,v);
splay(0,z,0);splay(1,w,0);
cal[0][z]=w;cal[1][w]=z;
splay(0,y,0);splay(1,v,0);
cal[0][y]=v;cal[1][v]=y;
}
splay(0,x,0);
z=kth(0,x,1);
splay(0,z,0);
v=findfr(0,y);w=findfr(0,z);
empty_real(0,z,y);empty_real(1,w,v);
splay(0,x,0);
}
}
void makeroot(int x){
access(x);
splay(0,x,0);
markbz(0,x);
int u=cal[0][x];
splay(1,u,0);
markbz(1,u);
}
void cut(int x,int y){
makeroot(x);
access(x);
splay(0,x,0);
siz[x]-=getsize(y);
update(0,x);
splay(0,y,0);
pp[0][y]=0;
int u=cal[0][y];
splay(1,u,0);
pp[1][u]=0;
}
makeroot(x);
makeroot(y);
access(x);
splay(0,x,0);
siz[x]+=getsize(y);
update(0,x);
splay(0,y,0);
pp[0][y]=x;
int u=cal[0][y],v=cal[0][x];
splay(1,u,0);
pp[1][u]=v;
}
void kf(int x,int y){
if (!x) return;
if (y>t){
t=y;
v=x;
}
clear(1,x);
if (mx[x]==mi[x]){
markst(x,int(sqrt(mx[x])));
return;
}
else if (mx[x]==mi[x]+1){
if (int(sqrt(mx[x]))==int(sqrt(mi[x]))) markst(x,int(sqrt(mx[x])));
return;
}
key[x]=int(sqrt(key[x]));
kf(tree[1][x][0],y+1);kf(tree[1][x][1],y+1);
update(1,x);
}
/*void kf(int x,int y){
if (!x) return;
if (y>t){
t=y;
v=x;
}
clear(1,x);
key[x]=int(sqrt(key[x]));
kf(tree[1][x][0],y+1);kf(tree[1][x][1],y+1);
update(1,x);
}*/
void likegcd(int a,int b,int c,int d){
if (a==0){
q=1;p=floor(d/c)+1;
return;
}
if (a>=b){
likegcd(a%b,b,c-d*(a/b),d);
q+=(ll)p*(a/b);
return;
}
if (c>d){
p=q=1;
return;
}
likegcd(d,c,b,a);
swap(p,q);
}
void write(ll x){
if (x<0){
putchar('-');
x=-x;
}
if (!x){
putchar('0');
return;
}
top=0;
while (x){
sta[++top]=x%10;
x/=10;
}
while (top) putchar('0'+sta[top--]);
}
int main(){
freopen("satori.in","r",stdin);freopen("satori.out","w",stdout);
fo(i,1,n-1){
}
dfs(1,0);
fo(i,1,n){
update(0,i);update(1,i);
}
root=1;
while (m--){
if (t==1){
cut(j,k);
}
else if (t==2){
root=j;
}
else if (t==3){
makeroot(j);
access(k);
splay(0,k,0);
u=cal[0][k];
splay(1,u,0);
}
else if (t==4){
makeroot(j);
access(k);
splay(0,k,0);
u=cal[0][k];
splay(1,u,0);
v=kth(1,u,1);
splay(1,v,0);
w=tree[1][v][1];
if (w){
father[1][w]=0;
tree[1][v][1]=0;
update(1,v);
x=kth(1,w,size[1][w]);
splay(1,x,0);
tree[1][x][1]=v;
father[1][v]=x;
update(1,x);
}
}
else if (t==5){
makeroot(j);
access(k);
splay(0,k,0);
u=cal[0][k];
splay(1,u,0);
t=-1;
kf(u,0);
splay(1,v,0);
}
else if (t==6){
makeroot(j);
access(k);
splay(0,k,0);
u=cal[0][k];
splay(1,u,0);
ans=sum[u];
write(ans);putchar('\n');
}
else if (t==7){
makeroot(j);
access(k);
splay(0,k,0);
u=cal[0][k];
splay(1,u,0);
da=mx[u];xi=mi[u];
if ((ll)da*q>(ll)xi*p){
swap(da,xi);
swap(p,q);
}
if ((ll)da*q==(ll)xi*p){
write(-1);putchar('\n');
continue;
}
likegcd(da,p,xi,q);
write(q);putchar(' ');write(p);putchar('\n');
}
else if (t==8){
makeroot(root);
access(j);
ans=siz[j]+1;
write(ans);putchar('\n');
}
}
}

#### ATM取款机模拟实战项目

1970年01月01日 08:00

#### GWXZ国庆模拟赛

2015-09-30 14:27:40

#### [51nod1743]雪之国度

2016-11-16 20:40:59

#### [WerKeyTom_FTD的模拟赛]永恒的契约

2016-11-03 20:37:05

#### 归途与征程

2016-05-28 14:57:47

#### GWXZnoip初赛模拟赛

2015-10-17 16:01:02

#### linux top

2010-08-05 10:28:00

#### [JZOJ5044]【NOI2017模拟4.4】Sone0

2017-04-07 10:31:40

#### [WerKeyTom_FTD的模拟赛]刻画在历史舞台上的群星

2016-11-03 21:17:09

#### [bzoj4899][WerKeyTom_FTD的模拟赛]记忆的轮廓

2016-11-03 21:31:02

## 不良信息举报

[WerKeyTom_FTD的模拟赛]Sone0