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

#define errOut(s)                                                              \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << std::endl; }

#include <bits/stdc++.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// #include <boost/multi_array.hpp>
// #include <boost/optional.hpp>
// #include <boost/range/adaptors.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/range/irange.hpp>
#pragma clang diagnostic pop

using namespace std;

struct UnionFind {
  std::vector<int64_t> par;

  explicit UnionFind(int64_t N) : par(N) {
    for (int64_t i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int64_t root(int64_t x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = root(par[x]);
  }

  void unite(int64_t x, int64_t y) {
    int64_t const rx = root(x);
    int64_t const ry = root(y);
    if (rx == ry) {
      return;
    }
    par[rx] = ry;
  }

  bool same(int64_t x, int64_t y) {
    int64_t const rx = root(x);
    int64_t const ry = root(y);
    return rx == ry;
  }
};

int64_t solve(int64_t n, int64_t m, std::vector<int64_t> as,
              std::vector<int64_t> bs) {
  auto uf = UnionFind(n);
  for (auto i = 0; i < m; i++) {
    auto const a = as[i];
    auto const b = bs[i];
    uf.unite(a, b);
  }

  int64_t ret = 0;
  std::vector<int64_t> counts(n, 0);
  for (auto i = 0; i < n; i++) {
    auto const r = uf.root(i);
    counts[r]++;
    ret = std::max(counts[r], ret);
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int64_t n;
  std::cin >> n;
  int64_t m;
  std::cin >> m;
  std::vector<int64_t> as(m);
  std::vector<int64_t> bs(m);
  for (int i = 0; i < m; i++) {
    std::cin >> as[i];
    as[i]--;
    std::cin >> bs[i];
    bs[i]--;
  }
  auto const res = solve(n, m, std::move(as), std::move(bs));
  std::cout << res << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define errOut(s)                                                              \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << std::endl; }

#include <bits/stdc++.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// #include <boost/multi_array.hpp>
// #include <boost/optional.hpp>
// #include <boost/range/adaptors.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/range/irange.hpp>
#pragma clang diagnostic pop

using namespace std;

struct UnionFind {
  std::vector<int64_t> par;

  explicit UnionFind(int64_t N) : par(N) {
    for (int64_t i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int64_t root(int64_t x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = root(par[x]);
  }

  void unite(int64_t x, int64_t y) {
    int64_t const rx = root(x);
    int64_t const ry = root(y);
    if (rx == ry) {
      return;
    }
    par[rx] = ry;
  }

  bool same(int64_t x, int64_t y) {
    int64_t const rx = root(x);
    int64_t const ry = root(y);
    return rx == ry;
  }
};

int64_t solve(int64_t n, int64_t m, std::vector<int64_t> as,
              std::vector<int64_t> bs) {
  auto uf = UnionFind(n);
  for (auto i = 0; i < m; i++) {
    auto const a = as[i];
    auto const b = bs[i];
    uf.unite(a, b);
  }

  std::multiset<int64_t> roots;
  for (auto i = 0; i < n; i++) {
    roots.insert(uf.root(i));
  }
  int64_t ret = 0;
  for (auto r : roots) {
    ret = std::max(static_cast<int64_t>(roots.count(r)), ret);
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int64_t n;
  std::cin >> n;
  int64_t m;
  std::cin >> m;
  std::vector<int64_t> as(m);
  std::vector<int64_t> bs(m);
  for (int i = 0; i < m; i++) {
    std::cin >> as[i];
    as[i]--;
    std::cin >> bs[i];
    bs[i]--;
  }
  auto const res = solve(n, m, std::move(as), std::move(bs));
  std::cout << res << std::endl;
  return 0;
}