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

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

using namespace std;

typedef long long ll;
const int mx = 200009;
ll mod = 1000000007;
ll a[mx];
/*
 int siz[mx];
 int par[mx];



 int parent(int i){
     while(i!=par[i]){
         i=par[i];
     }
     return i;
 }
 void unit(int x,int y){

     if(siz[x]<siz[y]){swap(x,y);}

         int par1=parent(x);
         int par2=parent(y);
     par[par2]=par1;
     siz[par1]+=siz[par2];


 }bool same(int a, int b) {
return parent(a) == parent(b);
}
*/
vector<int> edges[100000];
int visited[mx];
int dfs(int i) {
  if (visited[i]) {
    return 0;
  }
  visited[i] = 1;
  ll ans = 0;
  for (int j = 0; j < edges[i].size(); j++) {
    ans += dfs(edges[i][j]);
  }
  return ans + 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, m;
  cin >> n >> m;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;

    edges[x].push_back(y);
    edges[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}


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

using namespace std;

typedef long long ll;
const int mx = 200009;
ll mod = 1000000007;
ll a[mx];
/*
 int siz[mx];
 int par[mx];



 int parent(int i){
     while(i!=par[i]){
         i=par[i];
     }
     return i;
 }
 void unit(int x,int y){

     if(siz[x]<siz[y]){swap(x,y);}

         int par1=parent(x);
         int par2=parent(y);
     par[par2]=par1;
     siz[par1]+=siz[par2];


 }bool same(int a, int b) {
return parent(a) == parent(b);
}
*/
vector<int> edges[mx];
int visited[mx];
int dfs(int i) {
  if (visited[i]) {
    return 0;
  }
  visited[i] = 1;
  ll ans = 0;
  for (int j = 0; j < edges[i].size(); j++) {
    ans += dfs(edges[i][j]);
  }
  return ans + 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, m;
  cin >> n >> m;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;

    edges[x].push_back(y);
    edges[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int mxn = 1e5 + 5;
const int inf = 1e9;

int sz[mxn];

int par[mxn];

int findrep(int u) { return par[u] == u ? u : (par[u] = findrep(par[u])); }

void unite(int u, int v) {
  u = findrep(u);
  v = findrep(v);

  if (u == v)
    return;
  if (sz[u] > sz[v])
    swap(u, v);

  sz[v] += sz[u];
  par[u] = v;
}

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

  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    unite(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (findrep(i) == i) {
      ans = max(ans, sz[i]);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int mxn = 2e5 + 5;
const int inf = 1e9;

int sz[mxn];

int par[mxn];

int findrep(int u) { return par[u] == u ? u : (par[u] = findrep(par[u])); }

void unite(int u, int v) {
  u = findrep(u);
  v = findrep(v);

  if (u == v)
    return;
  if (sz[u] > sz[v])
    swap(u, v);

  sz[v] += sz[u];
  par[u] = v;
}

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

  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    unite(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (findrep(i) == i) {
      ans = max(ans, sz[i]);
    }
  }

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

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

vector<int> adj_list[100005];

void dfs(int u, vector<int> &flag, int *kitna) {
  *kitna = *kitna + 1;
  flag[u] = 1;
  for (auto v : adj_list[u]) {
    if (flag[v] == -1) {
      dfs(v, flag, kitna);
    }
  }
}

int32_t main() {
  vector<int> arr;
  int n;
  int m;
  cin >> n >> m;
  vector<int> flag(n + 1, -1);
  for (int i = 1; i <= m; i++) {
    int u;
    int v;
    cin >> u >> v;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i] == -1) {
      int kitna = 0;
      dfs(i, flag, &kitna);
      arr.push_back(kitna);
    }
  }
  int ans = *max_element(arr.begin(), arr.end());
  cout << ans << endl;
}

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

vector<int> adj_list[200005];

void dfs(int u, vector<int> &flag, int *kitna) {
  *kitna = *kitna + 1;
  flag[u] = 1;
  for (auto v : adj_list[u]) {
    if (flag[v] == -1) {
      dfs(v, flag, kitna);
    }
  }
}

int32_t main() {
  vector<int> arr;
  int n;
  int m;
  cin >> n >> m;
  vector<int> flag(n + 1, -1);
  for (int i = 1; i <= m; i++) {
    int u;
    int v;
    cin >> u >> v;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i] == -1) {
      int kitna = 0;
      dfs(i, flag, &kitna);
      arr.push_back(kitna);
    }
  }
  int ans = *max_element(arr.begin(), arr.end());
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int N = (int)1e5 + 7;

int n, m;
vector<int> g[N];
bool used[N];

int dfs(int v) {
  used[v] = 1;
  int q = 1;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) {
      q += dfs(to);
    }
  }
  return q;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    if (!used[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int N = (int)2e5 + 7;

int n, m;
vector<int> g[N];
bool used[N];

int dfs(int v) {
  used[v] = 1;
  int q = 1;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) {
      q += dfs(to);
    }
  }
  return q;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    if (!used[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
/***** author :  C0d1ngPhenomena *****/

#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define TestCases                                                              \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define revrep(i, a, b) for (ll i = b - 1; i >= a; i--)
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp(x, y) make_pair(x, y)
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define alld(c) c.begin(), c.end(), greater<int>()
#define mem(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
#define pb push_back
using namespace std;

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

ll maxi = 0;
bool visited[100000];
vector<ll> G[100000];

void dfs(int x) {
  visited[x] = true;

  for (auto &i : G[x]) {
    if (visited[i] == false) {
      dfs(i);
      maxi++;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  ll m;
  cin >> n >> m;

  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    G[x].pb(y);
    G[y].pb(x);
  }

  ll cnt = -1;

  rep(i, 1, n + 1) {
    if (!visited[i])
      dfs(i);
    cnt = max(cnt, maxi);
    maxi = 0;
  }
  cout << cnt + 1;
  return 0;
}


Fixed submission:
/***** author :  C0d1ngPhenomena *****/

#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define TestCases                                                              \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define revrep(i, a, b) for (ll i = b - 1; i >= a; i--)
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp(x, y) make_pair(x, y)
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define alld(c) c.begin(), c.end(), greater<int>()
#define mem(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
#define pb push_back
using namespace std;

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

ll maxi = 0;
bool visited[1000000];
vector<ll> G[1000000];

void dfs(int x) {
  visited[x] = true;

  for (auto &i : G[x]) {
    if (visited[i] == false) {
      dfs(i);
      maxi++;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  ll m;
  cin >> n >> m;

  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    G[x].pb(y);
    G[y].pb(x);
  }

  ll cnt = -1;

  rep(i, 1, n + 1) {
    if (!visited[i])
      dfs(i);
    cnt = max(cnt, maxi);
    maxi = 0;
  }
  cout << cnt + 1;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int MOD = 1e9 + 7;
int dsu[200005];
int getParent(int num) {
  if (dsu[num] == num)
    return num;
  return dsu[num] = getParent(dsu[num]);
}
int makeParent(int a, int b) {
  a = getParent(a);
  b = getParent(b);
  dsu[a] = b;
}
int main() {
  IO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    dsu[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    makeParent(a, b);
  }
  map<int, int> dd;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    dd[getParent(i)]++;
    ans = max(ans, dd[getParent(i)]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int MOD = 1e9 + 7;
int dsu[200005];
int getParent(int num) {
  if (dsu[num] == num)
    return num;
  return dsu[num] = getParent(dsu[num]);
}
void makeParent(int a, int b) {
  a = getParent(a);
  b = getParent(b);
  dsu[a] = b;
}
int main() {
  IO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    dsu[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    makeParent(a, b);
  }
  map<int, int> dd;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    dd[getParent(i)]++;
    ans = max(ans, dd[getParent(i)]);
  }
  cout << ans << endl;
}
Problem p02573 (C++)

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

using namespace std;

int a[200020];
int si[200020];

int ff(int x) { return a[x] == x ? x : ff(a[x]); }

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    a[i] = i, si[i] = 1;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    x = ff(x);
    y = ff(y);
    if (x != y) {
      a[y] = x;
      si[x] += si[y];
    }
  }
  int ma = 1;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, si[i]);
  }
  cout << ma << endl;
  return 0;
}

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

using namespace std;

int a[200020];
int si[200020];

int ff(int x) { return a[x] = a[x] == x ? x : ff(a[x]); }

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    a[i] = i, si[i] = 1;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    x = ff(x);
    y = ff(y);
    if (x != y) {
      a[y] = x;
      si[x] += si[y];
    }
  }
  int ma = 1;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, si[i]);
  }
  cout << ma << endl;
  return 0;
}
Problem p02573 (C++)

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

using namespace std;

#define LL long long int
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int N = 1e5 + 5;
const int M = 21;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

clock_t start = clock();

vector<int> g[N];
int viscnt, vis[N];
void dfs(int node) {
  vis[node] = 1;
  viscnt++;
  for (auto it : g[node])
    if (!vis[it]) {
      dfs(it);
    }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    g[i].clear();
    vis[i] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      viscnt = 0;
      dfs(i);
      ans = max(ans, viscnt);
    }
  cout << ans << '\n';
}

int main() {
  FASTIO;
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  // cerr << fixed << setprecision(10);
  // cerr << (clock() - start) / ((long double)CLOCKS_PER_SEC) << '\n';
  return 0;
}

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

using namespace std;

#define LL long long int
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int N = 2e5 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

clock_t start = clock();

vector<int> g[N];
int viscnt, vis[N];
void dfs(int node) {
  vis[node] = 1;
  viscnt++;
  for (auto it : g[node])
    if (!vis[it]) {
      dfs(it);
    }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    g[i].clear();
    vis[i] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      viscnt = 0;
      dfs(i);
      ans = max(ans, viscnt);
    }
  cout << ans << '\n';
}

int main() {
  FASTIO;
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  // cerr << fixed << setprecision(10);
  // cerr << (clock() - start) / ((long double)CLOCKS_PER_SEC) << '\n';
  return 0;
}
Problem p02573 (C++)

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

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

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}

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

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

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

struct UnionFind {
  vector<int> data;

  UnionFind(int N) { data.assign(N, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

UnionFind uf(2);

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int ans = 1;

  rep(i, m) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    ans = max(ans, uf.size(a));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

struct UnionFind {
  vector<int> data;

  UnionFind(int N) { data.assign(N, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

UnionFind uf(200000);

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int ans = 1;

  rep(i, m) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    ans = max(ans, uf.size(a));
  }
  cout << ans << endl;

  return 0;
}
Problem p02573 (C++)

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;

int pre[NMAX], a[NMAX];
inline int find(int x) { return x == pre[x] ? x : find(pre[x]); }
int main(int argc, char const *argv[]) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= n; i++)
    pre[i] = i;
  int u, v;
  while (m--) {
    scanf("%d%d", &u, &v);
    int u_pre = find(u), v_pre = find(v);
    if (u_pre == v_pre)
      continue;
    pre[u_pre] = v_pre;
  }
  int ans = 0;
  for (register int i = 1; i <= n; i++)
    a[find(i)] += 1, ans = max(ans, a[find(i)]);
  printf("%d\n", ans);
  return 0;
}

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

typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;

int pre[NMAX], a[NMAX];
inline int find(int x) { return pre[x] = (x == pre[x] ? x : find(pre[x])); }
int main(int argc, char const *argv[]) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= n; i++)
    pre[i] = i;
  int u, v;
  while (m--) {
    scanf("%d%d", &u, &v);
    int u_pre = find(u), v_pre = find(v);
    if (u_pre == v_pre)
      continue;
    pre[u_pre] = v_pre;
  }
  int ans = 0;
  for (register int i = 1; i <= n; i++)
    a[find(i)] += 1, ans = max(ans, a[find(i)]);
  printf("%d\n", ans);
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> d(n);
  vector<int> list(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    d[a].push_back(b);
    d[b].push_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (list[i] == 1)
      continue;
    queue<int> q;
    q.push(i);
    list[i] = 1;
    int sum = 1;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto np : d[p]) {
        list[np] = 1;
        q.push(np);
        sum += 1;
      }
    }
    ans = max(ans, sum);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> d(n);
  vector<int> list(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    d[a].push_back(b);
    d[b].push_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (list[i] == 1)
      continue;
    queue<int> q;
    q.push(i);
    list[i] = 1;
    int sum = 1;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto np : d[p]) {
        if (list[np] == 1)
          continue;
        list[np] = 1;
        q.push(np);
        sum += 1;
      }
    }
    ans = max(ans, sum);
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a, b) memset(a, b, sizeof a)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a, b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6 + 10, M = 2e6 + 10, mod = 1e9 + 7, inf = 0x3f3f3f;
ll cnt[N], pre[N];

int find(int x) { return x == pre[x] ? x : find(pre[x]); }

int main() {
  IOS;
  ll n, m, ans = MIN;
  cin >> n >> m;
  rep1(i, n) pre[i] = i;
  while (m--) {
    int a, b;
    cin >> a >> b;
    int x = find(a), y = find(b);
    if (x != y)
      pre[x] = y;
  }
  rep1(i, n) {
    int x = find(i);
    cnt[x]++, ans = max(ans, cnt[x]);
  }
  cout << ans << nl;

  AC;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a, b) memset(a, b, sizeof a)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a, b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6 + 10, M = 2e6 + 10, mod = 1e9 + 7, inf = 0x3f3f3f;
ll cnt[N], pre[N];

int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }

int main() {
  IOS;
  ll n, m, ans = MIN;
  cin >> n >> m;
  rep1(i, n) pre[i] = i;
  while (m--) {
    int a, b;
    cin >> a >> b;
    int x = find(a), y = find(b);
    if (x != y)
      pre[x] = y;
  }
  rep1(i, n) {
    int x = find(i);
    cnt[x]++, ans = max(ans, cnt[x]);
  }
  cout << ans << nl;

  AC;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
// control+option+n to run!
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define NEGFOR(i, a, b) for (ll i = (a); i > (b); i--)
#define vll vector<long long>
#define sll set<long long>
#define ld long double
#define inf 1000000000000000000;
#define mll multiset<long long>
#define nn << "\n"
#define F(i, b) for (ll i = 0; i < b; i++)
// 10^8 operations per second
// greatest int is 2,147,483,647
// greates long long is 9.22337204e18
// ALL FUNCTIONS SHOULD BE LL!!!!
int n, m;
ll co = 0;
vector<int> v[100002];
bool visited[100002];
void dfs(int node) {
  co++;
  visited[node] = true;
  for (int i = 0; i < v[node].size(); ++i) {
    int nxt = v[node][i];
    if (visited[nxt] == false)
      dfs(nxt);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;

  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vll vec;
  for (int i = 0; i < n; ++i) {
    if (visited[i] == false) {
      dfs(i);
      vec.pb(co);
      co = 0;
    }
  }
  ll x = vec.size();
  sort(vec.begin(), vec.end());
  ll currdelete = 0;
  ll ans = 0;
  FOR(i, 0, vec.size()) {
    ans += vec[i] - currdelete;
    currdelete = vec[i];
  }
  cout << ans;

  cout << "\n"; // GET RID OF THIS FOR THE ACTUAL PROGRAM!!!!
}


Fixed submission:
// control+option+n to run!
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define NEGFOR(i, a, b) for (ll i = (a); i > (b); i--)
#define vll vector<long long>
#define sll set<long long>
#define ld long double
#define inf 1000000000000000000;
#define mll multiset<long long>
#define nn << "\n"
#define F(i, b) for (ll i = 0; i < b; i++)
// 10^8 operations per second
// greatest int is 2,147,483,647
// greates long long is 9.22337204e18
// ALL FUNCTIONS SHOULD BE LL!!!!
int n, m;
ll co = 0;
vector<int> v[500002];
bool visited[500002];
void dfs(int node) {
  co++;
  visited[node] = true;
  for (int i = 0; i < v[node].size(); ++i) {
    int nxt = v[node][i];
    if (visited[nxt] == false)
      dfs(nxt);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;

  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  vll vec;
  for (int i = 0; i < n; ++i) {
    if (visited[i] == false) {
      dfs(i);
      vec.pb(co);
      co = 0;
    }
  }
  ll x = vec.size();
  sort(vec.begin(), vec.end());
  ll currdelete = 0;
  ll ans = 0;
  FOR(i, 0, vec.size()) {
    ans += vec[i] - currdelete;
    currdelete = vec[i];
  }
  cout << ans;

  cout << "\n"; // GET RID OF THIS FOR THE ACTUAL PROGRAM!!!!
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
struct UnionFind {
  vector<int> d;                     // データ
  UnionFind(int n = 0) : d(n, -1) {} // 初期値は全部根(-1)とする
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) { // 2つ頂点をくっつける
    x = find(x);
    y = find(y);
    if (x == y)
      return false; // 一致していればOK
    if (d[x] > d[y])
      swap(x, y); // サイズが大きいものをxとする（根は負なので大小関係に注意）
    d[x] += d[y]; // サイズの合算
    d[y] = x;     // 根をxとする
    return false;
  }
  bool same(int x, int y) { // 同じ集合に属しているかを判定する
    return find(x) == find(y);
  }
  int size(int x) { // サイズを求める（根はマイナスなので、マイナスをとる）
    return -d[find(x)];
  }
};
int deg[100005];
vector<int> to[100005];
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  int saidai = 0;
  rep(i, n) { saidai = max(saidai, uf.size(i)); }
  cout << saidai << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
struct UnionFind {
  vector<int> d;                     // データ
  UnionFind(int n = 0) : d(n, -1) {} // 初期値は全部根(-1)とする
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) { // 2つ頂点をくっつける
    x = find(x);
    y = find(y);
    if (x == y)
      return false; // 一致していればOK
    if (d[x] > d[y])
      swap(x, y); // サイズが大きいものをxとする（根は負なので大小関係に注意）
    d[x] += d[y]; // サイズの合算
    d[y] = x;     // 根をxとする
    return false;
  }
  bool same(int x, int y) { // 同じ集合に属しているかを判定する
    return find(x) == find(y);
  }
  int size(int x) { // サイズを求める（根はマイナスなので、マイナスをとる）
    return -d[find(x)];
  }
};
int deg[200200];
vector<int> to[200200];
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  int saidai = 0;
  rep(i, n) { saidai = max(saidai, uf.size(i)); }
  cout << saidai << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 1e5 + 7;

int N, M;
vector<int> graph[MAXN];
bool visited[MAXN];

int DFS(int cur) {
  int ret = 1;
  visited[cur] = true;
  for (auto adj : graph[cur]) {
    if (visited[adj])
      continue;
    ret += DFS(adj);
  }

  return ret;
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

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

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 7;

int N, M;
vector<int> graph[MAXN];
bool visited[MAXN];

int DFS(int cur) {
  int ret = 1;
  visited[cur] = true;
  for (auto adj : graph[cur]) {
    if (visited[adj])
      continue;
    ret += DFS(adj);
  }

  return ret;
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

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

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

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
vector<int> adj[MAXN];
bool visited[MAXN];
int n, m, mx = 0, curr = 0;

void dfs(int node) {
  visited[node] = true;
  for (int next : adj[node]) {
    if (!visited[next]) {
      curr++;
      dfs(next);
    }
  }
}

int main() {
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      curr = 1;
      dfs(i);
      mx = max(mx, curr);
    }
  }
  cout << mx << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
vector<int> adj[MAXN];
bool visited[MAXN];
int n, m, mx = 0, curr = 0;

void dfs(int node) {
  visited[node] = true;
  for (int next : adj[node]) {
    if (!visited[next]) {
      curr++;
      dfs(next);
    }
  }
}

int main() {
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      curr = 1;
      dfs(i);
      mx = max(mx, curr);
    }
  }
  cout << mx << endl;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  vector<vector<int>> f(N);
  for (int i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    int a = A.at(i) - 1;
    int b = B.at(i) - 1;
    if (find(f[a].begin(), f[a].end(), b) == f[a].end()) {
      f[a].push_back(b);
    }
    if (find(f[b].begin(), f[b].end(), a) == f[b].end()) {
      f[b].push_back(a);
    }
  }
  int ans = 0;
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    if (seen.at(i))
      continue;
    seen.at(i) = true;
    int num = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int j = q.front();
      q.pop();
      for (auto v : f[j]) {
        if (seen.at(v))
          continue;
        seen.at(v) = true;
        q.push(v);
        num++;
      }
    }
    ans = max(ans, num);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  vector<vector<int>> f(N);
  for (int i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    int a = A.at(i) - 1;
    int b = B.at(i) - 1;
    f[a].push_back(b);
    f[b].push_back(a);
  }
  int ans = 0;
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    if (seen.at(i))
      continue;
    seen.at(i) = true;
    int num = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int j = q.front();
      q.pop();
      for (auto v : f[j]) {
        if (seen.at(v))
          continue;
        seen.at(v) = true;
        q.push(v);
        num++;
      }
    }
    ans = max(ans, num);
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pi acos(-1.0)
#define pb push_back
#define vi vector<int>
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define CP(n) cout << "Case " << n << ": "
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
#define MAX 1000007
#define all(x) (x.begin(), x.end())
#define debug(x, c) cout << x << ": " << c << " ";
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
using namespace std;
int par[200005];
mapii mp;
int find_parent(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find_parent(par[a]);
}
void dsu(int a, int b) {
  int x = find_parent(a);
  int y = find_parent(b);
  par[x] = y;
  par[y] = x;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    par[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dsu(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    mp[find_parent(i)]++;

  for (auto x : mp)
    ans = max(ans, x.second);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pi acos(-1.0)
#define pb push_back
#define vi vector<int>
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define CP(n) cout << "Case " << n << ": "
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
#define MAX 1000007
#define all(x) (x.begin(), x.end())
#define debug(x, c) cout << x << ": " << c << " ";
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
using namespace std;
int par[200005];
mapii mp;
int find_parent(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find_parent(par[a]);
}
void dsu(int a, int b) {
  int x = find_parent(a);
  int y = find_parent(b);
  if (y > x)
    par[y] = x;
  else
    par[x] = y;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    par[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dsu(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    mp[find_parent(i)]++;

  for (auto x : mp)
    ans = max(ans, x.second);
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

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

using namespace std;

const int MAX = 1000 * 100 + 11;
vector<int> a[MAX];
vector<bool> vis(MAX);
int n, m;
int res = 0, curr = 0;

void dfs(int currNode) {
  if (vis[currNode]) {
    return;
  }

  curr++;
  vis[currNode] = true;
  for (auto it : a[currNode]) {
    dfs(it);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  for (int i = 0; i < n; i++) {
    dfs(i);
    res = max(res, curr);
    curr = 0;
  }

  cout << res;
  return 0;
}

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

using namespace std;

const int MAX = 2000 * 100 + 11;
vector<int> a[MAX];
vector<bool> vis(MAX);
int n, m;
int res = 0, curr = 0;

void dfs(int currNode) {
  if (vis[currNode]) {
    return;
  }

  curr++;
  vis[currNode] = true;
  for (auto it : a[currNode]) {
    dfs(it);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }

  for (int i = 0; i < n; i++) {
    dfs(i);
    res = max(res, curr);
    curr = 0;
  }

  cout << res;
  return 0;
}
Problem p02573 (C++)

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

int root[100010];    // root[i]=i's parent
int element[100010]; // element[i]=i's group's element
// element[find(i)] is better

void init(int n) {
  for (int i = 0; i < n; i++)
    root[i] = i, element[i] = 1;
} // reset
int find(int x) {
  if (root[x] == x) {
    return x;
  } else {
    return root[x] = find(root[x]);
  }
} // search x's parent
bool same(int x, int y) { return find(x) == find(y); }
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  root[x] = y;
  element[y] += element[x];
} // x"->"y

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, element[i]);
  }
  cout << ans << endl;
}

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

int root[200010];    // root[i]=i's parent
int element[200010]; // element[i]=i's group's element
// element[find(i)] is better

void init(int n) {
  for (int i = 0; i < n; i++)
    root[i] = i, element[i] = 1;
} // reset
int find(int x) {
  if (root[x] == x) {
    return x;
  } else {
    return root[x] = find(root[x]);
  }
} // search x's parent
bool same(int x, int y) { return find(x) == find(y); }
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  root[x] = y;
  element[y] += element[x];
} // x"->"y

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, element[i]);
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

struct group {
  int num;
  int size;
};

int root(struct group *data, int x) {
  if ((data + x)->num == x)
    return x;
  else
    return root(data, (data + x)->num);
}

void unite(struct group *data, int x, int y) {
  int rx = root(data, x), ry = root(data, y);
  if (rx == ry)
    return;
  (data + rx)->num = ry;
  (data + ry)->size += (data + rx)->size;
}

int main() {
  int N, M;
  cin >> N >> M;
  struct group data[N];

  rep(i, 0, N) {
    data[i].num = i;
    data[i].size = 1;
  }

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(data, a, b);
  }

  int ans = 0;
  rep(i, 0, N) { ans = _max(ans, data[i].size); }

  cout << ans;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

struct group {
  int num;
  int size;
};

int root(struct group *data, int x) {
  if ((data + x)->num == x)
    return x;
  else {
    int rx = root(data, (data + x)->num);
    (data + x)->num = rx;
    return rx;
  }
}

void unite(struct group *data, int x, int y) {
  int rx = root(data, x), ry = root(data, y);
  if (rx == ry)
    return;
  (data + rx)->num = ry;
  (data + ry)->size += (data + rx)->size;
}

int main() {
  int N, M;
  cin >> N >> M;
  struct group data[N];

  rep(i, 0, N) {
    data[i].num = i;
    data[i].size = 1;
  }

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(data, a, b);
  }

  int ans = 0;
  rep(i, 0, N) { ans = _max(ans, data[i].size); }

  cout << ans;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, sum = 0;
bool vis[200005];
vector<int> fr[200005];
void dfs(int a) {
  if (vis[a])
    return;
  vis[a] = true;
  sum++;
  for (int i = 0; i < fr[a].size(); ++i)
    if (!vis[fr[a][i]])
      dfs(fr[a][i]);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (find(fr[x].begin(), fr[x].end(), y) == fr[x].end())
      fr[x].push_back(y);
    if (find(fr[y].begin(), fr[y].end(), x) == fr[y].end())
      fr[y].push_back(x);
  }
  for (int i = 0; i < 200005; ++i)
    vis[i] = false;
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, sum);
      sum = 0;
    }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, sum = 0;
bool vis[200005];
vector<int> fr[200005];
void dfs(int a) {
  vis[a] = true;
  sum++;
  for (int i = 0; i < fr[a].size(); ++i)
    if (!vis[fr[a][i]])
      dfs(fr[a][i]);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (find(fr[x].begin(), fr[x].end(), y) == fr[x].end())
      fr[x].push_back(y);
    if (find(fr[y].begin(), fr[y].end(), x) == fr[y].end())
      fr[y].push_back(x);
  }
  for (int i = 0; i < 200005; ++i)
    vis[i] = false;
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, sum);
      sum = 0;
    }
  cout << ans;
  return 0;
}
Problem p02573 (C++)

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

using namespace std;

#define MAX_SIZE 200005

int parent[MAX_SIZE];

int Find(int x) {
  if (parent[x] == -1) {
    return x;
  }
  return parent[x] = Find(parent[x]);
}

int Union(int x, int y) {
  int px = Find(x), py = Find(y);
  if (px != py) {
    parent[py] = px;
  }
}

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

  memset(parent, -1, sizeof(parent));

  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(x, y);
  }

  unordered_map<int, int> group;
  for (int i = 1; i <= n; ++i) {
    group[Find(i)]++;
  }

  int ans = 0;
  for (const auto &p : group) {
    if (p.second > ans) {
      ans = p.second;
    }
  }

  printf("%d\n", ans);
  return 0;
}

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

using namespace std;

#define MAX_SIZE 200005

int parent[MAX_SIZE];

int Find(int x) {
  if (parent[x] == -1) {
    return x;
  }
  return parent[x] = Find(parent[x]);
}

void Union(int x, int y) {
  int px = Find(x), py = Find(y);
  if (px != py) {
    parent[py] = px;
  }
}

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

  memset(parent, -1, sizeof(parent));

  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(x, y);
  }

  unordered_map<int, int> group;
  for (int i = 1; i <= n; ++i) {
    group[Find(i)]++;
  }

  int ans = 0;
  for (const auto &p : group) {
    if (p.second > ans) {
      ans = p.second;
    }
  }

  printf("%d\n", ans);
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<long long ll>
#define pb push_back
#define f(i, x, n) for (i = x; i < n; i++)
ll visited[1000];
vector<ll> a[1000];
ll c = 0;
void dfs(ll k) {
  if (visited[k] != 1) {
    visited[k] = 1;
    c = c + 1;
    for (ll i = 0; i < (ll)a[k].size(); i++) {
      if (visited[a[k][i]] != 1)
        dfs(a[k][i]);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    c = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, c);
    }
    // cout << c <<" ";
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  while (t--)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<long long ll>
#define pb push_back
#define f(i, x, n) for (i = x; i < n; i++)
ll visited[1000000];
vector<ll> a[1000000];
ll c = 0;
void dfs(ll k) {
  if (visited[k] != 1) {
    visited[k] = 1;
    c = c + 1;
    for (ll i = 0; i < (ll)a[k].size(); i++) {
      if (visited[a[k][i]] != 1)
        dfs(a[k][i]);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    c = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, c);
    }
    // cout << c <<" ";
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  while (t--)
    solve();
}