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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
constexpr double PI = 3.141592653589793;
constexpr int INF = 1e9 + 10;
constexpr ll INFL = 1e18 + 10;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
//    cout << setprecision(20) << ans << "\\n";

class DisjointSet {
private:
  std::vector<int> r;

public:
  DisjointSet(int n) : r(n, -1) {}

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);

    if (x != y) {
      if (x > y) {
        std::swap(x, y);
      }
      r[x] += r[y];
      r[y] = x;
    }

    return x != y;
  }

  bool same(int x, int y) { return root(x) == root(y); }

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

private:
  int root(int x) { return r[x] < 0 ? x : r[x] = root(r[x]); }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  DisjointSet uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }

  int ans = -1;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, uf.size(i));
  }

  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
constexpr double PI = 3.141592653589793;
constexpr int INF = 1e9 + 10;
constexpr ll INFL = 1e18 + 10;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
//    cout << setprecision(20) << ans << "\\n";

class DisjointSet {
private:
  std::vector<int> parent;

public:
  DisjointSet(int n) : parent(n, -1) {}

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);

    if (x != y) {
      if (x > y) {
        std::swap(x, y);
      }
      parent[x] += parent[y];
      parent[y] = x;
    }

    return x != y;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int numOfGroups() {
    int c =
        count_if(parent.begin(), parent.end(), [](int x) { return x < -1; });
    return c;
  }

  vector<int> roots() {
    vector<int> ret;
    for (size_t i = 0; i < parent.size(); ++i) {
      if (parent[i] < 0)
        ret.emplace_back(i);
    }
    return ret;
  }

  vector<int> members(int x) {
    int xRoot = root(x);
    vector<int> ret;
    for (size_t i = 0; i < parent.size(); ++i) {
      if (root(i) == xRoot)
        ret.emplace_back(i);
    }
    return ret;
  }

  map<int, vector<int>> allGroups() {
    map<int, vector<int>> ret;
    auto allRoots = roots();
    for (auto e : allRoots) {
      ret.insert({e, members(e)});
    }
    return ret;
  }

  vector<int> get() { return parent; }

private:
  int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  DisjointSet uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }

  auto allMembers = uf.allGroups();
  //   for (auto e : allMembers) {
  //     cout << e.first << "\\n";
  //     for (auto es : e.second) {
  //       cout << es << " ";
  //     }
  //     cout << "\\n";
  //   }
  //   cout << "\\n";
  int ans = -1;
  for (auto &e : allMembers) {
    int len = e.second.size();
    if (ans < len) {
      ans = e.second.size();
    }
  }
  cout << ans << "\\n";
  return 0;
}