#include #include using namespace std; const int buffer = 1 << 13; char buff[buffer]; int cnt = 0; int sef[100001]; int operatii[100001], c = 0; int rez[100001], r = 0; int getInt(); inline bool find(int x, int y); inline void unire(int x, int y); inline void dezunire(int x); inline void initial(); int main() { initial(); int n = getInt(), m = getInt(); int op, x, y; for (int i = 0; i < m; i++) { op = getInt(); x = getInt(); if (op == 2) dezunire(x); y = getInt(); if (op == 1) unire(x, y); if (op == 3) { rez[r++] = find(x, y); } } for (int i = 0; i < r; i++) { printf("%d\n", rez[i]); } } void initial() { for (int i = 0; i < 100001; i++) sef[i] = i; } void dezunire(int x) { while (x--) { sef[operatii[c - 1]] = operatii[--c]; } } void unire(int x, int y) { while (x != sef[x]) x = sef[x]; while (y != sef[y]) y = sef[y]; sef[x] = y; operatii[c++] = x; } bool find(int x, int y) { while (x != sef[x]) x = sef[x]; while (y != sef[y]) y = sef[y]; if (x == y) return true; return false; } 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; }