#include #include #include using namespace std; const int buffer=1<<13; char buff[buffer]; int cnt=0; //getInt() reads and returns one integer at a time int getInt() { int number = 0; while(buff[cnt] < '0' || '9' < buff[cnt]) if(++cnt >= buffer) fread(buff, buffer, 1, stdin), cnt = 0; while('0' <= buff[cnt] && buff[cnt] <= '9') { number = number * 10 + buff[cnt] - '0'; if(++cnt >= buffer) fread(buff, buffer, 1, stdin), cnt = 0; } return number; } #define maxn 300010 int ct, n, m; vector t[maxn]; vector > p; int tata(int nod) { while(t[nod].size() != 0) nod=t[nod][t[nod].size()-1]; return nod; } void setTata(int nod) { while(t[nod].size() != 0) { t[nod].push_back(ct); p.push_back(make_pair(ct, nod)); nod=t[nod][t[nod].size()-2]; } t[nod].push_back(ct); p.push_back(make_pair(ct, nod)); } int main() { // freopen("undo.in", "r", stdin); n=getInt(); m=getInt(); ct=n; while(m--) { int tip = getInt(); if(tip==1) { ++ct; int x=getInt(), y=getInt(); int t1 = tata(x), t2 = tata(y); // printf("!%d %d\n", t1, t2); if(t1==t2) continue; setTata(x); setTata(y); t1=tata(x); t2=tata(y); // printf("%d %d\n", t1, t2); } else if(tip==2) { int x=getInt(); ct-=x; int j=p.size()-1; while(j>=0) { if(p[j].first>ct) { t[p[j].second].pop_back(); --j; p.pop_back(); } else break; } } else { int x=getInt(), y=getInt(); int t1 = tata(x), t2 = tata(y); if(t1==t2) printf("1\n"); else printf("0\n"); } } return 0; }