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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>
#include <utility>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define speedio                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define deb(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define deb(x...)
#endif

#define ll long long
#define int long long
#define pb push_back
#define ff first
#define sec second
#define eb emplace_back
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define all(x) x.begin(), x.end()
#define sz(v) ((int)(v.size()))
#define mem(a, val) memset(&a, val, sizeof(a))
#define mid(s, e) ((s) + ((e) - (s)) / 2)
#define endl '\\n'

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<string> vs;

typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

#define fo(i, a, b) for (ll i = (int)(a); i <= (int)(b); ++i)
#define fr(i, b) for (ll i = (int)0; i < (int)(b); ++i)
#define rf(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define read(a, n)                                                             \\
  for (int i = 0; i < n; ++i)                                                  \\
    cin >> a[i];
#define show(a, n)                                                             \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << a[i] << ' ';                                                       \\
  cout << endl;

template <typename... T> void rd(T &...args) { ((cin >> args), ...); }
template <typename... T> void pp(T... args) {
  ((cout << args << " "), ...);
  cout << "\\n";
}

const int MOD = 1e9 + 7;
const int INF = 1e18;

void add(ll &x, ll y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  if (x < 0)
    x += MOD;
}
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> oset;

const int N = 2e5 + 10;
vi parent(N);

void make_set(int v) { parent[v] = v; }

int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b)
    parent[b] = a;
}

void solve() {

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++)
    make_set(i);

  for (int i = 0; i < m; i++) {
    int a, b;
    rd(a, b);
    a--;
    b--;
    union_sets(a, b);
  }

  vi freq(N, 0);
  int mx = 0;

  for (int i = 0; i < n; i++) {
    int x = find_set(i);
    freq[x]++;
    mx = max(freq[x], mx);
  }

  pp(mx);
}

int32_t main() {
  speedio;
  cout << fixed << setprecision(12);
  /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif
  */
  int T = 1; // cin>>T;
  while (T--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>
#include <utility>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define speedio                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define deb(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define deb(x...)
#endif

#define ll long long
#define int long long
#define pb push_back
#define ff first
#define sec second
#define eb emplace_back
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define all(x) x.begin(), x.end()
#define sz(v) ((int)(v.size()))
#define mem(a, val) memset(&a, val, sizeof(a))
#define mid(s, e) ((s) + ((e) - (s)) / 2)
#define endl '\\n'

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<string> vs;

typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

#define fo(i, a, b) for (ll i = (int)(a); i <= (int)(b); ++i)
#define fr(i, b) for (ll i = (int)0; i < (int)(b); ++i)
#define rf(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define read(a, n)                                                             \\
  for (int i = 0; i < n; ++i)                                                  \\
    cin >> a[i];
#define show(a, n)                                                             \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << a[i] << ' ';                                                       \\
  cout << endl;

template <typename... T> void rd(T &...args) { ((cin >> args), ...); }
template <typename... T> void pp(T... args) {
  ((cout << args << " "), ...);
  cout << "\\n";
}

const int MOD = 1e9 + 7;
const int INF = 1e18;

void add(ll &x, ll y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  if (x < 0)
    x += MOD;
}
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> oset;

const int N = 2e5 + 10;
vi parent(N);

void make_set(int v) { parent[v] = v; }

int find_set(int v) {
  if (v == parent[v])
    return v;
  return find_set(parent[v]);
}

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b)
    parent[b] = a;
}

void solve() {

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++)
    make_set(i);

  for (int i = 0; i < m; i++) {
    int a, b;
    rd(a, b);
    a--;
    b--;
    union_sets(a, b);
  }

  vi freq(N, 0);
  int mx = 0;

  for (int i = 0; i < n; i++) {
    int x = find_set(i);
    freq[x]++;
    mx = max(freq[x], mx);
  }

  pp(mx);
}

int32_t main() {
  speedio;
  cout << fixed << setprecision(12);
  /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif
  */
  int T = 1; // cin>>T;
  while (T--) {
    solve();
  }

  return 0;
}