如题,求救
#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T &x) {
x = 0;
bool flag = false;
char c = getchar();
while (c < '0' || c > '9') {
flag ^= (c == '-');
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x = flag ? -x : x;
}
template<typename T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N=3e5+5,M=3e5+5;
struct Query{
int type;
int a,b;
Query(int type=0,int a=0,int b=0){
this->type=type;
this->a=a;
this->b=b;
}
};
struct Chain{
int top,head[M],to[M],nxt[M];
void addedge(int u,int v){
nxt[++top]=head[u];
head[u]=top;
to[top]=v;
}
};
int fa[N];
int sz[N];
int sta[M],top;
Query que[M];
int n,m,opt;
int ans[M];
Chain mp;
void init(){
for (int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
}
int findroot(int x){
return x==fa[x]?x:fa[x]=findroot(fa[x]);
}
void merge(int x,int y){
x=findroot(x),y=findroot(y);
if (x==y) return;
if (sz[x]<sz[y]) swap(x,y);
fa[y]=x;
sz[x]+=sz[y];
sta[++top]=y;
}
void back(){
int y=sta[top--];
sz[fa[y]]-=sz[y];
fa[y]=y;
}
void turn_back(int t){
while (top>t) back();
}
void dfs(int x){
int tim=top;
if (que[x].type==3){
ans[x]=(findroot(que[x].a)==findroot(que[x].b));
}
if (que[x].type==1){
merge(que[x].a,que[x].b);
}
for (int i=mp.head[x];i;i=mp.nxt[i]){
int v=mp.to[i];
dfs(v);
}
turn_back(tim);
}
signed main(){
read(n),read(m);
for (int i=1,a,b;i<=m;i++){
read(opt),read(a);
if (opt==2){
mp.addedge(a,i);
}
else{
read(b);
que[i]=Query(opt,a,b);
mp.addedge(i-1,i);
}
}
init();
dfs(0);
for (int i=1;i<=m;i++){
if (que[i].type==3){
write(ans[i]);
putchar('\n');
}
}
}