#include 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 n, m; const int N = 100001; int par[N]; int hh[N]; int step = 0; int undoHPos = 0; int undoH[N * 20]; int undoHStep[N * 20]; int undoPPos = 0; int undoPSon[N * 20]; int undoPPar[N * 20]; int undoPStep[N * 20]; int parent(int x) { if (par[x] == x) return x; else return parent(par[x]); } void uni(int x, int y) { if (x == y) { return; } if (hh[x] < hh[y]) { undoPPos++; undoPSon[undoPPos] = x; undoPPar[undoPPos] = par[x]; undoPStep[undoPPos] = step; par[x] = y; } else if (hh[x] > hh[y]) { undoPPos++; undoPSon[undoPPos] = y; undoPPar[undoPPos] = par[y]; undoPStep[undoPPos] = step; par[y] = x; } else { undoPPos++; undoPSon[undoPPos] = y; undoPPar[undoPPos] = par[y]; undoPStep[undoPPos] = step; par[y] = x; undoHPos++; undoH[undoHPos] = x; undoHStep[undoHPos] = step; hh[x] = hh[x] + 1; } } int main() { n = getInt(); m = getInt(); for (int i = 0; i < n; i++) { par[i] = i; hh[i] = 0; } for (int nro = 0; nro < m; nro++) { int op = getInt(); if (op == 1) { int x = getInt(); x--; int y = getInt(); y--; step++; uni(parent(x), parent(y)); } else if (op == 2) { int nru = getInt(); step -= nru; while (undoPPos >= 0 && undoPStep[undoPPos] > step) { par[undoPSon[undoPPos]] = undoPPar[undoPPos]; undoPPos--; } while (undoHPos >= 0 && undoHStep[undoHPos] > step) { hh[undoH[undoHPos]]--; undoHPos--; } // Undo. } else if (op == 3) { int x = getInt(); x--; int y = getInt(); y--; if (parent(x) == parent(y)) { printf("1\n"); } else { printf("0\n"); } } } }