#include //#include using namespace std; #define Nmax 100002 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; } int tata[Nmax], st[Nmax]; //stack < int > St; int Find ( int x ){ int rad, aux; for ( rad = x; rad != tata[rad]; rad = tata[rad] ); /* while ( x != tata[x] ){ aux = tata[x]; tata[x] = rad; x = aux; }*/ return rad; } int main() { int N, M, t, x, y; N = getInt(); M = getInt(); //scanf ( "%d%d", &N, &M ); for ( int i = 1; i <= N; ++i ) tata[i] = i; for ( int i = 1; i <= M; ++i ){ t = getInt(); //scanf ( "%d", &t ); if ( t == 1 ){ x = getInt(); y = getInt(); // scanf ( "%d%d", &x, &y ); int tX = Find(x); int tY = Find(y); //St.push ( tY ); st[++st[0]] = tY; tata[tY] = tX; continue; } if ( t == 2 ){ x = getInt(); //scanf ( "%d", &x ); for ( int j = 1; j <= x; ++j ){ /*int aux = St.top(); St.pop(); tata[aux] = aux;*/ tata[st[st[0]]] = st[st[0]]; st[0]--; } continue; } if ( t == 3 ){ x = getInt(); y = getInt(); // scanf ( "%d%d", &x, &y ); int tX = Find(x); int tY = Find(y); if ( tX == tY ) printf ( "1\n" ); else printf ( "0\n" ); continue; } } return 0; }