Advertisement
Giatro

Almost Union-Find

Jun 1st, 2020
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. typedef unsigned long long int ull;
  6. typedef long double lld;
  7. typedef pair<ll, ll> pll;
  8. typedef pair<int, int> pii;
  9. typedef vector<ll> vl;
  10. typedef vector<int> vi;
  11. typedef vector<pii> vii;
  12. typedef vector<pll> vll;
  13.  
  14. #define fastio                        \
  15.     ios_base::sync_with_stdio(false); \
  16.     cin.tie(NULL);                    \
  17.     cout.tie(NULL)
  18.  
  19. #define fr(i, n) for (ll i = 0; i < n; i++)
  20. #define frr(i, n) for (ll i = 1; i <= n; i++)
  21. #define frit(it, c) for (auto it = c.begin(); it != c.end(); it++)
  22.  
  23. #define sortvector(v) sort(v.begin(), v.end())
  24. #define sortvectorby(v, f) sort(v.begin(), v.end(), f)
  25.  
  26. #define pb push_back
  27. #define mk make_pair
  28. #define f first
  29. #define s second
  30.  
  31. #define debug(x) #x << " = " << x << " "
  32.  
  33. const int INF = 0x3f3f3f3f;
  34. const ll LINF = 0x3f3f3f3f3f3f3f;
  35. const ll M = 1000000007;
  36. // ===================================================== //
  37.  
  38. const int N = 112345;
  39. int uf[2 * N], w[2 * N], sum[2 * N];
  40. int reali[N];
  41. int curmax;
  42. int n, m, p, q, o;
  43.  
  44. void build() {
  45.     frr(i, n) {
  46.         uf[i] = reali[i] = sum[i] = i;
  47.         w[i] = 1;
  48.     }
  49.     curmax = n;
  50. }
  51.  
  52. int find(int u) { return (uf[u] == u ? u : uf[u] = find(uf[u])); }
  53.  
  54. void join(int u, int v) {
  55.     u = find(u);
  56.     v = find(v);
  57.  
  58.     if (u == v) return;
  59.  
  60.     if (w[u] < w[v]) swap(u, v);
  61.  
  62.     w[u] += w[v];
  63.     sum[u] += sum[v];
  64.     uf[v] = u;
  65. }
  66.  
  67. void move(int u, int v) {
  68.     int f = find(reali[u]);
  69.     if (f == find(reali[v])) return;
  70.     sum[f] -= u;
  71.     w[f]--;
  72.  
  73.     curmax++;
  74.     uf[curmax] = curmax;
  75.     sum[curmax] = u;
  76.     w[curmax] = 1;
  77.     reali[curmax] = 0;
  78.     reali[u] = curmax;
  79.     join(curmax, v);
  80. }
  81.  
  82. void info(int u) {
  83.     u = find(u);
  84.     cout << w[u] << ' ' << sum[u] << endl;
  85. }
  86.  
  87. void show() {
  88.     frr(i, curmax) {
  89.         cout << debug(i) << debug(uf[i]) << debug(w[i]);
  90.         cout << debug(sum[i]) << debug(reali[i]) << endl;
  91.     }
  92.     cout << endl;
  93. }
  94.  
  95. int main(int argc, char const *argv[]) {
  96.     fastio;
  97.  
  98.     while (cin >> n >> m) {
  99.         build();
  100.         // show();
  101.  
  102.         fr(i, m) {
  103.             cin >> o;
  104.             switch (o) {
  105.                 case 1:
  106.                     cin >> p >> q;
  107.                     join(reali[p], reali[q]);
  108.           show();
  109.                     break;
  110.                 case 2:
  111.                     cin >> p >> q;
  112.                     move(p, q);
  113.           show();
  114.                     break;
  115.                 case 3:
  116.                     cin >> p;
  117.                     info(reali[p]);
  118.                     break;
  119.             }
  120.         }
  121.     }
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement