#include #include #include using namespace std; struct UnionFindTree { vector parent; int cnt; set roots; UnionFindTree(int n) { parent.assign(n, -1); cnt = n; for (int i = 0; i < n; i++) { roots.insert(i); } } int findRoot(int a) { if (parent[a] < 0) { return a; } int r = findRoot(parent[a]); if (roots.count(a) && a != r) { roots.erase(a); } parent[a] = r; return r; } bool unite(int a, int b) { int x = findRoot(a), y = findRoot(b); if (x == y) { return false; } if (size(x) < size(y)) { swap(x, y); } roots.erase(y); parent[x] += parent[y]; parent[y] = x; cnt--; return true; } bool sameRoot(int a, int b) { return findRoot(a) == findRoot(b); } int size(int a) { return -parent[findRoot(a)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; UnionFindTree uf(n); int typ, u, v; for (int i = 0; i < q; i++) { cin >> typ; switch (typ) { case 1: cin >> u >> v; u--, v--; uf.unite(u, v); break; case 2: cin >> v; v--; int r = uf.findRoot(v); int ans = -1; for (auto it = uf.roots.begin(); it != uf.roots.end(); it++) { int v2 = *it; if (r != v2) { ans = v2 + 1; break; } } cout << ans << "\n"; break; } } return 0; }