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 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <utility> #define lson l, m, ls[rt] #define rson m + 1, r, rs[rt] #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 200000 + 5;
int n, m, root[maxn];
int tot, ls[maxn * 40], rs[maxn * 40], pre[maxn * 40], siz[maxn * 40]; void build(int l, int r, int& rt) { rt = ++tot; if (l == r) { pre[rt] = l; return ; } int m = (l + r) >> 1; build(lson); build(rson); } void update(int i, int x, int last, int l, int r, int& rt) { rt = ++tot; ls[rt] = ls[last]; rs[rt] = rs[last]; if (l == r) { pre[rt] = x; siz[rt] = siz[last]; return ; } int m = (l + r) >> 1; if (i <= m) update(i, x, ls[last], lson); else update(i, x, rs[last], rson); } void add(int i, int l, int r, int& rt) { if (l == r) { siz[rt]++; return ; } int m = (l + r) >> 1; if (i <= m) add(i, lson); else add(i, rson); } int query(int i, int l, int r, int rt) { if (l == r) return rt; int m = (l + r) >> 1; if (i <= m) return query(i, lson); else return query(i, rson); }
int find(int i, int x) { int p = query(x, 1, n, root[i]); return x == pre[p] ? p : find(i, pre[p]); } void join(int i, int x, int y) { x = find(i - 1, x); y = find(i - 1, y); if (pre[x] == pre[y]) return ; if (siz[x] > siz[y]) swap(x, y); update(pre[x], pre[y], root[i - 1], 1, n, root[i]); if (siz[x] == siz[y]) add(pre[y], 1, n, root[i]); }
int main() { scanf("%d%d", &n, &m); build(1, n, root[0]); for (int i = 1, op, a, b; i <= m; i++) { scanf("%d%d", &op, &a); if (op == 1) { root[i] = root[i - 1]; scanf("%d", &b); join(i, a, b); } else if (op == 2) { root[i] = root[a]; } else if (op == 3) { scanf("%d", &b); root[i] = root[i - 1]; a = find(i, a); b = find(i, b); if (pre[a] == pre[b]) puts("1"); else puts("0"); } } return 0; }
|