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
#include <iostream>
#define ll long long
using namespace std;
const int MAXN=2e5+5;
struct node {
int l,r;
int val;//dep
}tr[MAXN*40*2];
int n;
int tot,rootfa[MAXN],rootdep[MAXN];
void build (int &u,int l,int r){//建树
u=++tot;
if (l==r){
tr[u].val=l;
return ;
}
int m=(l+r)>>1;
build (tr[u].l,l,m);
build (tr[u].r,m+1,r);
}
void modify(int &u,int v,int l,int r,int pos,int val){//点修,修改fa/dep
u=++tot;
tr[u]=tr[v];
if (l==r){
tr[u].val=val;
return ;
}
int m=(l+r)>>1;
if (pos<=m){
modify(tr[u].l,tr[v].l,l,m,pos,val);
}else {
modify(tr[u].r,tr[v].r,m+1,r,pos,val);
}
}
int query (int u,int l,int r,int pos){
if (l==r){
return tr[u].val;
}
int m=(l+r)>>1;
if (pos<=m){
return query(tr[u].l,l,m,pos);
}else {
return query(tr[u].r,m+1,r,pos);
}
}
int find (int u,int x){
int fx= query(rootfa[u],1,n,x);
return fx==x?x:find (u,fx);
}
void merge (int u,int x,int y){
x=find (u-1,x);
y=find (u-1,y);
if (x==y){
rootfa[u]=rootfa[u-1];
rootdep[u]=rootdep[u-1];
}else {
int depx= query(rootdep[u-1],1,n,x);
int depy= query(rootdep[u-1],1,n,y);
if (depx<depy){
modify(rootfa[u],rootfa[u-1],1,n,x,y);
rootdep[u]=rootdep[u-1];
}else if (depx>depy){
modify(rootfa[u],rootfa[u-1],1,n,y,x);
rootdep[u]=rootdep[u-1];
}else {
modify(rootfa[u],rootfa[u-1],1,n,y,x);
Modify (rootdep[u], rootdep[u-1], 1, n, x, depx+1);
}
}
}
Int main ()
{
Int m;
Cin>>n>>m;
Build (rootfa[0], 1, n);
For (int ver=1; ver<=m; ver++){
Int opt, x, y;
Cin>>opt;
Switch (opt){
Case 1:
Cin>>x>>y;
Merge (ver, x, y);
Break;
Case 2:
Cin>>x;
Rootfa[ver]=rootfa[x];
Rootdep[ver]=rootdep[x];
Break;
Case 3:
Cin>>x>>y;
Rootfa[ver]=rootfa[ver-1];
Rootdep[ver]=rootdep[ver-1];
Int fx=find (ver, x);
Int fy=find (ver, y);
cout<<(fx==fy?1:0)<<endl;
Break;
}
}
Return 0;
}