并查集都写不来了qwq
之前写的是错的
sz的初值都是0,这样怎么加就都是0了,水这道题还是可以,但是加强版就过不了了

1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<iostream> 6 7 using namespace std; 8 9 template<typename Q> Q &read(Q &x) { 10 static char c, f; 11 for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1; 12 for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0'; 13 if(f) x = -x; return x; 14 } 15 template<typename Q> Q read() { 16 static Q x; read(x); return x; 17 } 18 19 const int N = 200000 + 10; 20 21 struct Node *newnode(int, Node *, Node *); 22 23 struct Node { 24 int v; 25 Node *ch[2]; 26 int query(int l, int r, int k) { 27 if(l == r) return v; 28 int mid = (l + r) >> 1; 29 if(k <= mid) return ch[0]->query(l, mid, k); 30 else return ch[1]->query(mid + 1, r, k); 31 } 32 Node *modify(int l, int r, int k, int w) { 33 if(l == r) return newnode(w, 0, 0); 34 int mid = (l + r) >> 1; 35 if(k <= mid) return newnode(0, ch[0]->modify(l, mid, k, w), ch[1]); 36 return newnode(0, ch[0], ch[1]->modify(mid + 1, r, k, w)); 37 } 38 }pool[N * 40], *pis = pool; 39 40 Node *newnode(int v, Node *lc, Node *rc) { 41 pis->v = v, pis->ch[0] = lc, pis->ch[1] = rc; 42 return pis++; 43 } 44 45 int n, now; 46 47 struct parr { 48 Node *root[N]; 49 int vt; 50 void init(int w) { 51 root[0] = newnode(w, pis, pis); 52 } 53 int get(int k, int h) const { 54 return root[h]->query(1, n, k); 55 } 56 void modify(int k, int w) { 57 root[vt] = root[vt]->modify(1, n, k, w); 58 } 59 void newver(int h) { 60 root[++vt] = root[h]; 61 } 62 } sz, fa; 63 64 int find(int x) { 65 int y; 66 while(x) 67 y = x, x = fa.get(x, now); 68 return y; 69 } 70 71 void unite(int x, int y) { 72 sz.newver(now), fa.newver(now); 73 x = find(x), y = find(y); 74 if(x == y) return; 75 int sx = sz.get(x, now), sy = sz.get(y, now); 76 if(sx < sy) swap(x, y), swap(sx, sy); 77 fa.modify(y, x); 78 sz.modify(x, sx + sy); 79 now = sz.vt; 80 } 81 82 int version[N]; 83 int main() { 84 #ifdef DEBUG 85 freopen("in.txt", "r", stdin); 86 #endif 87 88 sz.init(1), fa.init(0); 89 int m, ans = 0; read(n), read(m); 90 for(int i = 1; i <= m; i++) { 91 int opt = read<int>(); 92 if(opt == 1) { 93 int u, v; read(u) ^= ans, read(v) ^= ans; 94 unite(u, v); 95 } else if(opt == 2) { 96 int h; read(h) ^= ans; 97 now = version[h]; 98 } else if(opt == 3) { 99 int u, v; read(u) ^= ans, read(v) ^= ans; 100 printf("%d ", ans = find(u) == find(v)); 101 } 102 version[i] = now; 103 } 104 105 return 0; 106 }