Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  Taichicchi
 *    created: 21.09.2020 08:57:17
 **/

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  bool unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return false;
    if (r[a] > r[b])
      swap(a, b);
    r[a] += r[b];
    r[b] = a;
    return true;
  }

  int find(int a) {
    if (r[a] < 0)
      return a;
    return r[a] = find(r[a]);
  }

  int size(int a) { return -r[find(a)]; }
};

int main() {
  int N, M;
  cin >> N >> M;

  UnionFind uf(N);

  rep(i, M) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    uf.unite(A, B);
  }

  int ans = 0;
  rep(i, N) { ans = max(ans, uf.size(i)); }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  Taichicchi
 *    created: 21.09.2020 08:41:57
 **/

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

int main() {
  struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
      assert(0 <= a && a < _n);
      assert(0 <= b && b < _n);
      int x = leader(a), y = leader(b);
      if (x == y)
        return x;
      if (-parent_or_size[x] < -parent_or_size[y])
        std::swap(x, y);
      parent_or_size[x] += parent_or_size[y];
      parent_or_size[y] = x;
      return x;
    }

    bool same(int a, int b) {
      assert(0 <= a && a < _n);
      assert(0 <= b && b < _n);
      return leader(a) == leader(b);
    }

    int leader(int a) {
      assert(0 <= a && a < _n);
      if (parent_or_size[a] < 0)
        return a;
      return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
      assert(0 <= a && a < _n);
      return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
      std::vector<int> leader_buf(_n), group_size(_n);
      for (int i = 0; i < _n; i++) {
        leader_buf[i] = leader(i);
        group_size[leader_buf[i]]++;
      }
      std::vector<std::vector<int>> result(_n);
      for (int i = 0; i < _n; i++) {
        result[i].reserve(group_size[i]);
      }
      for (int i = 0; i < _n; i++) {
        result[leader_buf[i]].push_back(i);
      }
      result.erase(
          std::remove_if(result.begin(), result.end(),
                         [&](const std::vector<int> &v) { return v.empty(); }),
          result.end());
      return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
  };

  int N, M;

  cin >> N >> M;

  dsu d(N);

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    d.merge(a, b);
  }

  int g;
  g = d.groups().size();

  vector<int> gs(g);

  rep(i, g) { gs[i] = d.groups()[i].size(); }

  cout << *max_element(gs.begin(), gs.end()) << endl;

  return 0;
}