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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

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

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = find_(x);
    int fy = find_(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

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

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = find_(x);
    int fy = find_(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++)

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

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 500 + 5, mod = 1e9 + 7, M = 1e5 + 5;

int p[N], sz[N];

void make_set(int v) {
  p[v] = v;
  sz[v] = 1;
}

int find_set(int v) {
  if (p[v] == v)
    return v;
  return p[v] = find_set(p[v]);
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  p[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    make_set(i);
  while (m--) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, sz[i]);
  cout << mx << endl;
}

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

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 2e5 + 5, mod = 1e9 + 7, M = 1e5 + 5;

int p[N], sz[N];

void make_set(int v) {
  p[v] = v;
  sz[v] = 1;
}

int find_set(int v) {
  if (p[v] == v)
    return v;
  return p[v] = find_set(p[v]);
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  p[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    make_set(i);
  while (m--) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, sz[i]);
  cout << mx << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll, ll>
#define pb push_back
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int N = 1e5 + 5;

vector<int> vis(N, 0);
vector<int> adj[N];
int mx = 0;

int dfs(int u) {
  int ans = 0;
  if (!vis[u]) {
    vis[u] = true;
    ans++;
    for (auto v : adj[u]) {
      ans += dfs(v);
    }
  }
  return ans;
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      mx = max(mx, dfs(i));
    }
  }
  cout << mx;
}

int main() {
  fast;
  solve();
  // int t;
  // cin >> t;
  // while(t--){
  //     solve();
  // }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll, ll>
#define pb push_back
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int N = 2e5 + 5;

vector<int> vis(N, 0);
vector<int> adj[N];
int mx = 0;

int dfs(int u) {
  int ans = 0;
  if (!vis[u]) {
    vis[u] = true;
    ans++;
    for (auto v : adj[u]) {
      ans += dfs(v);
    }
  }
  return ans;
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      mx = max(mx, dfs(i));
    }
  }
  cout << mx;
}

int main() {
  fast;
  solve();
  // int t;
  // cin >> t;
  // while(t--){
  //     solve();
  // }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mxn = 1e5 + 5;
// check for forloop
// intialize variable
// overflow
// go for easy solution
#define mod 1000000007
ll INF = 1000000000000000005LL;
#define endl '\n'
void rishabh() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[mxn];
bool vis[mxn];
int dfs(int i) {
  vis[i] = true;
  int temp = 0;
  for (auto x : adj[i]) {
    if (!vis[x])
      temp = temp + dfs(x);
  }
  return temp + 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      int temp = dfs(i);
      ans = max(ans, temp);
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mxn = 2e5 + 5;
// check for forloop
// intialize variable
// overflow
// go for easy solution
#define mod 1000000007
ll INF = 1000000000000000005LL;
#define endl '\n'
void rishabh() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[mxn];
bool vis[mxn];
int dfs(int i) {
  vis[i] = true;
  int temp = 0;
  for (auto x : adj[i]) {
    if (!vis[x])
      temp = temp + dfs(x);
  }
  return temp + 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      int temp = dfs(i);
      ans = max(ans, temp);
    }
  }
  cout << ans;
}
Problem p02573 (C++)

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

int parent_find(int x, vector<int> &parent) {
  while (parent[x] != x) {
    x = parent[x];
  }
  return x;
}

void combine(int x, int y, vector<int> &parent, vector<int> &rank) {
  int t = parent_find(x, parent);
  int w = parent_find(y, parent);
  if (t != w) {
    parent[t] = w;
    rank[w] += rank[t];
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> group(n + 1, 0);
  vector<int> rank(n + 1, 1);
  for (int i = 0; i <= n; i++)
    group[i] = i;

  int t;
  cin >> t;
  while (t--) {
    int x, y;
    cin >> x >> y;
    combine(x, y, group, rank);
  }

  cout << *max_element(rank.begin(), rank.end()) << endl;

  return 0;
}

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

int parent_find(int x, vector<int> &parent) {
  while (parent[x] != x) {
    x = parent[x];
  }
  return x;
}

void combine(int x, int y, vector<int> &parent, vector<int> &rank) {
  int t = parent_find(x, parent);
  int w = parent_find(y, parent);
  if (t != w) {

    if (rank[w] > rank[t]) {
      parent[t] = w;
      rank[w] += rank[t];
    } else {
      parent[w] = t;
      rank[t] += rank[w];
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> group(n + 1, 0);
  vector<int> rank(n + 1, 1);
  for (int i = 0; i <= n; i++)
    group[i] = i;

  int t;
  cin >> t;
  while (t--) {
    int x, y;
    cin >> x >> y;
    combine(x, y, group, rank);
  }

  cout << *max_element(rank.begin(), rank.end()) << endl;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define N ((int)2e5 + 5)
using namespace std;
bool vis[N];
int dis[N];
vector<int> vec[N];
int bfs(int src) {
  memset(vis, 0, sizeof vis);
  int cnt = 1;
  queue<int> que;
  que.push(src);
  vis[src] = 1;
  dis[src] = 0;
  while (!que.empty()) {
    int a = que.front();
    que.pop();
    for (int b : vec[a])
      if (vis[b] == 0) {
        cnt++;
        que.push(b);
        vis[b] = 1;
        dis[b] = dis[a] + 1;
      }
  }
  return cnt;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      mx = max(mx, bfs(i));
    }
  }
  cout << mx << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N ((int)2e5 + 5)
using namespace std;
bool vis[N];
int dis[N];
vector<int> vec[N];
int bfs(int src) {
  int cnt = 1;
  queue<int> que;
  que.push(src);
  vis[src] = 1;
  dis[src] = 0;
  while (!que.empty()) {
    int a = que.front();
    que.pop();
    for (int b : vec[a])
      if (vis[b] == 0) {
        cnt++;
        que.push(b);
        vis[b] = 1;
        dis[b] = dis[a] + 1;
      }
  }
  return cnt;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      mx = max(mx, bfs(i));
    }
  }
  cout << mx << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
typedef struct {
  int Parent, Rank;
} UnionFind;
UnionFind uft[100000];
static inline int Root(int x) {
  return (uft[x].Parent != x) ? uft[x].Parent = Root(uft[x].Parent) : x;
}
static inline int Unite(int x, int y) {
  x = Root(x), y = Root(y);
  if (x != y)
    uft[x].Parent = y, uft[y].Rank += uft[x].Rank;
  return x != y;
}
static inline int Size(int x) { return uft[Root(x)].Rank; }
static inline int Belong(int x, int y) { return Root(x) == Root(y); }
int main(void) {
  int A, B, N, M, i = 0, estrange = 0;
  if (!scanf("%d %d", &N, &M))
    return -1;
  while (i < N) {
    uft[i].Parent = i;
    uft[i++].Rank = 1;
  }
  while (M--) {
    if (!scanf("%d %d", &A, &B)) {
      return -1;
    }
    A--, B--;
    Unite(A, B);
  }
  while (N--) {
    estrange = estrange > Size(N - 1) ? estrange : Size(N - 1);
  }
  return printf("%d\n", estrange), 0;
}

Fixed submission:
#include <stdio.h>
typedef struct {
  int Parent, Rank;
} UnionFind;
UnionFind uft[200000];
static inline int Root(int x) {
  return (uft[x].Parent != x) ? uft[x].Parent = Root(uft[x].Parent) : x;
}
static inline int Unite(int x, int y) {
  x = Root(x), y = Root(y);
  if (x != y)
    uft[x].Parent = y, uft[y].Rank += uft[x].Rank;
  return x != y;
}
static inline int Size(int x) { return uft[Root(x)].Rank; }
static inline int Belong(int x, int y) { return Root(x) == Root(y); }
int main(void) {
  int A, B, N, M, i = 0, estrange = 0;
  if (!scanf("%d %d", &N, &M))
    return -1;
  while (i < N) {
    uft[i].Parent = i;
    uft[i++].Rank = 1;
  }
  while (M--) {
    if (!scanf("%d %d", &A, &B)) {
      return -1;
    }
    A--, B--;
    Unite(A, B);
  }
  while (N--) {
    estrange = estrange > Size(N - 1) ? estrange : Size(N - 1);
  }
  return printf("%d\n", estrange), 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define ll long long
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define vi vector<int>
#define ff first
#define ss second
#define fr(a, b) for (int i = a; i <= b; i++)
#define inf 1e18
#define mod 1000000007
vector<int> v[100005];
bool vis[100005];
void dfs(int src, int &c) {
  c++;
  vis[src] = true;
  for (auto nvr : v[src]) {
    if (vis[nvr] == false)
      dfs(nvr, c);
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = INT_MIN;
  int c = 0;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define ll long long
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define vi vector<int>
#define ff first
#define ss second
#define fr(a, b) for (int i = a; i <= b; i++)
#define inf 1e18
#define mod 1000000007
vector<int> v[200005];
bool vis[200005];
void dfs(int src, int &c) {
  c++;
  vis[src] = true;
  for (auto nvr : v[src]) {
    if (vis[nvr] == false)
      dfs(nvr, c);
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = INT_MIN;
  int c = 0;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }

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

  int size(int x) { return -r[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int 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;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }

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

  int size(int x) { return -r[root(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: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using Graph = vector<vector<int>>;
map<ll, ll> mp;

vector<bool> seen;
void dfs(const Graph &G, int v, int c) {
  seen[v] = true;
  mp[c]++;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, c);
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  seen.assign(N, false);
  int cnt = 0;
  rep(i, N) {
    if (!seen[i]) {
      dfs(G, i, cnt);
      cnt++;
    }
  }
  ll ans = 0;
  while (mp.size()) {
    ans++;
    for (auto x : mp) {
      mp[x.first]--;
    }
    for (auto x : mp)
      if (x.second == 0)
        mp.erase(x.first);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using Graph = vector<vector<int>>;
map<ll, ll> mp;

vector<bool> seen;
void dfs(const Graph &G, int v, int c) {
  seen[v] = true;
  mp[c]++;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, c);
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  seen.assign(N, false);
  int cnt = 0;
  rep(i, N) {
    if (!seen[i]) {
      dfs(G, i, cnt);
      cnt++;
    }
  }
  ll ans = 0;
  for (auto x : mp)
    chmax(ans, x.second);
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e6 + 1000;
const LL P = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int n, m;
int f[N], sz[N];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    f[x] = find(f[x]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
    sz[i] = 1;
  }
  int u, v;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    u = find(u);
    v = find(v);
    if (u == v)
      continue;
    f[u] = v;
    sz[v] += sz[u];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    u = find(i);
    ans = max(ans, sz[u]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e6 + 1000;
const LL P = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int n, m;
int f[N], sz[N];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
    sz[i] = 1;
  }
  int u, v;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    u = find(u);
    v = find(v);
    if (u == v)
      continue;
    f[u] = v;
    sz[v] += sz[u];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    u = find(i);
    ans = max(ans, sz[u]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
/*
 * @Author: dnhirapara
 * @Problem: abc177_d
 * @Time: 11-September-2020 : ( 16:17:57 )
 */
#include <bits/stdc++.h>

using namespace std;

/************defination************/

#define endl "\n"
#define logger(x) cout << __LINE__ << ": " << #x << " -> " << (x) << endl;
#define ll long long int
#define ull unsigned long long int

vector<ll> g[100005];
vector<bool> vis(100005, 0);

ll max_len = 0;

void dfs(ll node, ll lvl) {
  vis[node] = true;
  for (auto i : g[node]) {
    // logger(i);
    if (!vis[i]) {
      dfs(i, lvl + 1);
      max_len++;
    }
  }
  // max_len = max(lvl, max_len);
}

//@time comp :
//@space comp :
int main() {
  // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  // cin.ignore(); must be there when using getline(cin, s)
  ll tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m;
    cin >> n >> m;
    // logger(n);
    ll u, v;
    for (int i = 0; i <= n; i++) {
      vis[i] = false;
    }
    for (ll i = 0; i < m; i++) {
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    ll maxi = LLONG_MIN;
    for (ll i = 1; i <= n; i++) {
      if (!vis[i]) {
        max_len = 0;
        dfs(i, 0);
        // logger(max_len);
        maxi = max(max_len, maxi);
      }
    }
    cout << maxi + 1 << endl;
  }
  return 0;
}

Fixed submission:
/*
 * @Author: dnhirapara
 * @Problem: abc177_d
 * @Time: 11-September-2020 : ( 16:17:57 )
 */
#include <bits/stdc++.h>

using namespace std;

/************defination************/

#define endl "\n"
#define logger(x) cout << __LINE__ << ": " << #x << " -> " << (x) << endl;
#define ll long long int
#define ull unsigned long long int

vector<ll> g[200005];
vector<bool> vis(200005, 0);

ll max_len = 0;

void dfs(ll node, ll lvl) {
  vis[node] = true;
  for (auto i : g[node]) {
    // logger(i);
    if (!vis[i]) {
      dfs(i, lvl + 1);
      max_len++;
    }
  }
  // max_len = max(lvl, max_len);
}

//@time comp :
//@space comp :
int main() {
  // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  // cin.ignore(); must be there when using getline(cin, s)
  ll tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m;
    cin >> n >> m;
    // logger(n);
    ll u, v;
    for (int i = 0; i <= n; i++) {
      vis[i] = false;
    }
    for (ll i = 0; i < m; i++) {
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    ll maxi = LLONG_MIN;
    for (ll i = 1; i <= n; i++) {
      if (!vis[i]) {
        max_len = 0;
        dfs(i, 0);
        // logger(max_len);
        maxi = max(max_len, maxi);
      }
    }
    cout << maxi + 1 << endl;
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define per(i, a, b) for (int i = (a); i >= (int)(b); i--)
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int fa[maxn], siz[maxn];
int Find(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = Find(fa[x]);
}
void Union(int a, int b) {
  int f1 = Find(a), f2 = Find(b);
  siz[f1] += siz[f2];
  fa[f2] = f1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) {
    fa[i] = i;
    siz[i] = 1;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    if (Find(a) != Find(b))
      Union(a, b);
  }
  int ans = 0;
  rep(i, 1, n) {
    if (i == fa[i])
      ans = max(ans, siz[i]);
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define per(i, a, b) for (int i = (a); i >= (int)(b); i--)
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int fa[maxn], siz[maxn];
int Find(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = Find(fa[x]);
}
void Union(int a, int b) {
  int f1 = Find(a), f2 = Find(b);
  siz[f1] += siz[f2];
  fa[f2] = f1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) {
    fa[i] = i;
    siz[i] = 1;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    if (Find(a) != Find(b))
      Union(a, b);
  }
  int ans = 0;
  rep(i, 1, n) {
    if (i == fa[i])
      ans = max(ans, siz[i]);
  }
  cout << ans << '\n';
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define re return
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<int> v[100005];
int vis[100005];
int dfs(int now) {
  vis[now] = 1;
  int num = 1;
  for (int i : v[now])
    if (!vis[i])
      num += dfs(i);
  re num;
}
signed main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    v[b].pb(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
  re 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define re return
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<int> v[200005];
int vis[200005];
int dfs(int now) {
  vis[now] = 1;
  int num = 1;
  for (int i : v[now])
    if (!vis[i])
      num += dfs(i);
  re num;
}
signed main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    v[b].pb(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
  re 0;
}
Problem p02573 (C++)

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

int getID(vector<int> &v, int idx) {
  if (v[idx] == -1)
    return -1;
  int cnt = 0;
  while (1) {
    if (v[idx] == idx)
      break;
    idx = v[idx];
    if (cnt++ == 10000)
      break;
  }
  return idx;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> d(N, 0);
  vector<int> v(N, -1);
  for (int i = 0; i < M; i++) {
    int s[2];
    cin >> s[0] >> s[1];
    s[0]--;
    s[1]--;

    if (v[s[0]] == -1 && v[s[1]] == -1) {
      v[s[0]] = s[0];
      v[s[1]] = s[0];
      d[s[0]] = 2;
    } else if (v[s[0]] == -1 || v[s[1]] == -1) {
      for (int j = 0; j < 2; j++) {
        if (v[s[j]] == -1) {
          int id = getID(v, s[(j + 1) % 2]);
          v[s[j]] = id;
          d[id]++;
        }
      }
    } else {
      int r1 = getID(v, s[0]);
      int r2 = getID(v, s[1]);
      if (r1 == r2)
        continue;
      v[r1] = r2;
      d[r2] += d[r1];
      d[r1] = 0;
    }
  }
  int ans = 1;
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}

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

int getID(vector<int> &v, int idx) {
  if (v[idx] == idx)
    return idx;
  v[idx] = getID(v, v[idx]);
  return v[idx];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> d(N, 0);
  vector<int> v(N, -1);
  for (int i = 0; i < M; i++) {
    int s[2];
    cin >> s[0] >> s[1];
    s[0]--;
    s[1]--;

    if (v[s[0]] == -1 && v[s[1]] == -1) {
      v[s[0]] = s[0];
      v[s[1]] = s[0];
      d[s[0]] = 2;
    } else if (v[s[0]] == -1 || v[s[1]] == -1) {
      for (int j = 0; j < 2; j++) {
        if (v[s[j]] == -1) {
          int id = getID(v, s[(j + 1) % 2]);
          v[s[j]] = id;
          d[id]++;
        }
      }
    } else {
      int r1 = getID(v, s[0]);
      int r2 = getID(v, s[1]);
      if (r1 == r2)
        continue;
      v[r1] = r2;
      d[r2] += d[r1];
      d[r1] = 0;
    }
  }
  int ans = 1;
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using ll = long long;
using namespace std;
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define ar array

const int N = 1e5 + 9;
vector<int> g[N];
bool used[N];
int val[N];
int n, m;

int dfs(int u) {
  used[u] = 1;
  int y = 1;
  for (auto x : g[u]) {
    if (used[x])
      continue;
    y += dfs(x);
  }
  return y;
}

void solve() {
  memset(used, 0, sizeof(used));
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  int ans = -1e9;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int y = dfs(i);
      ans = max(ans, y);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("input.txt",r,"stdin");
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using ll = long long;
using namespace std;
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define ar array

const int N = 2e5 + 9;
vector<int> g[N];
bool used[N];
int val[N];
int n, m;

int dfs(int u) {
  used[u] = 1;
  int y = 1;
  for (auto x : g[u]) {
    if (used[x])
      continue;
    y += dfs(x);
  }
  return y;
}

void solve() {
  memset(used, 0, sizeof(used));
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  int ans = -1e9;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int y = dfs(i);
      ans = max(ans, y);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("input.txt",r,"stdin");
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define foi(n) for (ll i = 0; i < n; i++)
#define foj(n) for (ll j = 0; j < n; j++)
#define pb push_back
using namespace std;

ll a[100005];
vector<ll> v[100005];
bool visited[100005] = {false};

void bfs(ll i, ll cnt) {
  visited[i] = true;
  a[i] = cnt;
  foj(v[i].size()) {
    if (!visited[v[i][j]])
      bfs(v[i][j], cnt);
  }
}

int main() {

  ll n, m;
  cin >> n >> m;

  ll cnt = 1;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      bfs(i, cnt);
      cnt++;
    }
  }
  sort(a + 1, a + n + 1);
  map<ll, ll> ma;
  foi(n) { ma[a[i + 1]]++; }

  ll ans = 0;
  for (auto it : ma) {
    ans = max(ans, it.second);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define foi(n) for (ll i = 0; i < n; i++)
#define foj(n) for (ll j = 0; j < n; j++)
#define pb push_back
using namespace std;

ll a[200005];
vector<ll> v[200005];
bool visited[200005] = {false};

void bfs(ll i, ll cnt) {
  visited[i] = true;
  a[i] = cnt;
  foj(v[i].size()) {
    if (!visited[v[i][j]])
      bfs(v[i][j], cnt);
  }
}

int main() {

  ll n, m;
  cin >> n >> m;

  ll cnt = 1;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      bfs(i, cnt);
      cnt++;
    }
  }
  sort(a + 1, a + n + 1);
  map<ll, ll> ma;
  foi(n) { ma[a[i + 1]]++; }

  ll ans = 0;
  for (auto it : ma) {
    ans = max(ans, it.second);
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
#define forn(i, n) for (ll i = 0; i < ll(n); i++)

class DisjointSet {
  vector<ll> Parent, Rank;

public:
  DisjointSet(ll n) { forn(i, n + 1) Parent.push_back(i), Rank.push_back(0); }
  ll Find(ll x) {
    while (Parent[x] != x)
      x = Parent[x];
    return x;
  }
  void Union(ll x, ll y) {
    ll u = Find(x);
    ll v = Find(y);
    if (u == v)
      return;
    if (Rank[u] > Rank[v])
      Parent[v] = u;
    else {
      Parent[u] = v;
      if (Rank[u] == Rank[v])
        Rank[u]++;
    }
    return;
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  DisjointSet dis(n);
  ll j, k;
  ll ans = 1;
  forn(i, m) {
    cin >> j >> k;
    dis.Union(j, k);
  }
  map<ll, ll> mp;
  forn(i, n) {
    ll u = dis.Find(i + 1);
    mp[u]++;
    ans = max(mp[u], ans);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
#define forn(i, n) for (ll i = 0; i < ll(n); i++)

class DisjointSet {
  vector<ll> Parent, Rank;

public:
  DisjointSet(ll n) { forn(i, n + 1) Parent.push_back(i), Rank.push_back(0); }
  ll Find(ll x) {
    if (Parent[x] == x)
      return x;
    else
      Parent[x] = Find(Parent[x]);
    return Parent[x];
  }
  void Union(ll x, ll y) {
    ll u = Find(x);
    ll v = Find(y);
    if (u == v)
      return;
    if (Rank[u] > Rank[v])
      Parent[v] = u;
    else {
      Parent[u] = v;
      if (Rank[u] == Rank[v])
        Rank[u]++;
    }
    return;
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  DisjointSet dis(n);
  ll j, k;
  ll ans = 1;
  forn(i, m) {
    cin >> j >> k;
    dis.Union(j, k);
  }
  map<ll, ll> mp;
  forn(i, n) {
    ll u = dis.Find(i + 1);
    mp[u]++;
    ans = max(mp[u], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
using namespace std;
vector<bool> seen;
int dfs(const vector<vector<int>> &G, int v) {
  int size = 1, p = 0;
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue; // next_v が探索済だったらスルー
                // dfs(G, next_v); // 再帰的に探索
    p = dfs(G, next_v);
    size += p;
  }
  return size;
}
int main() {
  int n, i, j, m, ans = 0;
  int a[100000], b[100000];
  cin >> n >> m;
  vector<vector<int>> group(n);
  seen.assign(n, false);
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i] -= 1;
    b[i] -= 1;
    group.at(a[i]).push_back(b[i]);
    group.at(b[i]).push_back(a[i]);
  }
  for (i = 0; i < n; i++) {
    if (seen[i] == false) {
      // ans=dfs(group,i);
      ans = max(ans, dfs(group, i));
    }
  }
  cout << ans << endl;
}


Fixed submission:
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
using namespace std;
vector<bool> seen;
int dfs(const vector<vector<int>> &G, int v) {
  int size = 1, p = 0;
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue; // next_v が探索済だったらスルー
                // dfs(G, next_v); // 再帰的に探索
    p = dfs(G, next_v);
    size += p;
  }
  return size;
}
int main() {
  int n, i, j, m, ans = 0;
  int a[200010], b[200010];
  cin >> n >> m;
  vector<vector<int>> group(n);
  seen.assign(n, false);
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i] -= 1;
    b[i] -= 1;
    group.at(a[i]).push_back(b[i]);
    group.at(b[i]).push_back(a[i]);
  }
  for (i = 0; i < n; i++) {
    if (seen[i] == false) {
      // ans=dfs(group,i);
      ans = max(ans, dfs(group, i));
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> e[200005];
int n, m, ans;
bool vis[200005];
int bfs(int x) {
  queue<int> q;
  int num = 1;
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int i = 0; i < e[k].size(); ++i)
      if (!vis[e[k][i]]) {
        vis[e[k][i]] = 1;
        ++num;
        q.push(e[k][i]);
      }
  }
  if (num > ans)
    ans = num;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> e[200005];
int n, m, ans;
bool vis[200005];
void bfs(int x) {
  queue<int> q;
  int num = 1;
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int i = 0; i < e[k].size(); ++i)
      if (!vis[e[k][i]]) {
        vis[e[k][i]] = 1;
        ++num;
        q.push(e[k][i]);
      }
  }
  if (num > ans)
    ans = num;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  cout << ans;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
vector<int> graph[maxx];
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;
  ++cnt;
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

void MaximumVisit(int n) {
  int visited[maxx];
  memset(visited, 0, sizeof visited);
  int ans[maxx];
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }
  int an = 0;
  for (int i = 1; i <= n; ++i) {
    an = max(an, ans[i]);
  }
  cout << an;
  return;
}
int main() {
  int n, k, a, b;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  MaximumVisit(n);
  return 0;
}


Fixed submission:
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int maxx = 200005;
vector<int> graph[maxx];
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;
  ++cnt;
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

void MaximumVisit(int n) {
  int visited[maxx];
  memset(visited, 0, sizeof visited);
  int ans[maxx];
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }
  int an = 0;
  for (int i = 1; i <= n; ++i) {
    an = max(an, ans[i]);
  }
  cout << an;
  return;
}
int main() {
  int n, k, a, b;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  MaximumVisit(n);
  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  vector<int> par;
  vector<int> siz;

  UnionFind(int n) : par(n), siz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }

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

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

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

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  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 - 1, b - 1);
  }

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

  return 0;
}

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

struct UnionFind {
  vector<int> par;
  vector<int> siz;

  UnionFind(int n) : par(n), siz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }

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

  int size(int x) { return siz[root(x)]; }

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

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  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 - 1, b - 1);
  }

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

  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  vector<int> par;
  vector<int> siz;

  UnionFind(int n) : par(n), siz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }

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

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

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

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  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 - 1, b - 1);
  }

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

  return 0;
}

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

struct UnionFind {
  vector<int> par;
  vector<int> siz;

  UnionFind(int n) : par(n), siz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }

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

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

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

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  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 - 1, b - 1);
  }

  int maxsize = 0;
  for (int i = 0; i < n; i++) {
    maxsize = max(maxsize, uf.siz[i]);
  }
  cout << maxsize << endl;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
int parent[200005], value[200005];
int maxn = 1;

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

void freedom(int n) {
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    value[i] = 1;
  }
  return;
}

void unionxy(int a, int b) {
  if (find(a) == find(b)) {
    return;
  } else {
    value[find(a)] += value[find(b)];
    maxn = max(maxn, value[find(a)]);
    parent[find(b)] = find(a);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  freedom(n);
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    unionxy(a, b);
  }
  cout << maxn << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
int parent[200005], value[200005];
int maxn = 1;

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

void freedom(int n) {
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    value[i] = 1;
  }
  return;
}

void unionxy(int a, int b) {
  if (find(a) == find(b)) {
    return;
  } else {
    value[find(a)] += value[find(b)];
    maxn = max(maxn, value[find(a)]);
    parent[find(b)] = find(a);
    parent[b] = parent[find(b)];
    parent[a] = parent[find(a)];
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  freedom(n);
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    unionxy(a, b);
  }
  cout << maxn << endl;
}