alexjercan/bugnet huggingface dataset:
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int father[maxn], cnt[maxn];
int findFather(int x) {
  return x == father[x] ? x : father[x] = findFather(father[x]);
}
int main() {
  fastio();
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) father[i] = i;
  rep(i, 1, m) {
    int u, v;
    cin >> u >> v;
    int fu = findFather(u), fv = findFather(v);
    if (fu != fv)
      father[fu] = fv;
  }
  int ans = 0;
  rep(i, 1, n) {
    int f = findFather(i);
    cnt[f]++;
    ans = max(cnt[f], ans);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int father[maxn], cnt[maxn];
int findFather(int x) { return x == father[x] ? x : findFather(father[x]); }
int main() {
  fastio();
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) father[i] = i;
  rep(i, 1, m) {
    int u, v;
    cin >> u >> v;
    int fu = findFather(u), fv = findFather(v);
    if (fu != fv)
      father[fu] = fv;
  }
  int ans = 0;
  rep(i, 1, n) {
    int f = findFather(i);
    cnt[f]++;
    ans = max(cnt[f], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 7e18;
using namespace std;

ll par[200001];
ll ran[200001];
ll siz[200001];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    ran[i] = 0;
    siz[i] = 1;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    par[x] = find(par[x]);
    return par[x];
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ran[x] < ran[y]) {
    par[x] = y;
    siz[y] += siz[x];
  } else {
    par[y] = x;
    siz[x] += siz[y];
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return siz[find(x)]; }

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, size(i));
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 7e18;
using namespace std;

ll par[100001];
ll ran[100001];
ll siz[100001];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    ran[i] = 0;
    siz[i] = 1;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    par[x] = find(par[x]);
    return par[x];
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ran[x] < ran[y]) {
    par[x] = y;
    siz[y] += siz[x];
  } else {
    par[y] = x;
    siz[x] += siz[y];
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return siz[find(x)]; }

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, size(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 2e5 + 7;
const int mod = 1e9 + 7;

vector<int> adj[N];
int used[N], cnt, mx;

void dfs(int v) {
  used[v] = 1;
  cnt++;
  for (auto u : adj[v]) {
    if (!used[u]) {
      dfs(u);
    }
  }
}

int32_t main() {
  speed;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cnt = 0;
      dfs(i);
      mx = max(mx, cnt);
    }
  }
  cout << mx;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;

vector<int> adj[N];
int used[N], cnt, mx;

void dfs(int v) {
  used[v] = 1;
  cnt++;
  for (auto u : adj[v]) {
    if (!used[u]) {
      dfs(u);
    }
  }
}

int32_t main() {
  speed;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cnt = 0;
      dfs(i);
      mx = max(mx, cnt);
    }
  }
  cout << mx;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define fs first
#define sec second
#define md 1000000007
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
using namespace std;
ll power(ll x, unsigned ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
vector<ll> par(200001, 0);
vector<ll> sz(200001, 1);
void func() {
  for (ll i = 0; i < 200001; i++) {
    par[i] = i;
  }
}
ll find(ll a) {
  if (par[a] == a)
    return a;
  return par[a] = find(par[a]);
}
void union_(ll a, ll b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  if (sz[a] < sz[b]) {
    par[b] = par[a];
    sz[a] += sz[b];
  } else {
    par[a] = par[b];
    sz[b] += sz[a];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll T = 1;
  // cin>>T;
  while (T--) {
    ll n, m;
    cin >> n >> m;
    // vector<ll> par(n+1,0);
    func();
    for (ll i = 0; i < m; i++) {
      ll a, b;
      cin >> a >> b;
      union_(a, b);
    }
    ll ans = 0;
    for (ll i = 1; i <= n; i++) {
      ll x = find(i);
    }
    for (ll i = 1; i <= n; i++) {
      // cout<<sz[i]<<" ";
      ans = max(ans, sz[i]);
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fs first
#define sec second
#define md 1000000007
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
using namespace std;
ll power(ll x, unsigned ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
vector<ll> par(100001, 0);
vector<ll> sz(100001, 1);
void func() {
  for (ll i = 0; i < 100001; i++) {
    par[i] = i;
  }
}
ll find(ll a) {
  if (par[a] == a)
    return a;
  return par[a] = find(par[a]);
}
void union_(ll a, ll b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  if (sz[a] < sz[b]) {
    par[b] = par[a];
    sz[a] += sz[b];
  } else {
    par[a] = par[b];
    sz[b] += sz[a];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll T = 1;
  // cin>>T;
  while (T--) {
    ll n, m;
    cin >> n >> m;
    // vector<ll> par(n+1,0);
    func();
    for (ll i = 0; i < m; i++) {
      ll a, b;
      cin >> a >> b;
      union_(a, b);
    }
    ll ans = 0;
    for (ll i = 1; i <= n; i++) {
      ll x = find(i);
    }
    for (ll i = 1; i <= n; i++) {
      // cout<<sz[i]<<" ";
      ans = max(ans, sz[i]);
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

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:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> data(n + 1, vector<int>());
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    if (find(data[a].begin(), data[a].end(), b) == data[a].end())
      data[a].push_back(b);
    if (find(data[b].begin(), data[b].end(), a) == data[b].end())
      data[b].push_back(a);
  }
  /*
  for(int i = 1; i <= n; i++){
      for(const auto& e: data[i]) cout << e << " ";
      cout << endl;
  }
  */

  int max = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> table;
    table.push_back(i);
    queue<int> que;
    que.push(i);
    while (!que.empty()) {
      int tmp = que.front();
      que.pop();
      for (const auto &e : data[tmp]) {
        if (find(table.begin(), table.end(), e) == table.end()) {
          table.push_back(e);
          que.push(e);
        }
      }
    }
    if (max < table.size())
      max = table.size();
    // for(const auto& et: table) cout << et << " "; cout << endl;
  }
  cout << max << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

/*coderanant*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define f1(i, a, b) for (int i = a; i < b; i++)
#define f2(i, a, b) for (int i = a; i >= b; i--)
#define endl '\\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair
#define ld long double

const int mod = 1000000007;
const ld PI = acosl(-1.0);
ll temp;

// Don't forget to initialise parent and siz.
int parent[200001]; // 1e6 by default
int siz[200001];    // 1e6 by default
int ans[200001];
void make_set(int v) {
  parent[v] = v;
  siz[v] = 1;
}

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) {
    if (siz[a] < siz[b])
      swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  f1(i, 1, n + 1) make_set(i);
  f1(i, 0, m) {
    int a, b;
    cin >> a >> b;
    union_sets(a, b);
  }
  memset(ans, 0, sizeof ans);
  f1(i, 1, n + 1) { ans[find_set(i)]++; }
  int final = 0;
  f1(i, 1, n + 1) { final = max(final, ans[i]); }
  cout << final << endl;
  return 0;
}

Error: 0

Fixed submission:

/*coderanant*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define f1(i, a, b) for (int i = a; i < b; i++)
#define f2(i, a, b) for (int i = a; i >= b; i--)
#define endl '\\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair
#define ld long double

const int mod = 1000000007;
const ld PI = acosl(-1.0);
ll temp;

// Don't forget to initialise parent and siz.
int parent[1000001]; // 1e6 by default
int siz[1000001];    // 1e6 by default
void make_set(int v) {
  parent[v] = v;
  siz[v] = 1;
}

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) {
    if (siz[a] < siz[b])
      swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  f1(i, 1, n + 1) make_set(i);
  f1(i, 0, m) {
    int a, b;
    cin >> a >> b;
    union_sets(a, b);
  }
  int ans[100001];
  memset(ans, 0, sizeof ans);
  f1(i, 1, n + 1) { ans[find_set(i)]++; }
  int final = 0;
  f1(i, 1, n + 1) { final = max(final, ans[i]); }
  cout << final << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <climits>
#include <iomanip>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;
#define int long long

const int SIZE = 200020;

struct UnionFind {
  vector<int> par;

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

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

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

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

  int maxtree(int N) {
    int kosuu[SIZE];
    for (int i = 0; i < N; i++) {
      kosuu[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      kosuu[root(i)]++;
    }
    int max = 0;
    for (int i = 0; i < N; i++) {
      if (max < kosuu[i])
        max = kosuu[i];
    }
    return max;
  }
};

signed main() {

  int N, M;

  int ans = 0;

  cin >> N >> M;

  int A[SIZE], B[SIZE];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    tree.unite(A[i] - 1, B[i] - 1);
  }

  ans = tree.maxtree(N);

  cout << ans << endl;

  cin >> ans;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <climits>
#include <iomanip>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;
#define int long long

const int SIZE = 220;

struct UnionFind {
  vector<int> par;

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

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

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

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

  int maxtree(int N) {
    int kosuu[SIZE];
    for (int i = 0; i < N; i++) {
      kosuu[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      kosuu[root(i)]++;
    }
    int max = 0;
    for (int i = 0; i < N; i++) {
      if (max < kosuu[i])
        max = kosuu[i];
    }
    return max;
  }
};

signed main() {

  int N, M;

  int ans = 0;

  cin >> N >> M;

  int A[SIZE], B[SIZE];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    tree.unite(A[i] - 1, B[i] - 1);
  }

  ans = tree.maxtree(N);

  cout << ans << endl;

  cin >> ans;

  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

// Author : Gaara

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define endl "\\n"
#define fo(i, n) for (ll i = 0; i < n; i++)
#define ro(i, n) for (ll j = n - 1; i >= 0; i--)
#define vll vector<long long>
#define mod 1000000007
#define MAX 200000

vector<vector<int>> adj(MAX + 1);
bool visited[MAX + 1];
int x = 0;

void dfs(int s, int e) {
  visited[s] = true;
  x++;
  for (auto u : adj[s]) {
    if (visited[u])
      continue;
    if (u != e)
      dfs(u, s);
  }
  return;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, m;
    cin >> n >> m;

    fo(i, m) {
      int a, b;
      cin >> a >> b;
      adj[a].pb(b);
      adj[b].pb(a);
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (visited[i])
        continue;
      x = 0;
      dfs(i, 0);
      ans = max(ans, x);
    }

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

Error: 0

Fixed submission:

// Author : Gaara

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define endl "\\n"
#define fo(i, n) for (ll i = 0; i < n; i++)
#define ro(i, n) for (ll j = n - 1; i >= 0; i--)
#define vll vector<long long>
#define mod 1000000007
#define MAX 200000

vector<vector<int>> adj(MAX + 1);
bool visited[MAX + 1];
int x = 0;

bool dfs(int s, int e) {
  visited[s] = true;
  x++;
  for (auto u : adj[s]) {
    if (visited[u])
      continue;
    if (u != e)
      dfs(u, s);
  }
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, m;
    cin >> n >> m;

    fo(i, m) {
      int a, b;
      cin >> a >> b;
      adj[a].pb(b);
      adj[b].pb(a);
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (visited[i])
        continue;
      x = 0;
      dfs(i, 0);
      ans = max(ans, x);
    }

    cout << ans << "\\n";
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a, b, i;
  cin >> n >> m;
  vector<set<int>> s(n);
  vector<int> c(n);
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    s.at(a).insert(b);
    s.at(b).insert(a);
  }
  queue<int> q;
  q.push(0);
  c.at(0) = 1;
  int cnt = 1, cnta = 1, ans = 0;
  set<int> st;
  for (i = 1; i < n; i++)
    st.insert(i);
  while (cnta < n) {
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto z : s.at(u)) {
        if (c.at(z) == 0) {
          c.at(z) = 1;
          st.erase(z);
          q.push(z);
          cnt++;
          cnta++;
        }
      }
    }
    ans = max(ans, cnt);
    if (cnta == n)
      break;
    else {
      i = *st.begin();
      st.erase(i);
      q.push(i);
      c.at(i) = 1;
      cnt = 1;
      cnta++;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a, b, i;
  cin >> n >> m;
  vector<set<int>> s(n);
  vector<vector<int>> g(n);
  vector<int> c(n);
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    s.at(a).insert(b);
    s.at(b).insert(a);
  }
  for (i = 0; i < n; i++)
    for (auto z : s.at(i))
      g.at(i).push_back(z);
  queue<int> q;
  q.push(0);
  c.at(0) = 1;
  int cnt = 1, cnta = 1, ans = 0;
  while (cnta < n) {
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto z : g.at(u)) {
        if (c.at(z) == 0) {
          c.at(z) = 1;
          q.push(z);
          cnt++;
          cnta++;
        }
      }
    }
    ans = max(ans, cnt);
    if (cnta == n)
      break;
    else {
      i = 0;
      while (c.at(i) == 1)
        i++;
      q.push(i);
      c.at(i) = 1;
      cnt = 1;
      cnta++;
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;
const int mod = 1e9 + 7;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

const int maxn = 2e5 + 10;

int f[maxn];
int n, m;

inline void init() {
  for (int i = 0; i <= n; i++)
    f[i] = i;
}

int getf(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = getf(f[x]);
}

void merge(int x, int y) {
  x = getf(x);
  y = getf(y);

  if (x != y)
    f[y] = x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;

  init();

  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    merge(x, y);
  }

  map<int, int> book;

  int maxx = 0;

  for (int i = 1; i <= n; i++) {
    int now = getf(i);
    book[now]++;

    if (book[now] > maxx)
      maxx = book[now];
  }

  cout << maxx << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;
const int mod = 1e9 + 7;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

const int maxn = 2e5 + 10;

int f[maxn];
int n, m;

inline void init() {
  for (int i = 0; i <= n; i++)
    f[i] = i;
}

int getf(int x) {
  if (f[x] == x)
    return x;
  else
    return getf(f[x]);
}

void merge(int x, int y) {
  x = getf(x);
  y = getf(y);

  if (x != y)
    f[y] = x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;

  init();

  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    merge(x, y);
  }

  map<int, int> book;

  int maxx = 0;

  for (int i = 1; i <= n; i++) {
    int now = getf(i);
    book[now]++;

    if (book[now] > maxx)
      maxx = book[now];
  }

  cout << maxx << endl;

  return 0;
}