Mivik

tears.cpp

Jan 30th, 2022 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <ametus.h>
  2.  
  3. MI cin;
  4.  
  5. const int $n = 600005;
  6.  
  7. int n, m, f[$n], s[$n], e[$n][2];
  8. std::set<std::pair<int, int>> w[$n];
  9. inline int getf(int x) { return f[x] == x? x: (f[x] = getf(f[x])); }
  10. inline void merge(int x, int y) {
  11.     static int q[$n][2], hd, tl;
  12.     hd = 1, tl = 0; q[++tl][0] = x; q[tl][1] = y;
  13.     while (hd <= tl) {
  14.         x = getf(q[hd][0]); y = getf(q[hd++][1]); if (x == y) continue;
  15.         if (w[x].size() > w[y].size()) std::swap(x, y);
  16.         f[x] = y; s[y] += s[x]; s[x] = 0;
  17.         for (auto [t, d] : w[x])
  18.             if (getf(t) == y) q[++tl][0] = e[d][0], q[tl][1] = e[d][1];
  19.             else w[y].emplace(t, d);
  20.     }
  21. }
  22. int main() {
  23.     cin > n > m;
  24.     E(i, n) f[i] = i, s[i] = 1;
  25.     int tp = 0;
  26.     E(m) {
  27.         let op = R;
  28.         switch (op) {
  29.             case 1: {
  30.                 let a = getf(R), b = getf(R), c = R, d = R;
  31.                 if (a == b) merge(c, d);
  32.                 else {
  33.                     e[++tp][0] = c; e[tp][1] = d;
  34.                     w[a].emplace(b, tp);
  35.                     w[b].emplace(a, tp);
  36.                 }
  37.                 break;
  38.             }
  39.             case 2: merge(R, R); break;
  40.             case 3: cout < ((getf(R) == getf(R))? "entangled\n": "separate\n"); break;
  41.             case 4: cout < s[getf(R)] < endl; break;
  42.             default: assert(0);
  43.         }
  44.     }
  45. }
  46.  
Add Comment
Please, Sign In to add comment