#include #include #include #include #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; scanf("%d", &number); /*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; } const int LIM = 350; int N, M; int T[100002], S[100002]; int STx[100002], STy[100002], ST; int ch[100002], ls[100002]; int Find(int x) { if (T[x] != x) return Find(T[x]); return T[x]; } void Union(int x, int y) { if (S[x] < S[y]) { T[x] = y; S[y] = S[x] + S[y]; ch[ST] = x; } else { T[y] = x; S[x] = S[x] + S[y]; ch[ST] = y; } } int main() { N = getInt(); M = getInt(); for (int i = 1; i <= N; ++i) { T[i] = i; S[i] = 1; } for (int i = 1; i <= M; ++i) { int type = getInt(); if (type == 1) { int x = getInt(); int y = getInt(); ++ST; STx[ST] = x; STy[ST] = y; if (Find(x) != Find(y)) Union(Find(x), Find(y)); } else if (type == 2) { int tm = getInt(); int rem = ST - tm; for (int i = ST; i >= rem + 1; --i) { S[T[ch[i]]] -= S[ch[i]]; T[ch[i]] = ch[i]; } ST -= tm; } else if (type == 3) { int x = getInt(); int y = getInt(); if (Find(x) != Find(y)) printf("0\n"); else printf("1\n"); } } }