prithivMLmods/Coder-Stat huggingface dataset:
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define testcase                                                               \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll tc = 1; tc <= T; tc++)
#define M 1000000007
#define MM 998244353
#define eps 1e-8
#define eq(x, y) (fabs((x) - (y)) < eps)
#define r2 1.41421356237
ll powmod(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // testcase {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 1, -1);
  while (m--) {
    int c, d;
    cin >> c >> d;
    // if(v[p]==q || v[q]==p) { continue; }
    while (v[c] > 0) {
      c = v[c];
    }
    while (v[d] > 0) {
      d = v[d];
    }
    if (c == d) {
      continue;
    }
    v[c] += v[d];
    v[d] = c;
  }
  int ans = M;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, v[i]);
  }
  cout << -ans;
  //}
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define testcase                                                               \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll tc = 1; tc <= T; tc++)
#define M 1000000007
#define MM 998244353
#define eps 1e-8
#define eq(x, y) (fabs((x) - (y)) < eps)
#define r2 1.41421356237
ll powmod(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // testcase {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 1, -1);
  while (m--) {
    int c, d;
    cin >> c >> d;
    // if(v[p]==q || v[q]==p) { continue; }
    while (v[c] > 0) {
      c = v[c];
    }
    while (v[d] > 0) {
      d = v[d];
    }
    if (c == d) {
      continue;
    }
    if (v[c] < v[d]) {
      v[c] += v[d];
      v[d] = c;
    } else {
      v[d] += v[c];
      v[c] = d;
    }
  }
  int ans = M;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, v[i]);
  }
  cout << -ans;
  //}
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct bcj {
  int f[100001];
  int size[100001];
  int find(int n) {
    if (f[n] == n)
      return n;
    else
      f[n] = find(f[n]);
    return f[n];
  }
  int add(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a == b)
      return 0;
    else {
      f[a] = b;
      size[b] += size[a];
    }
    return 1;
  }
  void clear(int n) {
    for (int i = 1; i <= n; i++) {
      f[i] = i;
      size[i] = 1;
    }
  }
} S;
int n, m, x, y, ans;
int main() {
  scanf("%d %d", &n, &m);
  S.clear(n);
  while (m--) {
    scanf("%d %d", &x, &y);
    S.add(x, y);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, S.size[i]);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct bcj {
  int f[200001];
  int size[200001];
  int find(int n) {
    if (f[n] == n)
      return n;
    else
      f[n] = find(f[n]);
    return f[n];
  }
  int add(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a == b)
      return 0;
    else {
      f[a] = b;
      size[b] += size[a];
    }
    return 1;
  }
  void clear(int n) {
    for (int i = 1; i <= n; i++) {
      f[i] = i;
      size[i] = 1;
    }
  }
} S;
int n, m, x, y, ans;
int main() {
  scanf("%d %d", &n, &m);
  S.clear(n);
  while (m--) {
    scanf("%d %d", &x, &y);
    S.add(x, y);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, S.size[i]);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
typedef std::pair<int, int> Edge;

int find(int *par, int x) {
  if (x == par[x])
    return x;
  else
    return find(par, par[x]);
}

void unite(int *par, int x, int y) { par[find(par, x)] = find(par, y); }

bool isSame(int *par, int x, int y) { return find(par, x) == find(par, y); }

int main() {
  int N, M;
  std::cin >> N >> M;
  Edge *edge_list = new Edge[M];
  for (int i = 0; i < M; i++) {
    int a, b;
    std::cin >> a >> b;
    edge_list[i].first = a;
    edge_list[i].second = b;
  }
  int *par = new int[N + 1];
  for (int i = 1; i <= N; i++)
    par[i] = i;
  for (int i = 0; i < M; i++) {
    int u, v;
    u = edge_list[i].first;
    v = edge_list[i].second;
    unite(par, u, v);
  }
  int *color_cnt = new int[N + 1];
  for (int i = 1; i <= N; i++) {
    color_cnt[i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    color_cnt[find(par, i)]++;
  }
  int max_size = 0;
  for (int i = 1; i <= N; i++) {
    max_size = std::max(max_size, color_cnt[i]);
  }
  std::cout << max_size << std::endl;
  delete[] edge_list;
  delete[] par;
  delete[] color_cnt;
}


Fixed submission:
#include <iostream>
typedef std::pair<int, int> Edge;

int find(int *par, int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par, par[x]);
}

void unite(int *par, int x, int y) { par[find(par, x)] = find(par, y); }

bool isSame(int *par, int x, int y) { return find(par, x) == find(par, y); }

int main() {
  int N, M;
  std::cin >> N >> M;
  Edge *edge_list = new Edge[M];
  for (int i = 0; i < M; i++) {
    int a, b;
    std::cin >> a >> b;
    edge_list[i].first = a;
    edge_list[i].second = b;
  }
  int *par = new int[N + 1];
  for (int i = 1; i <= N; i++)
    par[i] = i;
  for (int i = 0; i < M; i++) {
    int u, v;
    u = edge_list[i].first;
    v = edge_list[i].second;
    unite(par, u, v);
  }
  int *color_cnt = new int[N + 1];
  for (int i = 1; i <= N; i++) {
    color_cnt[i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    color_cnt[find(par, i)]++;
  }
  int max_size = 0;
  for (int i = 1; i <= N; i++) {
    max_size = std::max(max_size, color_cnt[i]);
  }
  std::cout << max_size << std::endl;
  delete[] edge_list;
  delete[] par;
  delete[] color_cnt;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pl pair<long long, long long>
#define pi pair<int, int>
#define lb(v, x) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ub(v, x) upper_bound(v.begin(), v.end(), x) - v.begin()
#define ct(i) cout << i << "\n"
#define sv(v) sort(v.begin(), v.end())
#define mod 1000000007
#define M 100005
#define endl "\n"

vector<int> adj[M + 1], visited(M);
int co = 0;

void dfs(int s, int nodes) {
  visited[s] = true;
  co++;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      dfs(adj[s][i], nodes);
    }
  }
}

void solve() {
  ll n, x = 0, ans = 0, y, m;
  // string s,t;
  cin >> n >> m;
  set<pair<int, int>> se;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    se.insert({x, y});
  }
  for (auto i : se) {
    x = i.F, y = i.S;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      dfs(i, n);
      ans = max((ll)co, ans);
      co = 0;
    }
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q;
  // cin>>q; while(q--)
  solve();
  return 0;
}


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

#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pl pair<long long, long long>
#define pi pair<int, int>
#define lb(v, x) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ub(v, x) upper_bound(v.begin(), v.end(), x) - v.begin()
#define ct(i) cout << i << "\n"
#define sv(v) sort(v.begin(), v.end())
#define mod 1000000007
#define M 100005
#define endl "\n"

vector<int> adj[2 * M + 1], visited(2 * M);
int co = 0;

void dfs(int s, int nodes) {
  visited[s] = true;
  co++;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      dfs(adj[s][i], nodes);
    }
  }
}

void solve() {
  ll n, x = 0, ans = 0, y, m;
  // string s,t;
  cin >> n >> m;
  set<pair<int, int>> se;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    se.insert({x, y});
  }
  for (auto i : se) {
    x = i.F, y = i.S;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      dfs(i, n);
      ans = max((ll)co, ans);
      co = 0;
    }
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q;
  // cin>>q; while(q--)
  solve();
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans = 1;
int par[200001] = {};
int r[200001] = {};
int si[200001] = {};

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (r[x] < r[y]) {
    par[x] += y; // 親をyにする
    si[y] += si[x];
    ans = max(ans, si[y]);
  } else {
    par[y] = x;
    si[x] += si[y];
    ans = max(ans, si[x]);
    if (r[x] == r[y])
      r[x]++;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n + 1; i++) {
    par[i] = i;
    si[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unite(x, y);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans = 1;
int par[200001] = {};
int r[200001] = {};
int si[200001] = {};

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (r[x] < r[y]) {
    par[x] = y; // 親をyにする
    si[y] += si[x];
    ans = max(ans, si[y]);
  } else {
    par[y] = x;
    si[x] += si[y];
    ans = max(ans, si[x]);
    if (r[x] == r[y])
      r[x]++;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n + 1; i++) {
    par[i] = i;
    si[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unite(x, y);
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

const int MX = 100010;
vector<vector<int>> adj(MX + 1);

// adding edge utility
void add_edge(vector<vector<int>> &ref, int start, int end, bool dir) {

  ref[start].push_back(end);
  if (!dir) {
    ref[end].push_back(start);
  }
}

std::vector<bool> vis(MX + 1, false);
std::vector<ll> comp;

// DFS
void dfs(int v) {
  vis[v] = true;
  comp.push_back(v);
  for (auto u : adj[v]) {
    if (!vis[u]) {
      dfs(u);
    }
  }
  return;
}

// CONNECTED COMPONENTS
void find_comps(int n) {
  // std::vector<ll> arr;
  ll ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      comp.clear();
      dfs(i);
      // for(auto u: comp) {
      //     cout << u << ' ';
      // }
      // cout << '\n';
      // arr.push_back(comp.size());
      ans = max(ans, (ll)comp.size());
    }
  }
  cout << ans << '\n';
  // for(auto it: arr) {
  //     cout << it << '\n';
  // }
}

// testing
int32_t main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
  // for debug in error.txt
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    while (m--) {
      int s, d;
      cin >> s >> d;
      s--, d--;
      add_edge(adj, s, d, 0);
    }
    find_comps(n);
  }
}

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

const int MX = 400010;
vector<vector<int>> adj(MX + 1);

// adding edge utility
void add_edge(vector<vector<int>> &ref, int start, int end, bool dir) {

  ref[start].push_back(end);
  if (!dir) {
    ref[end].push_back(start);
  }
}

std::vector<bool> vis(MX + 1, false);
std::vector<ll> comp;

// DFS
void dfs(int v) {
  vis[v] = true;
  comp.push_back(v);
  for (auto u : adj[v]) {
    if (!vis[u]) {
      dfs(u);
    }
  }
  return;
}

// CONNECTED COMPONENTS
void find_comps(int n) {
  // std::vector<ll> arr;
  ll ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      comp.clear();
      dfs(i);
      // for(auto u: comp) {
      //     cout << u << ' ';
      // }
      // cout << '\n';
      // arr.push_back(comp.size());
      ans = max(ans, (ll)comp.size());
    }
  }
  cout << ans << '\n';
  // for(auto it: arr) {
  //     cout << it << '\n';
  // }
}

// testing
int32_t main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
  // for debug in error.txt
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    while (m--) {
      int s, d;
      cin >> s >> d;
      s--, d--;
      add_edge(adj, s, d, 0);
    }
    find_comps(n);
  }
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    author:ujwalll
*/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
const int MOD = 1073741824;
#define pp pair<ll, ll>
#define ppi pair<int, int>
typedef long long int ll;
bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
////////////////////////////////////////////////////////////////////
set<int> adj[200005];
bool vis[200005];
int cnt = 0;
int solve(int s) {
  vis[s] = 1;
  cnt++;
  for (auto i : adj[s]) {
    if (vis[i])
      continue;
    solve(i);
  }
}
int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    memset(vis, 0, sizeof(vis));
    int n, m;
    cin >> n >> m;
    rep(i, m) {
      int x, y;
      cin >> x >> y;
      adj[x].insert(y);
      adj[y].insert(x);
    }
    int ans = 0;
    for1(i, n) {
      if (!vis[i]) {
        vis[i] = 1;
        solve(i);
        ans = max(ans, cnt);
        cnt = 0;
      }
    }
    cout << ans;
  }
  return 0;
}

Fixed submission:
/*
    author:ujwalll
*/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
const int MOD = 1073741824;
#define pp pair<ll, ll>
#define ppi pair<int, int>
typedef long long int ll;
bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
////////////////////////////////////////////////////////////////////
set<int> adj[200005];
bool vis[200005];
int cnt = 0;
void solve(int s) {
  vis[s] = 1;
  cnt++;
  for (auto i : adj[s]) {
    if (vis[i])
      continue;
    solve(i);
  }
}
int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    memset(vis, 0, sizeof(vis));
    int n, m;
    cin >> n >> m;
    rep(i, m) {
      int x, y;
      cin >> x >> y;
      adj[x].insert(y);
      adj[y].insert(x);
    }
    int ans = 0;
    for1(i, n) {
      if (!vis[i]) {
        vis[i] = 1;
        solve(i);
        ans = max(ans, cnt);
        cnt = 0;
      }
    }
    cout << ans;
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define li long int
#define ld long double
#define pb push_back
using namespace std;

vector<int> lis[100001];
bool vis[100001];

void dfs(int node, int &curr) {
  vis[node] = true;
  curr++;
  for (int x : lis[node]) {
    if (!vis[x]) {
      dfs(x, curr);
    }
  }
}

int main() {
  int n, m, a, b;
  cin >> n >> m;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    lis[a].pb(b);
    lis[b].pb(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int curr = 0;
      dfs(i, curr);
      ans = max(ans, curr);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define li long int
#define ld long double
#define pb push_back
using namespace std;

vector<int> lis[200001];
bool vis[200001];

void dfs(int node, int &curr) {
  vis[node] = true;
  curr++;
  for (int x : lis[node]) {
    if (!vis[x]) {
      dfs(x, curr);
    }
  }
}

int main() {
  int n, m, a, b;
  cin >> n >> m;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    lis[a].pb(b);
    lis[b].pb(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int curr = 0;
      dfs(i, curr);
      ans = max(ans, curr);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#define ll long long int

const int md = 1e9 + 7;
ll Parent[100000];
ll sze[100000];

void Build(int n) {
  for (ll i = 1; i <= n; i++) {
    Parent[i] = i;
    sze[i] = 1;
  }
}

ll find(ll u) {
  if (Parent[u] == u)
    return u;
  else {
    return Parent[u] = find(Parent[u]);
  }
}

void Union(ll a, ll b) {
  ll P_a = find(a);
  ll P_b = find(b);

  if (P_a != P_b) {
    sze[P_b] += sze[P_a];
    Parent[P_a] = Parent[P_b];
  }
}

int main() {
  ll n, m;
  cin >> n >> m;

  // std::vector<Edge> E(m) ;
  Build(n);

  // for(ll i=0;i<m;i++)
  // {
  //     cin>>E[i].src;
  //     E[i].src--;
  //     cin>>E[i].dest;
  //     E[i].dest--;
  //     cin>>E[i].wt;
  // }

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

  unordered_map<int, int> mp;
  ll mx = 0;

  for (int i = 1; i <= n; i++) {
    int p = find(i);
    // cout<<p<<endl;
    if (mp.find(p) == mp.end()) {
      mp[p] = 1;
      mx = max(mx, sze[p]);
    }
  }

  cout << mx << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#define ll long long int

const int md = 1e9 + 7;
ll Parent[200005];
ll sze[200005];

void Build(int n) {
  for (ll i = 1; i <= n; i++) {
    Parent[i] = i;
    sze[i] = 1;
  }
}

ll find(ll u) {
  if (Parent[u] == u)
    return u;
  else {
    return Parent[u] = find(Parent[u]);
  }
}

void Union(ll a, ll b) {
  ll P_a = find(a);
  ll P_b = find(b);

  if (P_a != P_b) {
    sze[P_b] += sze[P_a];
    Parent[P_a] = Parent[P_b];
  }
}

int main() {
  ll n, m;
  cin >> n >> m;

  // std::vector<Edge> E(m) ;
  Build(n);

  // for(ll i=0;i<m;i++)
  // {
  //     cin>>E[i].src;
  //     E[i].src--;
  //     cin>>E[i].dest;
  //     E[i].dest--;
  //     cin>>E[i].wt;
  // }

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

  unordered_map<int, int> mp;
  ll mx = 0;

  for (int i = 1; i <= n; i++) {
    int p = find(i);
    // cout<<p<<endl;
    if (mp.find(p) == mp.end()) {
      mp[p] = 1;
      mx = max(mx, sze[p]);
    }
  }

  cout << mx << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 1e5 + 5;
ll arr[L];
ll szz[L];
ll root(ll i) {
  while (arr[i] != i) {
    arr[i] = arr[arr[i]];
    i = arr[i];
  }
  return i;
}
void union1(ll i, ll j) {
  ll ri = root(i);
  ll rj = root(j);
  if (ri == rj)
    return;
  if (szz[ri] <= szz[rj]) {
    arr[ri] = rj;
    szz[rj] += szz[ri];
  } else {
    arr[rj] = ri;
    szz[ri] += szz[rj];
  }
}
ll find(ll i, ll j) {
  if (root(i) == root(j))
    return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    arr[i] = i;
    szz[i] = 1;
  }
  ll a, b;
  while (m--) {
    cin >> a >> b;
    union1(a, b);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, szz[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 2e5 + 5;
ll arr[L];
ll szz[L];
ll root(ll i) {
  while (arr[i] != i) {
    arr[i] = arr[arr[i]];
    i = arr[i];
  }
  return i;
}
void union1(ll i, ll j) {
  ll ri = root(i);
  ll rj = root(j);
  if (ri == rj)
    return;
  if (szz[ri] <= szz[rj]) {
    arr[ri] = rj;
    szz[rj] += szz[ri];
  } else {
    arr[rj] = ri;
    szz[ri] += szz[rj];
  }
}
ll find(ll i, ll j) {
  if (root(i) == root(j))
    return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    arr[i] = i;
    szz[i] = 1;
  }
  ll a, b;
  while (m--) {
    cin >> a >> b;
    union1(a, b);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, szz[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define int64 long long
const int mod = (int)1e9 + 7;

const int nax = 1e5 + 5;
vector<int> par(nax), siz(nax);

int find(int u) {
  if (par[u] == u)
    return u;
  return par[u] = find(par[u]);
}

void union_set(int a, int b) {
  a = find(a), b = find(b);
  if (a == b)
    return;
  if (siz[a] > siz[b])
    swap(a, b);
  par[a] = b;
  siz[b] += siz[a];
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i, siz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    union_set(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == par[i]) {
      ans = max(ans, siz[i]);
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int64 long long
const int mod = (int)1e9 + 7;

const int nax = 2e5 + 5;
vector<int> par(nax), siz(nax);

int find(int u) {
  if (par[u] == u)
    return u;
  return par[u] = find(par[u]);
}

void union_set(int a, int b) {
  a = find(a), b = find(b);
  if (a == b)
    return;
  if (siz[a] > siz[b])
    swap(a, b);
  par[a] = b;
  siz[b] += siz[a];
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i, siz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    union_set(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == par[i]) {
      ans = max(ans, siz[i]);
    }
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: 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 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;
}

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 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;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
/*author @dhanush*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define pb push_back
#define se second
#define fi first
#define INF INT_MAX

int parent[200001];

int find(int a) {
  while (parent[a] > 0)
    a = parent[a];

  return a;
}

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}

int main() {

  int n, m, a, b;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    parent[i] = -1; // initializing

  while (m--) {
    cin >> a >> b;
    a = find(a), b = find(b);

    if (a != b)
      Union(a, b);
  }

  int res = -1;

  for (int i = 1; i <= n; i++)
    if (parent[i] < 0)
      res = min(res, parent[i]);

  cout << abs(res);
}


Fixed submission:
/*author @dhanush*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define pb push_back
#define se second
#define fi first
#define INF INT_MAX

int parent[200001];

int find(int a) {
  if (parent[a] < 0) {
    return a;
  } else {
    int x = find(parent[a]);
    parent[a] = x;
    return x;
  }
}

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}

int main() {

  int n, m, a, b;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    parent[i] = -1; // initializing

  while (m--) {
    cin >> a >> b;
    a = find(a), b = find(b);

    if (a != b)
      Union(a, b);
  }

  int res = -1;

  for (int i = 1; i <= n; i++)
    if (parent[i] < 0)
      res = min(res, parent[i]);

  cout << abs(res);
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M_PI 3.1415926535
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define Summon_Tourist                                                         \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll inf = 1e9 + 7;
ll modexp(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power & 1)
    return base * modexp(base, power - 1) % inf;
  return modexp(base * base % inf, power / 2);
}
vector<ll> a[20005];
vector<ll> visited(20005, 0), weights(20005, 1);
ll dfs(ll x) {
  if (visited[x])
    return 0;
  visited[x] = 1;
  ll ret = weights[x];
  for (auto it : a[x]) {
    if (visited[it])
      continue;
    ret += dfs(it);
  }
  return ret;
}
int main() {
  Summon_Tourist
      // freopen("input.txt" , "r" , stdin ) ;
      ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    for (ll i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      a[x].push_back(y);
      a[y].push_back(x);
    }
    ll ans = -1;
    for (ll i = 1; i <= n; i++) {
      if (visited[i] != 1) {
        ll ret = dfs(i);
        ans = max(ans, ret);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M_PI 3.1415926535
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define Summon_Tourist                                                         \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll inf = 1e9 + 7;
ll modexp(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power & 1)
    return base * modexp(base, power - 1) % inf;
  return modexp(base * base % inf, power / 2);
}
vector<ll> a[200005];
vector<ll> visited(200005, 0), weights(200005, 1);
ll dfs(ll x) {
  if (visited[x])
    return 0;
  visited[x] = 1;
  ll ret = weights[x];
  for (auto it : a[x]) {
    if (visited[it])
      continue;
    ret += dfs(it);
  }
  return ret;
}
int main() {
  Summon_Tourist
      // freopen("input.txt" , "r" , stdin ) ;
      ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    for (ll i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      a[x].push_back(y);
      a[y].push_back(x);
    }
    ll ans = -1;
    for (ll i = 1; i <= n; i++) {
      if (visited[i] != 1) {
        ll ret = dfs(i);
        ans = max(ans, ret);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int findSet(vector<int> a, int i) {
  if (a[i] < 0)
    return i;
  return a[i] = findSet(a, a[i]);
}

void mergeSets(vector<int> &a, int i, int j) {
  int x = findSet(a, i);
  int y = findSet(a, j);
  if (x == y) {
    return;
  }
  if (a[x] > a[y]) {
    int t = x;
    x = y;
    y = t;
  }
  a[x] += a[y];
  a[y] = x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mergeSets(v, a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << v[i] << " " << i << endl;
    ans = min(ans, v[i]);
  }
  ans = abs(ans);
  cout << ans << endl;
}

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

int findSet(vector<int> &a, int i) {
  if (a[i] < 0)
    return i;
  return a[i] = findSet(a, a[i]);
}

void mergeSets(vector<int> &a, int i, int j) {
  int x = findSet(a, i);
  int y = findSet(a, j);
  if (x == y) {
    return;
  }
  if (a[x] > a[y]) {
    int t = x;
    x = y;
    y = t;
  }
  a[x] += a[y];
  a[y] = x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mergeSets(v, a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << v[i] << " " << i << endl;
    ans = min(ans, v[i]);
  }
  ans = abs(ans);
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int dfs(int i, vector<bool> &visited, std::map<int, std::set<int>> friends) {
  int result = 1;
  for (std::set<int>::iterator it = friends[i].begin(); it != friends[i].end();
       ++it) {
    if (!visited[*it]) {
      visited[*it] = true;
      result += dfs(*it, visited, friends);
    }
  }
  return result;
}

int main() {
  int n = 0;
  int m = 0;
  int a = 0;
  int b = 0;
  scanf("%d %d", &n, &m);
  vector<bool> visited(n, false);
  std::map<int, std::set<int>> friends;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    friends[a - 1].insert(b - 1);
    friends[b - 1].insert(a - 1);
  }
  int max_size = 1;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = true;
      int size = dfs(i, visited, friends);
      max_size = std::max(size, max_size);
      //      printf("%d\n", i);
    }
  }
  printf("%d\n", max_size);

  //  for(int i=0; i<n; i++){
  //    for(int j=0; j<k+1; j++){
  //      printf("%lld ", arr[i][j]);
  //    }
  //    printf("\n");
  //  }
  //  printf("%lld\n", (arr[n-1][k] - arr[n-1][k-1] + base) % base);
}


Fixed submission:
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int dfs(int i, vector<bool> &visited, std::map<int, std::set<int>> &friends) {
  int result = 1;
  for (std::set<int>::iterator it = friends[i].begin(); it != friends[i].end();
       ++it) {
    if (!visited[*it]) {
      visited[*it] = true;
      result += dfs(*it, visited, friends);
    }
  }
  return result;
}

int main() {
  int n = 0;
  int m = 0;
  int a = 0;
  int b = 0;
  scanf("%d %d", &n, &m);
  vector<bool> visited(n, false);
  std::map<int, std::set<int>> friends;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    friends[a - 1].insert(b - 1);
    friends[b - 1].insert(a - 1);
  }
  int max_size = 1;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = true;
      int size = dfs(i, visited, friends);
      max_size = std::max(size, max_size);
      //      printf("%d\n", i);
    }
  }
  printf("%d\n", max_size);

  //  for(int i=0; i<n; i++){
  //    for(int j=0; j<k+1; j++){
  //      printf("%lld ", arr[i][j]);
  //    }
  //    printf("\n");
  //  }
  //  printf("%lld\n", (arr[n-1][k] - arr[n-1][k-1] + base) % base);
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <typename T> string join(vector<T> &vec, string &sp) {
  int si = vec.length;
  if (si == 0) {
    return "";
  } else {
    stringstream ss;
    rep(i, si - 1) { ss << vec[i] << sp; }
    ss << vec[si - 1];
    return ss.str();
  }
}
vector<int> uf(200010);
int root(int p) {
  if (p == uf[p])
    return p;
  else
    return root(uf[p]);
}
int main(void) {
  int n, m;
  cin >> n >> m;
  n++;
  rep(i, n + 1) { uf[i] = i; }
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    int rx = root(uf[a]);
    int ry = root(uf[b]);
    if (rx != ry) {
      uf[rx] = ry;
    }
  }
  unordered_map<int, int> mp;
  rep(i, n) {
    int p = root(i);
    if (!mp[p]) {
      mp[p] = 1;
    } else {
      mp[p]++;
    }
  }
  int max = 0;
  for (auto i = mp.begin(); i != mp.end(); i++) {
    if (max < i->second)
      max = i->second;
  }
  cout << max << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <typename T> string join(vector<T> &vec, string &sp) {
  int si = vec.length;
  if (si == 0) {
    return "";
  } else {
    stringstream ss;
    rep(i, si - 1) { ss << vec[i] << sp; }
    ss << vec[si - 1];
    return ss.str();
  }
}
vector<int> uf(200010);
int root(int p) {
  if (p == uf[p])
    return p;
  else {

    return uf[p] = root(uf[p]);
  }
}
int main(void) {
  int n, m;
  cin >> n >> m;
  n++;
  rep(i, n + 1) { uf[i] = i; }
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    int rx = root(uf[a]);
    int ry = root(uf[b]);
    if (rx != ry) {
      uf[rx] = ry;
    }
  }
  unordered_map<int, int> mp;
  rep(i, n) {
    int p = root(i);
    if (!mp[p]) {
      mp[p] = 1;
    } else {
      mp[p]++;
    }
  }
  int max = 0;
  for (auto i = mp.begin(); i != mp.end(); i++) {
    if (max < i->second)
      max = i->second;
  }
  cout << max << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<ll, ll>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
using namespace std;
vector<int> v[100001];
vector<int> parent(100001, -1);
int R[100001] = {1};
ll ct = 0;
int find(int a) {
  // cout<<parent[a]<<endl;
  if (parent[a] < 0)
    return a;
  return parent[a] = find(parent[a]);
}
void u(int a, int b) {
  // cout<<a<<b<<endl;
  parent[a] = -(abs(parent[a]) + abs(parent[b]));
  // cout<<parent[a]<<endl;
  if (R[a] >= R[b]) {
    parent[b] = a;
    R[a] += R[b];
  } else {
    parent[a] = b;
    R[b] += R[a];
  }
  // Rank decrease we can find in o(logn) max time;
}
int main() {
  //	freopen("in4.txt", "r", stdin);
  //    freopen("output4.txt", "w", stdout);

  ll n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    v[i].pb(i);
  }
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    a = find(a);
    // cout<<a<<endl;
    b = find(b);
    // cout<<b<<endl;
    if (a != b)
      u(a, b);
  }
  int maxi = INT_MIN;
  for (int i = 1; i <= n; i++) {
    // cout<<parent[i]<<" ";
    if (parent[i] < 0) {
      maxi = max(maxi, abs(parent[i]));
    }
  }
  cout << maxi << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<ll, ll>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
using namespace std;
vector<int> v[2000001];
vector<int> parent(2000001, -1);
int R[2000001] = {1};
ll ct = 0;
int find(int a) {
  // cout<<parent[a]<<endl;
  if (parent[a] < 0)
    return a;
  return parent[a] = find(parent[a]);
}
void u(int a, int b) {
  // cout<<a<<b<<endl;
  parent[a] = -(abs(parent[a]) + abs(parent[b]));
  // cout<<parent[a]<<endl;
  if (R[a] >= R[b]) {
    parent[b] = a;
    R[a] += R[b];
  } else {
    parent[a] = b;
    R[b] += R[a];
  }
  // Rank decrease we can find in o(logn) max time;
}
int main() {
  //	freopen("in4.txt", "r", stdin);
  //    freopen("output4.txt", "w", stdout);

  ll n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    v[i].pb(i);
  }
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    a = find(a);
    // cout<<a<<endl;
    b = find(b);
    // cout<<b<<endl;
    if (a != b)
      u(a, b);
  }
  int maxi = INT_MIN;
  for (int i = 1; i <= n; i++) {
    // cout<<parent[i]<<" ";
    if (parent[i] < 0) {
      maxi = max(maxi, abs(parent[i]));
    }
  }
  cout << maxi << endl;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int a[200005];
int cnt[200005];
int findroot(int v) { return a[v] == v ? v : findroot(a[v]); }
int main() {
  int n, m;
  cin >> n;
  cin >> m;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    cnt[i] = 1;
  }
  int max = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    int findrootx = findroot(x);
    int findrooty = findroot(y);
    if (findrootx != findrooty) {
      a[findrooty] = findrootx;
      cnt[findrootx] += cnt[findrooty];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (findroot(i) == i) {
      if (max < cnt[i]) {
        max = cnt[i];
      }
    }
  }
  cout << max;
}


Fixed submission:
#include <iostream>
using namespace std;
int a[200005];
int cnt[200005];
int findroot(int v) { return a[v] == v ? v : a[v] = findroot(a[v]); }
int main() {
  int n, m;
  cin >> n;
  cin >> m;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    cnt[i] = 1;
  }
  int max = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    int findrootx = findroot(x);
    int findrooty = findroot(y);
    if (findrootx != findrooty) {
      a[findrooty] = findrootx;
      cnt[findrootx] += cnt[findrooty];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (findroot(i) == i) {
      if (max < cnt[i]) {
        max = cnt[i];
      }
    }
  }
  cout << max;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct Node {
  int pos;
  int flag;
  std::vector<int> connects;
};

Node nodes[200010];
int n, m;

int curFlag;
int maxCount;

int checkAround(int pos, int curFlag) {
  int count = 0;
  std::queue<int> q;
  q.push(pos);
  nodes[pos].flag = curFlag;
  count++;

  int newPos;
  while (!q.empty()) {
    int curPos = q.front();
    q.pop();
    if (nodes[curPos].flag != curFlag) {
      nodes[curPos].flag = curFlag;
      count++;
    }

    for (int j = 0; j < nodes[curPos].connects.size(); j++) {
      int newPos = nodes[pos].connects[j];

      if (nodes[newPos].flag == -1) {
        nodes[newPos].flag = curFlag;
        count++;
        q.push(newPos);
      }
    }
  }

  return count;
}

int main() {
  scanf("%d%d", &n, &m);

  for (int i = 1; i <= n; i++) {
    nodes[i].flag = -1;
    nodes[i].pos = i;
  }

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);

    nodes[u].connects.push_back(v);
    nodes[v].connects.push_back(u);
  }

  maxCount = 0;
  curFlag = 1;
  for (int i = 1; i <= n; i++) {
    if (nodes[i].flag == -1) {
      int count = checkAround(i, curFlag);
      if (count > maxCount)
        maxCount = count;
      curFlag++;
    }
  }

  printf("%d\n", maxCount);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct Node {
  int pos;
  int flag;
  std::vector<int> connects;
};

Node nodes[200010];
int n, m;

int curFlag;
int maxCount;

int checkAround(int pos, int curFlag) {
  int count = 0;
  std::queue<int> q;
  q.push(pos);
  nodes[pos].flag = curFlag;
  count++;

  int newPos;
  while (!q.empty()) {
    int curPos = q.front();
    q.pop();
    if (nodes[curPos].flag != curFlag) {
      nodes[curPos].flag = curFlag;
      count++;
    }

    for (int j = 0; j < nodes[curPos].connects.size(); j++) {
      int newPos = nodes[curPos].connects[j];

      if (nodes[newPos].flag == -1) {
        nodes[newPos].flag = curFlag;
        count++;
        q.push(newPos);
      }
    }
  }

  return count;
}

int main() {
  scanf("%d%d", &n, &m);

  for (int i = 1; i <= n; i++) {
    nodes[i].flag = -1;
    nodes[i].pos = i;
  }

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);

    nodes[u].connects.push_back(v);
    nodes[v].connects.push_back(u);
  }

  maxCount = 0;
  curFlag = 1;
  for (int i = 1; i <= n; i++) {
    if (nodes[i].flag == -1) {
      int count = checkAround(i, curFlag);
      if (count > maxCount)
        maxCount = count;
      curFlag++;
    }
  }

  printf("%d\n", maxCount);

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long

#define F first
#define S second
#define all(x) (x).begin(), (x).end()

using namespace std;

vector<vector<int>> g(100005);
int u[100005];

void dfs(int v, int &cnt) {
  u[v] = 1, cnt++;
  for (int to : g[v])
    if (!u[to])
      dfs(to, cnt);
}

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

  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);

  int n, m, ans = 0;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {
    if (!u[i]) {
      int cnt = 0;
      dfs(i, cnt);
      ans = max(ans, cnt);
    }
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long

#define F first
#define S second
#define all(x) (x).begin(), (x).end()

using namespace std;

vector<vector<int>> g(200005);
int u[200005];

void dfs(int v, int &cnt) {
  u[v] = 1, cnt++;
  for (int to : g[v])
    if (!u[to])
      dfs(to, cnt);
}

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

  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);

  int n, m, ans = 0;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {
    if (!u[i]) {
      int cnt = 0;
      dfs(i, cnt);
      ans = max(ans, cnt);
    }
  }

  cout << ans;

  return 0;
}