1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #define MAX 200005 vector<int> G[MAX]; int fa[MAX];
int finds(int x) { return x == fa[x] ? x : fa[x] = finds(fa[x]); }
void merge(int x, int y) { int nx = finds(x), ny = finds(y); if (nx == ny) { return; } vector<int> tmp; int tot = 0; for (int i = 0, j = 0; i < G[nx].size() || j < G[ny].size(); ) { if (tot > 10) { break; } tot++; if (i < G[nx].size() && j < G[ny].size()) { if (G[nx][i] > G[ny][j]) { tmp.push_back(G[nx][i]); i++; } else { tmp.push_back(G[ny][j]); j++; } continue; } if (i < G[nx].size()) { tmp.push_back(G[nx][i]); i++; } else { tmp.push_back(G[ny][j]); j++; } } G[nx] = tmp; fa[ny] = nx; G[ny].clear(); }
signed main() { IOS; int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { fa[i] = i; G[i].push_back(i); } while (m--) { int op, x, y; cin >> op >> x >> y; if (op == 1) { merge(x, y); } else { int nx = finds(x); if (G[nx].size() < y) { cout << -1 << endl; } else { cout << G[nx][y - 1] << endl; } } }
return 0; }
|