Advertisement
Soupborsh

snm1

Mar 26th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | Source Code | 0 0
  1. // #include <algorithm>
  2. #include <cstdio>
  3. #include <utility>
  4. #include <vector>
  5.  
  6. #define ULL unsigned long long
  7. #define LL long long
  8. #define uint unsigned int
  9.  
  10. uint sz_max = 1;
  11.  
  12. std::vector<uint> p, sz;
  13.  
  14. // O(log(n))
  15. uint get(uint v) {
  16.   if (v == p[v])
  17.     return v;
  18.   p[v] = get(p[v]);
  19.   return p[v];
  20. }
  21.  
  22. bool check(uint v, uint u) {
  23.   if (get(v) == get(u))
  24.     return true;
  25.   else
  26.     return false;
  27. }
  28.  
  29. bool unite(uint v, uint u) {
  30.   int x = get(v);
  31.   int y = get(u);
  32.   if (x == y)
  33.     return false;
  34.   if (sz[x] > sz[y])
  35.     std::swap(x, y);
  36.   p[x] = y;
  37.   sz[y] += sz[x];
  38.  
  39.   if (sz[y] > sz_max) {
  40.     sz_max = sz[y];
  41.   }
  42.   return true;
  43. }
  44.  
  45. int main() {
  46.   uint n = 0;
  47.   scanf("%u", &n);
  48.  
  49.   p.resize(n, 0);
  50.   sz.resize(n, 0);
  51.  
  52.   uint n_cmp = n;
  53.  
  54.   for (uint i = 0; i < n; i++) {
  55.     p[i] = i;
  56.     sz[i] = 1;
  57.   }
  58.  
  59.   uint q;
  60.   scanf("%u", &q);
  61.  
  62.   for (uint i = 0; i < q; i++) {
  63.     uint v, u;
  64.     scanf("%u %u", &v, &u);
  65.     if (unite(--v, --u)) {
  66.       n_cmp--;
  67.     }
  68.  
  69.     printf("%u %u\n", n_cmp, sz_max);
  70.   }
  71.  
  72.   return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement