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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}

ll max(ll a, ll b) { return (a > b ? a : b); }
ll min(ll a, ll b) { return (a < b ? a : b); }
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}

int n, edges;
vector<vector<int>> graph;
vector<int> vis;
void addEdge(int u, int v) {
  graph[u].push_back(v);
  graph[v].push_back(u);
}
void init() {
  graph = vector<vector<int>>(n + 1);
  vis = vector<int>(n + 1, 0);
}
void input() {
  cin >> n >> edges;
  init();
  int u, v;
  for (int i = 0; i < edges; i++) {
    cin >> u >> v;
    addEdge(u, v);
  }
}
void dfs(int u) {
  vis[u] = 1;

  for (int i : graph[u])
    if (vis[i] == 0)
      dfs(i);
}
void solve_test() {
  ll ans = 0;
  input();

  for (int i = 1; i <= n; i++)
    if (vis[i] == 0) {
      dfs(i);
      ans++;
    }
  ans--;
  cout << ans;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  // cin >> t; while (t--)
  solve_test();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}

ll max(ll a, ll b) { return (a > b ? a : b); }
ll min(ll a, ll b) { return (a < b ? a : b); }
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
void yes_or_no(ll x) {
  if (x)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
// find_parent -> Returns representative of group
// unite -> merge two groups
struct dsu {
  vector<int> parent, sz;
  dsu(int n) {
    parent = vector<int>(n + 1);
    sz = vector<int>(n + 1);

    for (int i = 0; i <= n; i++)
      parent[i] = i, sz[i] = 1;
  }
  int find_parent(int u) {
    return (u == parent[u] ? u : find_parent(parent[u]));
  }
  void unite(int u, int v) {
    int a = find_parent(u), b = find_parent(v);
    if (sz[a] > sz[b])
      parent[b] = a, sz[a] += sz[b];
    else
      parent[a] = b, sz[b] += sz[a];
  }
};
void solve_test() {
  ll ans = 0;
  ll n, m;
  cin >> n >> m;
  dsu d(n);

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    d.unite(u, v);
  }
  ans--;

  for (int i = 1; i <= n; i++)
    if (d.find_parent(i) == i)
      ans++;

  cout << ans;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  // cin >> t; while (t--)
  solve_test();
  return 0;
}