/* -*- coding: utf-8 -*- * * 2290.cc: No.2290 UnUnion Find - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ struct UFT { vector links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ UFT uft; /* subroutines */ /* main */ int main() { int n, qn; scanf("%d%d", &n, &qn); uft.init(n); unordered_set as; for (int i = 0; i < n; i++) as.insert(i); while (qn--) { int op; scanf("%d", &op); if (op == 1) { int u, v; scanf("%d%d", &u, &v); u--, v--; int ru = uft.root(u), rv = uft.root(v); int r = uft.merge(ru, rv); as.erase(r != ru ? ru : rv); } else { int v; scanf("%d", &v); v--; if (as.size() == 1) puts("-1"); else { int rv = uft.root(v); auto sit = as.begin(); if (*sit == rv) sit++; printf("%d\n", *sit + 1); } } } return 0; }