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

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

using ll = long long int;
using P = std::pair<int, int>;

#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rag(con) std::begin(con), std::end(con)

constexpr int MAX = 1e5 + 5;

std::vector<int> to[MAX];

bool used[MAX];

int dfs(int v) {

  int res = 1;

  for (int x : to[v]) {
    if (!used[x]) {
      used[x] = 1;
      res += dfs(x);
    }
  }

  return res;
}

int main() {
  int n, m;

  std::cin >> n >> m;

  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  int ans = 0;

  rep(i, n) {
    if (!used[i]) {
      used[i] = 1;
      ans = std::max(ans, dfs(i));
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

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

using ll = long long int;
using P = std::pair<int, int>;

#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rag(con) std::begin(con), std::end(con)

constexpr int MAX = 2 * 1e5 + 5;

std::vector<int> to[MAX];

bool used[MAX];

int dfs(int v) {

  int res = 1;

  for (int x : to[v]) {
    if (!used[x]) {
      used[x] = 1;
      res += dfs(x);
    }
  }

  return res;
}

int main() {
  int n, m;

  std::cin >> n >> m;

  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  int ans = 0;

  rep(i, n) {
    if (!used[i]) {
      used[i] = 1;
      ans = std::max(ans, dfs(i));
    }
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p02573 (C++)

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

#define f(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define int long long
using namespace std;
#define mod 1000000007
vector<int> adj[200001];
int visited[200001];
int dfs(int s) {
  visited[s] = 1;
  int count = 1;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      count += dfs(adj[s][i]);
    }
  }
  return count;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<int> arr;
  int r, sum1 = 0, ans = 0;
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      r = dfs(i);
      if (r != 1) {
        arr.pb(r);
      }
    }
  }
  sort(arr.rbegin(), arr.rend());
  cout << arr[0] << endl;
  return 0;
}


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

#define f(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define int long long
using namespace std;
#define mod 1000000007
vector<int> adj[200001];
int visited[200001];
int dfs(int s) {
  visited[s] = 1;
  int count = 1;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      count += dfs(adj[s][i]);
    }
  }
  return count;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<int> arr;
  int r, sum1 = 0, ans = 0;
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      r = dfs(i);
      if (r != 1) {
        arr.pb(r);
      }
    }
  }
  if (arr.size() != 0) {
    sort(arr.rbegin(), arr.rend());
    cout << arr[0] << endl;
  } else {
    cout << 1 << endl;
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define ll long long int
#define hell 1000000007LL
using namespace std;
vector<int> adj[100001];
bool v[100001];
ll power(ll x, ll y, ll z) {
  if (y == 0)
    return 1;
  else {
    ll p = power(x, y / 2, z);
    if (y % 2 == 0)
      return (p * p) % z;
    else
      return (x * ((p * p) % z)) % z;
  }
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll po(ll x) {
  ll c = 0;
  while (x > 0) {
    x = x & (x - 1);
    c++;
  }
  return c;
}
int dfs(int x) {
  int c = 1;
  v[x] = 1;
  for (auto it = adj[x].begin(); it != adj[x].end(); it++) {
    if (!v[*it]) {
      c = c + dfs(*it);
    }
  }
  return c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll n, m;
  int k = 0;
  cin >> n >> m;
  ll a[m], b[m];
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    if (mp[{a[i], b[i]}] == 0 && mp[{b[i], a[i]}] == 0) {
      ll x = min(a[i], b[i]), y = max(a[i], b[i]);
      adj[x].push_back(y);
      adj[y].push_back(x);
      mp[{x, y}] = 1;
      mp[{y, x}] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] == false)
      k = max(k, dfs(i));
  }
  cout << k;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define ll long long int
#define hell 1000000007LL
using namespace std;
vector<int> adj[200001];
bool v[200001];
ll power(ll x, ll y, ll z) {
  if (y == 0)
    return 1;
  else {
    ll p = power(x, y / 2, z);
    if (y % 2 == 0)
      return (p * p) % z;
    else
      return (x * ((p * p) % z)) % z;
  }
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll po(ll x) {
  ll c = 0;
  while (x > 0) {
    x = x & (x - 1);
    c++;
  }
  return c;
}
int dfs(int x) {
  int c = 1;
  v[x] = 1;
  for (auto it = adj[x].begin(); it != adj[x].end(); it++) {
    if (!v[*it]) {
      c = c + dfs(*it);
    }
  }
  return c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll n, m;
  int k = 0;
  cin >> n >> m;
  ll a[m], b[m];
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    if (mp[{a[i], b[i]}] == 0 && mp[{b[i], a[i]}] == 0) {
      ll x = min(a[i], b[i]), y = max(a[i], b[i]);
      adj[x].push_back(y);
      adj[y].push_back(x);
      mp[{x, y}] = 1;
      mp[{y, x}] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] == false)
      k = max(k, dfs(i));
  }
  cout << k;
}
Problem p02573 (C++)

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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long par[100007], mx = 1, sz[100007];

void mk(long long n) {
  long long i;
  for (i = 0; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

long long fin(long long a) {
  if (par[a] == a) {
    return a;
  }
  return par[a] = fin(par[a]);
}

void uni(long long a, long long b) {
  a = fin(a);
  b = fin(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    mx = max(mx, sz[a]);
  }
  mx = max(mx, sz[a]);
  mx = max(mx, sz[b]);
}

int main() {
  long long n, m, i, x, y;
  cin >> n >> m;
  mk(n + 1);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    uni(x, y);
  }
  cout << mx;
  return 0;
}


Fixed submission:
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long par[200007], mx = 1, sz[200007];

void mk(long long n) {
  long long i;
  for (i = 0; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

long long fin(long long a) {
  if (par[a] == a) {
    return a;
  }
  return par[a] = fin(par[a]);
}

void uni(long long a, long long b) {
  a = fin(a);
  b = fin(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    mx = max(mx, sz[a]);
  }
  mx = max(mx, sz[a]);
  mx = max(mx, sz[b]);
}

int main() {
  long long n, m, i, x, y;
  cin >> n >> m;
  mk(n + 1);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    uni(x, y);
  }
  cout << mx;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int f[N], sum[N];
int get(int x) {
  if (f[x] != x) {
    f[x] = get(f[x]);
    sum[x] = sum[f[x]];
  }
  return f[x];
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i, sum[i] = 1;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    int f1 = get(a), f2 = get(b);
    if (f1 != f2) {
      f[f1] = f2;
      sum[f2] += sum[f1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, sum[i]);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int f[N], sum[N];
int get(int x) {
  if (f[x] != x) {
    f[x] = get(f[x]);
    sum[x] = sum[f[x]];
  }
  return f[x];
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i, sum[i] = 1;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    int f1 = get(a), f2 = get(b);
    if (f1 != f2) {
      f[f1] = f2;
      sum[f2] += sum[f1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, sum[i]);
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<long long> g[100100];
bool ind[100107];
long long n, m, mx, ans;
void dfs(long long i) {
  ind[i] = 1;
  mx++;
  for (long long v : g[i]) {
    if (ind[v] == 0)
      dfs(v);
  }
}
int main() {
  cin >> n >> m;
  long long x, y;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) {
    if (ind[i] == 0) {
      mx = 0;
      dfs(i);
      ans = max(mx, ans);
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<long long> g[200100];
bool ind[200107];
long long n, m, mx, ans;
void dfs(long long i) {
  ind[i] = 1;
  mx++;
  for (long long v : g[i]) {
    if (ind[v] == 0)
      dfs(v);
  }
}
int main() {
  cin >> n >> m;
  long long x, y;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) {
    if (ind[i] == 0) {
      mx = 0;
      dfs(i);
      ans = max(mx, ans);
    }
  }
  cout << ans;
}
Problem p02573 (C++)

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

using ll = long long;
using pi = pair<int, int>;

const int maxn = 1e5 + 5;
int n;
vector<int> adj[maxn];
int vis[maxn];

int dfs(int node) {
  vis[node] = 1;
  int res = 1;
  for (int nei : adj[node]) {
    if (vis[nei])
      continue;
    res += dfs(nei);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < n; i++)
    vis[i] = 0;

  int res = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      res = max(res, dfs(i));
  }
  cout << res << endl;

  return 0;
}

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

using ll = long long;
using pi = pair<int, int>;

const int maxn = 2e5 + 5;
int n;
vector<int> adj[maxn];
int vis[maxn];

int dfs(int node) {
  vis[node] = 1;
  int res = 1;
  for (int nei : adj[node]) {
    if (vis[nei])
      continue;
    res += dfs(nei);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < n; i++)
    vis[i] = 0;

  int res = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      res = max(res, dfs(i));
  }
  cout << res << endl;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
// atcoder
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dfs(vector<vector<ll>> adj, ll root, ll &count, vector<bool> &vis) {
  vis[root] = true;
  count++;
  for (int i = 0; i < adj[root].size(); i++) {
    if (vis[adj[root][i]] == false)
      dfs(adj, adj[root][i], count, vis);
  }
  return count;
}
int main() {
  ll n, m, u, v, ans = INT_MIN, count;
  cin >> n >> m;
  vector<vector<ll>> adj(n + 1);
  vector<bool> vis(n + 1, false);
  while (m > 0) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    m--;
  }
  for (ll i = 1; i < n + 1; i++) {
    count = 0;
    if (vis[i] == false) {
      ans = max(ans, dfs(adj, i, count, vis));
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
// atcoder
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dfs(vector<vector<ll>> &adj, ll root, ll &count, vector<bool> &vis) {
  vis[root] = true;
  count++;
  for (int i = 0; i < adj[root].size(); i++) {
    if (vis[adj[root][i]] == false)
      dfs(adj, adj[root][i], count, vis);
  }
  return count;
}
int main() {
  ll n, m, u, v, ans = INT_MIN, count;
  cin >> n >> m;
  vector<vector<ll>> adj(n + 1);
  vector<bool> vis(n + 1, false);
  while (m > 0) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    m--;
  }
  for (ll i = 1; i < n + 1; i++) {
    count = 0;
    if (vis[i] == false) {
      ans = max(ans, dfs(adj, i, count, vis));
    }
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 1e15
vector<ll> ad[400009];
map<pair<ll, ll>, ll> mp;
bool vis[400009];
ll A;
ll dfs(ll a) {
  vis[a] = true;
  A++;
  ll i, b, l = ad[a].size();
  for (i = 0; i < l; i++) {
    b = ad[a][i];
    if (!vis[b]) {
      dfs(b);
    }
  }
}
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  cin >> n >> m;
  for (i = 0; i <= n; i++) {
    vis[i] = false;
  }
  for (i = 1; i <= m; i++) {
    cin >> a >> b;
    if (mp[make_pair(a, b)] == 0) {
      mp[make_pair(a, b)] = 1;
      mp[make_pair(b, a)] = 1;
    } else {
      continue;
    }
    ad[a].push_back(b);
    ad[b].push_back(a);
  }

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      A = 0;
      dfs(i);
      ans = max(ans, A);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 1e15
vector<ll> ad[400009];
map<pair<ll, ll>, ll> mp;
bool vis[400009];
ll A;
void dfs(ll a) {
  vis[a] = true;
  A++;
  ll i, b, l = ad[a].size();
  for (i = 0; i < l; i++) {
    b = ad[a][i];
    if (!vis[b]) {
      dfs(b);
    }
  }
}
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  cin >> n >> m;
  for (i = 0; i <= n; i++) {
    vis[i] = false;
  }
  for (i = 1; i <= m; i++) {
    cin >> a >> b;
    if (mp[make_pair(a, b)] == 0) {
      mp[make_pair(a, b)] = 1;
      mp[make_pair(b, a)] = 1;
    } else {
      continue;
    }
    ad[a].push_back(b);
    ad[b].push_back(a);
  }

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      A = 0;
      dfs(i);
      ans = max(ans, A);
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++)

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

using namespace std;

const int MAX = 2 * 1000 * 100 + 11;
int parent[MAX];
int n, m;

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

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

  return find_set(parent[node]);
}

void union_sets(int firstNode, int secondNode) {
  int parentOfFirstNode = find_set(firstNode);
  int parentOfSecondNode = find_set(secondNode);

  if (parentOfFirstNode != parentOfSecondNode) {
    parent[parentOfSecondNode] = parentOfFirstNode;
  }
}

map<int, int> mp;

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

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

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

  for (int i = 1; i <= n; i++) {
    mp[find_set(i)]++;
  }

  int res = 0;
  for (auto it : mp) {
    res = max(res, it.second);
  }

  cout << res;
  return 0;
}


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

using namespace std;

const int MAX = 2 * 1000 * 100 + 11;
int parent[MAX];
int n, m;

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

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

  return parent[node] = find_set(parent[node]);
}

void union_sets(int firstNode, int secondNode) {
  int parentOfFirstNode = find_set(firstNode);
  int parentOfSecondNode = find_set(secondNode);

  if (parentOfFirstNode != parentOfSecondNode) {
    parent[parentOfSecondNode] = parentOfFirstNode;
  }
}

map<int, int> mp;

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

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

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

  for (int i = 1; i <= n; i++) {
    mp[find_set(i)]++;
  }

  int res = 0;
  for (auto it : mp) {
    res = max(res, it.second);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e5 + 7;

int used[N];
vector<int> g[N];

void dfs(int cur, int &c) {
  c++;
  used[cur] = 1;
  for (auto t : g[cur]) {
    if (!used[t]) {
      dfs(t, c);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 2e5 + 7;

int used[N];
vector<int> g[N];

void dfs(int cur, int &c) {
  c++;
  used[cur] = 1;
  for (auto t : g[cur]) {
    if (!used[t]) {
      dfs(t, c);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << '\n';
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<int> par;

  // 要素nで初期化
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  // xが属する集合の根を返す
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  // xとyが同じ集合に属しているかを判定
  bool issame(int x, int y) { return root(x) == root(y); }

  // xが属する集合とyが属する集合をマージ
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // 計算量が減るよ
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  // xが属する集合のサイズ
  int size(int x) { return -par[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;
    uf.merge(a, b);
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<int> par;

  // 要素nで初期化
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  // xが属する集合の根を返す
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  // xとyが同じ集合に属しているかを判定
  bool issame(int x, int y) { return root(x) == root(y); }

  // xが属する集合とyが属する集合をマージ
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // 計算量が減るよ
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  // xが属する集合のサイズ
  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    uf.merge(a, b);
  }
  int ans = -1;
  for (int i = 1; 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>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

struct UF {
  vector<int> data;
  UF(int n) { data.assign(n, -1); }
  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y];
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UF data(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data.merge(a, b);
  }
  rep(i, n) chmax(ans, data.size(i));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

struct UF {
  vector<int> data;
  UF(int n) { data.assign(n, -1); }
  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UF data(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data.merge(a, b);
  }
  rep(i, n) chmax(ans, data.size(i));
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 100010
const int MOD = 1000000007;

int visited[maxn];
set<int> g[maxn];

int dfs(int u) {
  int cur = 1;
  visited[u] = true;
  for (auto x : g[u]) {
    if (!visited[x]) {
      cur += dfs(x);
    }
  }
  return cur;
}

void solve() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;

    g[a].insert(b);
    g[b].insert(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 200010
const int MOD = 1000000007;

int visited[maxn];
set<int> g[maxn];

int dfs(int u) {
  int cur = 1;
  visited[u] = true;
  for (auto x : g[u]) {
    if (!visited[x]) {
      cur += dfs(x);
    }
  }
  return cur;
}

void solve() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;

    g[a].insert(b);
    g[b].insert(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}
Problem p02573 (C++)

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

#define ll long long
#define ar array

const int mxN = 1e5, INF = 999999999;
const int modr = 1e9 + 7;
int n, m, a[mxN];

/* 1. If you can't keep solution in mind, then visualize it on a paper.
 * 2. Try to identify the type of problem - DP, Greedy, Graph, Constructive,
 * 	  Prefix/Suffix, Difference Array, BIT/Segment Tree, etc.
 * 3. Look at the problems from different perspectives - reverse the problem,
 * 	  add instead of delete (maybe?)
 * 4. Divide difficult solutions into cases and/or sequences.
 * 5. Use math rules to solve Nested Summation.
 * 6. Optimisation problems are generally solved with DP, greedy, binary search
 * or brute-force.
 * ------------------------------------------------------------------
 */

bool vis[mxN];
vector<int> adj[mxN];
int t;
void dfs(int s) {
  vis[s] = true;
  t++;
  for (auto child : adj[s]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

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

  cout << maxm;
}

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

  int t = 1;

  for (int i = 1; i <= t; ++i) {
    //	cout << "Case #" << i << ": ";
    solve();
  }
}


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

#define ll long long
#define ar array

const int mxN = 2e5 + 5, INF = 999999999;
const int modr = 1e9 + 7;
int n, m, a[mxN];

/* 1. If you can't keep solution in mind, then visualize it on a paper.
 * 2. Try to identify the type of problem - DP, Greedy, Graph, Constructive,
 * 	  Prefix/Suffix, Difference Array, BIT/Segment Tree, etc.
 * 3. Look at the problems from different perspectives - reverse the problem,
 * 	  add instead of delete (maybe?)
 * 4. Divide difficult solutions into cases and/or sequences.
 * 5. Use math rules to solve Nested Summation.
 * 6. Optimisation problems are generally solved with DP, greedy, binary search
 * or brute-force.
 * ------------------------------------------------------------------
 */

bool vis[mxN];
vector<int> adj[mxN];
int t;
void dfs(int s) {
  vis[s] = true;
  t++;
  for (auto child : adj[s]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

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

  cout << maxm;
}

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

  int t = 1;

  for (int i = 1; i <= t; ++i) {
    //	cout << "Case #" << i << ": ";
    solve();
  }
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[100001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}
int main() {
  int a, b, ans = 0;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a = find(a);
    b = find(b);
    if (a != b) {
      merge(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].parent == i) {
      ans = max(ans, g[i].node);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[300001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}
int main() {
  int a, b, ans = 0;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a = find(a);
    b = find(b);
    if (a != b) {
      merge(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].parent == i) {
      ans = max(ans, g[i].node);
    }
  }
  cout << ans << endl;
}
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> Parent;
  vector<int> Size;

  UnionFind(int N) : Parent(N), Size(N, 1) {
    for (int i = 0; i < N; i++)
      Parent[i] = i;
  }

  int root(int A) {
    if (Parent[A] == A)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  bool merge(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (Size[A] < Size[B])
      swap(A, B);

    Size[A] += Size[B];
    Parent[B] = A;
    return true;
  }

  bool issame(int A, int B) { //
    return root(A) == root(B);
  }

  int findsize(int A) { return Size[root(A)]; }
};

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

  UnionFind Tree(n);
  vector<int> a(n), b(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Tree.merge(a[i], b[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int s = Tree.findsize(i);
    ans = max(ans, s);
  }

  cout << ans << '\n';
}


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> Parent;
  vector<int> Size;

  UnionFind(int N) : Parent(N), Size(N, 1) {
    for (int i = 0; i < N; i++)
      Parent[i] = i;
  }

  int root(int A) {
    if (Parent[A] == A)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  bool merge(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (Size[A] < Size[B])
      swap(A, B);

    Size[A] += Size[B];
    Parent[B] = A;
    return true;
  }

  bool issame(int A, int B) { //
    return root(A) == root(B);
  }

  int findsize(int A) { return Size[root(A)]; }
};

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

  UnionFind Tree(n);
  vector<int> a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Tree.merge(a[i], b[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int s = Tree.findsize(i);
    ans = max(ans, s);
  }

  cout << ans << '\n';
}
Problem p02573 (C++)

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

using namespace std;

int getParent(vector<int> &parent, int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = getParent(parent, parent[x]);
}

void uniteParent(vector<int> &parent, int a, int b) {
  a = getParent(parent, a);
  b = getParent(parent, b);
  if (a == b)
    return;
  if (a > b)
    swap(a, b);
  // 항상 a < b
  parent[a] += parent[b];
  parent[b] = a;
}

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

  vector<int> parent(n + 1, -1);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    uniteParent(parent, a, b);
  }

  int cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    cnt = max(cnt, -parent[parent[i]]);
  }
  cout << cnt;

  return 0;
}

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

using namespace std;

int getParent(vector<int> &parent, int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = getParent(parent, parent[x]);
}

void uniteParent(vector<int> &parent, int a, int b) {
  a = getParent(parent, a);
  b = getParent(parent, b);
  if (a == b)
    return;
  if (a > b)
    swap(a, b);
  // 항상 a < b
  parent[a] += parent[b];
  parent[b] = a;
}

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

  vector<int> parent(n + 1, -1);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    uniteParent(parent, a, b);
  }

  int cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    cnt = max(cnt, -parent[i]);
  }
  cout << cnt;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000000];
bool vis[100000];
int cnt;

void dfs(int node) {
  if (vis[node] == 1)
    return;
  vis[node] = 1;
  cnt++;

  for (auto c : g[node])
    dfs(c);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {

      cnt = 0;
      dfs(i);
      mx = max(cnt, mx);
    }
  }
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000079];
bool vis[1000009];
int cnt;

void dfs(int node) {
  if (vis[node] == 1)
    return;
  vis[node] = 1;
  cnt++;

  for (auto c : g[node])
    dfs(c);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {

      cnt = 0;
      dfs(i);
      mx = max(cnt, mx);
    }
  }
  cout << mx << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int, int>
#define endl "\n"
#define MAXN 100005
using namespace std;

int n, m, a, b, vis[MAXN], cnt;
vector<int> adj[MAXN];
set<pi> s;

void dfs(int s) {
  vis[s] = 1;
  cnt++;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]]) {
      dfs(adj[s][i]);
    }
  }
}

void solve() {
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    s.insert(mp(a, b));
  }
  for (auto &it : s) {
    int l = it.ff, r = it.ss;
    // cout << l << " " << r << endl;
    adj[l].pb(r);
    adj[r].pb(l);
  }
  int maxx = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      maxx = max(maxx, cnt);
    }
  }
  cout << maxx << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int, int>
#define endl "\n"
#define MAXN 200005
using namespace std;

int n, m, a, b, vis[MAXN], cnt;
vector<int> adj[MAXN];
set<pi> s;

void dfs(int s) {
  vis[s] = 1;
  cnt++;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]]) {
      dfs(adj[s][i]);
    }
  }
}

void solve() {
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    s.insert(mp(a, b));
  }
  for (auto &it : s) {
    int l = it.ff, r = it.ss;
    // cout << l << " " << r << endl;
    adj[l].pb(r);
    adj[r].pb(l);
  }
  int maxx = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      maxx = max(maxx, cnt);
    }
  }
  cout << maxx << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

struct unionfind {
  vector<long long> d;

  unionfind(long long n) : d(n, -1) {}

  long long root(long long x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = root(d[x]);
    }
  }

  bool unite(long long x, long long y) {
    x = root(x);
    y = root(y);

    if (x == y) {
      return false;
    }

    if (d[x] > d[y]) {
      swap(x, y);
    }

    d[x] += d[y];
    d[y] = x;
    return true;
  }

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

  long long size(long long x) { return -d[root(x)]; }
};

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

  unionfind u(n);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;

    if (!u.same(a, b)) {
      u.unite(a, b);
    }
  }

  ll ans = 0;

  if (m == 0) {
    cout << 1 << endl;
  } else {
    for (int i = 0; i < n; i++) {
      ans = max(u.size(i), ans);
    }

    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

struct unionfind {
  vector<long long> d;

  unionfind(long long n) : d(n, -1) {}

  long long root(long long x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = root(d[x]);
    }
  }

  bool unite(long long x, long long y) {
    x = root(x);
    y = root(y);

    if (x == y) {
      return false;
    }

    if (d[x] > d[y]) {
      swap(x, y);
    }

    d[x] += d[y];
    d[y] = x;
    return true;
  }

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

  long long size(long long x) { return -d[root(x)]; }
};

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

  unionfind u(n);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    if (!u.same(a, b)) {
      u.unite(a, b);
    }
  }

  ll ans = 0;

  if (m == 0) {
    cout << 1 << endl;
  } else {
    for (int i = 0; i < n; i++) {
      ans = max(u.size(i), ans);
    }

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

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;

int n, m, p[200005], ans = 1;

int pa(int x) {
  if (p[x] < 0)
    return x;
  return pa(p[x]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  memset(p, -1, sizeof(p));
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    int x = pa(a), y = pa(b);
    if (x != y) {
      p[x] += p[y];
      p[y] = x;
      ans = max(ans, -p[x]);
    }
  }
  cout << ans << "\n";
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;

int n, m, p[200005], ans = 1;

int pa(int x) {
  if (p[x] < 0)
    return x;
  return p[x] = pa(p[x]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  memset(p, -1, sizeof(p));
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    int x = pa(a), y = pa(b);
    if (x != y) {
      p[x] += p[y];
      p[y] = x;
      ans = max(ans, -p[x]);
    }
  }
  cout << ans << "\n";
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
vector<int> adj[200005];
int cnt;
vector<int> vis(200005, 0);
int dfs(int node) {
  if (!vis[node]) {
    ++cnt;
    vis[node] = 1;
    for (int son : adj[node])
      dfs(son);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
vector<int> adj[200005];
int cnt;
vector<int> vis(200005, 0);
void dfs(int node) {
  if (!vis[node]) {
    ++cnt;
    vis[node] = 1;
    for (int son : adj[node])
      dfs(son);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, z, x, y, fa[N];
inline int find(int x) {
  while (x != fa[x])
    x = fa[x] = fa[fa[x]];
  return x;
}
int num[N];
int ans;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  while (m--) {
    cin >> x >> y;
    int a = find(x), b = find(y);
    fa[a] = b;
  }
  for (int i = 1; i <= n; i++)
    num[find(i)]++;
  for (int i = 1; i <= n; i++)
    ans = max(ans, num[i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, z, x, y, fa[N];
inline int find(int x) {
  while (x != fa[x])
    x = fa[x] = fa[fa[x]];
  return x;
}
int num[N];
int ans;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  while (m--) {
    cin >> x >> y;
    int a = find(x), b = find(y);
    fa[a] = b;
  }
  for (int i = 1; i <= n; i++)
    num[find(i)]++;
  for (int i = 1; i <= n; i++)
    ans = max(ans, num[i]);
  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 100005
const ll INF = 1000000001;
const ll MOD = 1000000007;

vector<vector<ll>> g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
ll vis[N] = {0}, par[N] = {0}, child[N] = {0}, cnt = 0;
map<pair<ll, ll>, ll> ump;

void dfs(ll v) {
  vis[v] = 1;
  cnt++;
  for (auto u : g[v])
    if (!vis[u])
      dfs(u);
}

void solve() {
  ll n, m;
  cin >> n >> m;
  f(1, n) g[i].clear();
  f(0, m - 1) {
    ll x, y;
    cin >> x >> y;
    if (ump[{x, y}] != 0)
      continue;
    g[x].pb(y);
    g[y].pb(x);
    ump[{x, y}] += 1;
    ump[{y, x}] += 1;
  }
  ll ans = 0;
  f(1, n) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  //}
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 200005
const ll INF = 1000000001;
const ll MOD = 1000000007;

vector<vector<ll>> g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
ll vis[N] = {0}, par[N] = {0}, child[N] = {0}, cnt = 0;
map<pair<ll, ll>, ll> ump;

void dfs(ll v) {
  vis[v] = 1;
  cnt++;
  for (auto u : g[v])
    if (!vis[u])
      dfs(u);
}

void solve() {
  ll n, m;
  cin >> n >> m;
  f(1, n) g[i].clear();
  f(0, m - 1) {
    ll x, y;
    cin >> x >> y;
    if (ump[{x, y}] != 0)
      continue;
    g[x].pb(y);
    g[y].pb(x);
    ump[{x, y}] += 1;
    ump[{y, x}] += 1;
  }
  ll ans = 0;
  f(1, n) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  //}
  return 0;
}