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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
const ll mod = 1e9 + 7;
const int N = 1e5 + 205;
vector<int> v[N];
bool vis[N];
int sz;
int ans;
void dfs(int x) {
  vis[x] = true;
  sz++;
  ans = max(ans, sz);
  for (int y : v[x]) {
    if (!vis[y])
      dfs(y);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sz = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
const ll mod = 1e9 + 7;
const int N = 2e5 + 205;
vector<int> v[N];
bool vis[N];
int sz;
int ans;
void dfs(int x) {
  vis[x] = true;
  sz++;
  ans = max(ans, sz);
  for (int y : v[x]) {
    if (!vis[y])
      dfs(y);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sz = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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 int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
ll n, m, k = 0;
vi adj[100001];
bool vis[100001] = {false};

void dfs(int x) {
  vis[x] = true;
  k++;
  for (auto ip = adj[x].begin(); ip != adj[x].end(); ip++) {
    if (!vis[*ip]) {
      dfs(*ip);
    }
  }
}
void solve() {
  cin >> n >> m;
  ll ans = 0;
  fo(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  fo(i, 0, n + 1) {
    if (!vis[i]) {
      int temp = 0;
      k = 0;
      dfs(i);
    }
    ans = max(ans, k);
  }
  cout << ans;
  cout << "\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
ll n, m, k = 0;
vi adj[200001];
bool vis[200001] = {false};

void dfs(int x) {
  vis[x] = true;
  k++;
  for (auto ip = adj[x].begin(); ip != adj[x].end(); ip++) {
    if (!vis[*ip]) {
      dfs(*ip);
    }
  }
}
void solve() {
  cin >> n >> m;
  ll ans = 0;
  fo(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  fo(i, 0, n + 1) {
    if (!vis[i]) {
      int temp = 0;
      k = 0;
      dfs(i);
    }
    ans = max(ans, k);
  }
  cout << ans;
  cout << "\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
set<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 mx = 0;
  for (int i = 1; i <= n; ++i) {
    mx = max(ans[i], mx);
  }
  cout << mx << endl;
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  int m = K;
  while (m--) {
    int u, v;
    cin >> u >> v;
    graph[u].insert(v);
    graph[v].insert(u);
  }
  MaximumVisit(N);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxx = 200010;
set<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 mx = 0;
  for (int i = 1; i <= n; ++i) {
    mx = max(ans[i], mx);
  }
  cout << mx << endl;
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  int m = K;
  while (m--) {
    int u, v;
    cin >> u >> v;
    graph[u].insert(v);
    graph[v].insert(u);
  }
  MaximumVisit(N);
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int fin = 0, ans = 0;
bool vis[200050];
vector<int> v[200050];
int dfs(int x) {
  if (vis[x])
    return 0;
  vis[x] = true;
  ans++;
  for (auto it : v[x])
    if (!vis[it])
      dfs(it);
}
int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans = 0;
      dfs(i);
    }
    fin = max(fin, ans);
  }
  cout << fin;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int fin = 0, ans = 0;
bool vis[200050];
vector<int> v[200050];
int dfs(int x) {
  if (vis[x])
    return 0;
  vis[x] = true;
  ans++;
  for (auto it : v[x])
    if (!vis[it])
      dfs(it);
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans = 0;
      dfs(i);
    }
    fin = max(fin, ans);
  }
  cout << fin;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

struct Unionfind {
  vector<int> d;
  Unionfind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0) {
      return x;
    }
    return d[x] = root(d[x]);
  }
  bool unite(int x, int 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;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[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 MAX = 0;
  for (int i = 0; i < N; i++) {
    chmax(MAX, uf.size(i));
  }
  cout << MAX << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

struct Unionfind {
  vector<int> d;
  Unionfind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0) {
      return x;
    }
    return d[x] = root(d[x]);
  }
  bool unite(int x, int 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(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[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 MAX = 0;
  for (int i = 0; i < N; i++) {
    chmax(MAX, uf.size(i));
  }
  cout << MAX << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

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

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

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

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

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

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

  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }

  int output = 0;
  for (int i = 0; i < N; i++) {
    if (tree.size(i + 1) > output) {
      output = tree.size(i + 1);
    }
  }
  cout << output << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

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

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

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

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

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

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

  UnionFind tree(N + 1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }

  int output = 0;
  for (int i = 0; i < N; i++) {
    if (tree.size(i + 1) > output) {
      output = tree.size(i + 1);
    }
  }
  cout << output << endl;

  return 0;
}
Problem p02573 (C++)

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

using namespace std;

#define ll long long
#define F first
#define S second
#define ii pair<int, int>

const int N = 1e5;
int cnt[N];
vector<int> adj[N];
int n, m;
bool visited[N];
void dfs(int u) {

  if (visited[u])
    return;

  visited[u] = 1;
  int cur = 1;
  for (auto x : adj[u]) {
    if (!visited[x]) {
      dfs(x);
      cur += cnt[x];
    }
  }
  cnt[u] = cur;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[y].push_back(x);
    adj[x].push_back(y);
  }
  int ans = -1e9;
  for (int i = 1; i <= n; i++) {
    dfs(i);
    ans = max(ans, cnt[i]);
  }
  cout << ans;
}


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

using namespace std;

#define ll long long
#define F first
#define S second
#define ii pair<int, int>

const int N = 7e5 + 5;
int cnt[N];
vector<int> adj[N];
int n, m;
bool visited[N];
void dfs(int u) {

  if (visited[u])
    return;

  visited[u] = 1;
  int cur = 1;
  for (auto x : adj[u]) {
    if (!visited[x]) {
      dfs(x);
      cur += cnt[x];
    }
  }
  cnt[u] = cur;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[y].push_back(x);
    adj[x].push_back(y);
  }
  int ans = -1e9;
  for (int i = 1; i <= n; i++) {
    dfs(i);
    ans = max(ans, cnt[i]);
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
using namespace std;
#include <bits/stdc++.h>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define debug(x) cout << #x << " = " << x << endl
vector<int> adj[100001];
bool visited[100001];
int val;
void dfs(int p) {
  visited[p] = true;
  val++;
  for (auto it = adj[p].begin(); it < adj[p].end(); it++) {
    if (!visited[*it]) {
      dfs(*it);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    unique(adj[i].begin(), adj[i].end());
  }
  // now run the dfs
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      ans = max(ans, val);
      val = 0;
    }
  }
  cout << ans << endl;
}
int main() {
  FASTIO
  long long int t = 1;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
using namespace std;
#include <bits/stdc++.h>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define debug(x) cout << #x << " = " << x << endl
vector<int> adj[200001];
bool visited[200001];
int val;
void dfs(int p) {
  visited[p] = true;
  val++;
  for (auto it = adj[p].begin(); it < adj[p].end(); it++) {
    if (!visited[*it]) {
      dfs(*it);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    unique(adj[i].begin(), adj[i].end());
  }
  // now run the dfs
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      ans = max(ans, val);
      val = 0;
    }
  }
  cout << ans << endl;
}
int main() {
  FASTIO
  long long int t = 1;
  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 f first
#define s second
#define inf 1e15

#define N 300005

string s;
ll n, m, x, y, d, k;
ll a[N];

ll parent[N];
ll siz[N];

int find(int g) {

  if (parent[g] != g) {
    parent[g] = find(parent[g]);
  }
  return g;
}
int main() {

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

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      parent[i] = i;
      siz[i] = 1;
    }

    for (int i = 0; i < m; i++) {

      int x, y;
      ;
      cin >> x >> y;
      x--;
      y--;

      x = find(x);
      y = find(y);

      if (x == y)
        continue;

      parent[x] = y;
      siz[y] += siz[x];
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, siz[find(i)]);
    }

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


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

#define ll long long
#define f first
#define s second
#define inf 1e15

#define N 300005

string s;
ll n, m, x, y, d, k;
ll a[N];

ll parent[N];
ll siz[N];

int find(int g) {

  if (parent[g] != g) {
    parent[g] = find(parent[g]);
  }
  return parent[g];
}
int main() {

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

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      parent[i] = i;
      siz[i] = 1;
    }

    for (int i = 0; i < m; i++) {

      int x, y;
      ;
      cin >> x >> y;
      x--;
      y--;

      x = find(x);
      y = find(y);

      if (x == y)
        continue;

      parent[x] = y;
      siz[y] += siz[x];
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, siz[find(i)]);
    }

    cout << ans << "\n";
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
bool visited[1000000] = {0};
ll ans;
ll c;
void dfs(vector<vector<ll>> ans, ll n, ll pos) {
  if (visited[pos])
    return;
  visited[pos] = 1;
  c++;
  for (auto i = ans[pos].begin(); i != ans[pos].end(); i++) {
    if (ans[pos].size() == 0)
      return;
    if (visited[*i])
      continue;
    dfs(ans, n, *i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return 0;
  }
  vector<vector<ll>> adj(n + 1);
  ll a, b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    dfs(adj, n, i);
    ans = max(ans, c);
    c = 0;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
bool visited[1000000] = {0};
ll ans;
ll c;
void dfs(vector<vector<ll>> &ans, ll n, ll pos) {
  if (visited[pos])
    return;
  visited[pos] = 1;
  c++;
  for (auto i = ans[pos].begin(); i != ans[pos].end(); i++) {
    if (ans[pos].size() == 0)
      return;
    if (visited[*i])
      continue;
    dfs(ans, n, *i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return 0;
  }
  vector<vector<ll>> adj(n + 1);
  ll a, b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    dfs(adj, n, i);
    ans = max(ans, c);
    c = 0;
  }
  cout << ans;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int sz[M], fa[M], n, m;
int find(int x) { return fa[x] == x ? x : find(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    sz[i] = 1, fa[i] = i;
  for (int i = 0, x, y; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = find(x), y = find(y);
    if (x != y)
      fa[y] = x, sz[x] += sz[y];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, sz[i]);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int sz[M], fa[M], n, m;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    sz[i] = 1, fa[i] = i;
  for (int i = 0, x, y; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = find(x), y = find(y);
    if (x != y)
      fa[y] = x, sz[x] += sz[y];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, sz[i]);
  printf("%d\n", ans);
  return 0;
}
Problem p02573 (C++)

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

#define ll long long
// #define endl '\n'

void max_self(int &a, int b) { a = max(a, b); }
// void min_self(int &a, int b){a = min(a, b);}

const int nax = 1e5 + 5;
vector<int> G[nax], vis(nax);
int cnt;

void dfs(int node) {
  vis[node] = 1;
  cnt++;
  for (int i : G[node]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      max_self(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


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

#define ll long long
// #define endl '\n'

void max_self(int &a, int b) { a = max(a, b); }
// void min_self(int &a, int b){a = min(a, b);}

const int nax = 2e5 + 5;
vector<int> G[nax], vis(nax);
int cnt;

void dfs(int node) {
  vis[node] = 1;
  cnt++;
  for (int i : G[node]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      max_self(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\n'
#define dc " "
#define Truong_Thanh_Minh                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef pair<ll, ll> ii;
const ll maxn = 1e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, m, x, y, ok[maxn], ngroup, cnt[maxn], group[maxn], res;
vector<ll> a[maxn];
void dfs(int i) {
  ok[i] = 1;
  group[i] = ngroup;
  ++cnt[ngroup];
  for (int j : a[i])
    if (!ok[j])
      dfs(j);
}
int main() {
  Truong_Thanh_Minh
          // freopen(name".inp","r",stdin);
          // freopen(name".out","w",stdout);
          cin >>
      n >> m;
  rep(i, 1, m) {
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }
  rep(i, 1, n) {
    if (!ok[i]) {
      ++ngroup;
      dfs(i);
    }
  }
  rep(i, 1, ngroup) res = max(res, cnt[i]);
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\n'
#define dc " "
#define Truong_Thanh_Minh                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef pair<ll, ll> ii;
const ll maxn = 2e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, m, x, y, ok[maxn], ngroup, cnt[maxn], group[maxn], res;
vector<ll> a[maxn];
void dfs(int i) {
  ok[i] = 1;
  group[i] = ngroup;
  ++cnt[ngroup];
  for (int j : a[i])
    if (!ok[j])
      dfs(j);
}
int main() {
  Truong_Thanh_Minh
          // freopen(name".inp","r",stdin);
          // freopen(name".out","w",stdout);
          cin >>
      n >> m;
  rep(i, 1, m) {
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }
  rep(i, 1, n) {
    if (!ok[i]) {
      ++ngroup;
      dfs(i);
    }
  }
  rep(i, 1, ngroup) res = max(res, cnt[i]);
  cout << res;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

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) {
    if (x == y)
      return false;
    if (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};

int main(void) {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b);
  }
  int res = 0;
  rep(i, N) { chmax(res, uf.size(i)); }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

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 (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};

int main(void) {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b);
  }
  int res = 0;
  rep(i, N) { chmax(res, uf.size(i)); }
  cout << res << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

const int sz = 1e5 + 1;
int p[sz], w[sz];

int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void uni(int a, int b) {
  if (a == b)
    return;
  a < b ? p[b] = a : p[a] = b;
  w[a] = w[b] += w[a];
}

int main() {
  int N, M, u, v;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++)
    p[i] = i, w[i] = 1;

  while (M--) {
    scanf("%d %d", &u, &v);
    uni(get(u), get(v));
  }

  int res = 0;
  for (int i = 1; i <= N; i++)
    res = max(res, w[i]);
  printf("%d\n", res);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;

const int sz = 2e5 + 1;
int p[sz], w[sz];

int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void uni(int a, int b) {
  if (a == b)
    return;
  a < b ? p[b] = a : p[a] = b;
  w[a] = w[b] += w[a];
}

int main() {
  int N, M, u, v;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++)
    p[i] = i, w[i] = 1;

  while (M--) {
    scanf("%d %d", &u, &v);
    uni(get(u), get(v));
  }

  int res = 0;
  for (int i = 1; i <= N; i++)
    res = max(res, w[i]);
  printf("%d\n", res);
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
template <class T> void setmax(T &a, T const &b) {
  if (a < b)
    a = b;
}

template <class T> void setmin(T &a, T const &b) {
  if (a > b)
    a = b;
}

template <typename T, typename X> auto vectors(T a, X x) {
  return vector<T>(x, a);
}

template <typename T, typename X, typename Y, typename... Zs>
auto vectors(T a, X x, Y y, Zs... zs) {
  auto cont = vectors(a, y, zs...);
  return vector<decltype(cont)>(x, cont);
}

// 連結している頂点の集合について、それらは必ず別々に別れなければならない。

int main() {
  int N, M;
  cin >> N >> M;
  auto G = vectors(0, N, 0);
  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);
  }

  vector<bool> visited(N, false);
  stack<int> stk;
  for (int i = 0; i < N; ++i) {
    stk.push(i);
  }
  auto dfs = [&](auto f, int cur, int &num) -> int {
    visited[cur] = true;
    ++num;
    for (int to : G[cur]) {
      if (!visited[to])
        f(f, to, num);
    }
  };
  int res = 0;
  while (!stk.empty()) {
    int vertex = stk.top();
    stk.pop();
    if (!visited[vertex]) {
      int tmp = 0;
      dfs(dfs, vertex, tmp);
      setmax(res, tmp);
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
template <class T> void setmax(T &a, T const &b) {
  if (a < b)
    a = b;
}

template <class T> void setmin(T &a, T const &b) {
  if (a > b)
    a = b;
}

template <typename T, typename X> auto vectors(T a, X x) {
  return vector<T>(x, a);
}

template <typename T, typename X, typename Y, typename... Zs>
auto vectors(T a, X x, Y y, Zs... zs) {
  auto cont = vectors(a, y, zs...);
  return vector<decltype(cont)>(x, cont);
}

// 連結している頂点の集合について、それらは必ず別々に別れなければならない。

int main() {
  int N, M;
  cin >> N >> M;
  auto G = vectors(0, N, 0);
  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);
  }

  vector<bool> visited(N, false);
  stack<int> stk;
  for (int i = 0; i < N; ++i) {
    stk.push(i);
  }
  auto dfs = [&](auto f, int cur, int &num) -> void {
    visited[cur] = true;
    ++num;
    for (int to : G[cur]) {
      if (!visited[to])
        f(f, to, num);
    }
  };
  int res = 0;
  while (!stk.empty()) {
    int vertex = stk.top();
    stk.pop();
    if (!visited[vertex]) {
      int tmp = 0;
      dfs(dfs, vertex, tmp);
      setmax(res, tmp);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INIT                                                                   \
  ios_base ::sync_with_stdio(false);                                           \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;
vector<int> g[100010];
bool v[100010];
int dfs(int s) {
  int res = 1;
  v[s] = true;
  for (int i : g[s]) {
    if (!v[i]) {
      v[i] = true;
      res += dfs(i);
    }
  }
  return res;
}

int cnctd() {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      res = max(res, dfs(i));
    }
  }
  return res;
}

int32_t main() {
  INIT cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  int gr = cnctd();
  cout << gr;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INIT                                                                   \
  ios_base ::sync_with_stdio(false);                                           \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;
vector<int> g[200010];
bool v[200010];
int dfs(int s) {
  int res = 1;
  v[s] = true;
  for (int i : g[s]) {
    if (!v[i]) {
      v[i] = true;
      res += dfs(i);
    }
  }
  return res;
}

int cnctd() {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      res = max(res, dfs(i));
    }
  }
  return res;
}

int32_t main() {
  INIT cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  int gr = cnctd();
  cout << gr;
  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  vector<int> r;

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

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x > y)
      swap(x, y);
    if (x == y)
      return false;
    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;
    UF.unite(--a, --b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, UF.size(i));
  }
  cout << ans;
}


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

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)
      swap(x, y);
    if (x == y)
      return false;
    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;
    UF.unite(--a, --b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, UF.size(i));
  }
  cout << ans;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

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

const int SIZE = 220;

struct UnionFind {
  vector<int> par;

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

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

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

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

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

signed main() {

  int N, M;

  int ans = 0;

  cin >> N >> M;

  int A[SIZE], B[SIZE];

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

  UnionFind tree(N);

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

  ans = tree.maxtree(N);

  cout << ans << endl;

  cin >> ans;

  return 0;
}


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

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

const int SIZE = 200020;

struct UnionFind {
  vector<int> par;

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

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

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

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

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

signed main() {

  int N, M;

  int ans = 0;

  cin >> N >> M;

  int A[SIZE], B[SIZE];

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

  UnionFind tree(N);

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

  ans = tree.maxtree(N);

  cout << ans << endl;

  cin >> ans;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()

int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

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

int main() {

  int n, m;
  cin >> n >> m;
  UnionFind uf(n);

  int ans = 0;

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()

int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

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

int main() {

  int n, m;
  cin >> n >> m;
  UnionFind uf(n + 1);

  int ans = 0;

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

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

Buggy submission (status: Runtime Error):
// AUTHOR: ROSHAN SRIVASTAVA
// WEBSITE: https://www.youtube.com/channel/UC6uQdd7kLLOdlHSVklhV7Cw

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

vector<int> G[100009];
bool vis[100009];

int solve(int x, int par) {
  vis[x] = true;
  int ans = 1;
  for (auto n : G[x]) {
    if (n == par)
      continue;
    if (vis[n])
      continue;
    ans += solve(n, x);
  }
  return ans;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  memset(vis, false, sizeof vis);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int ma = -1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ma = max(ma, solve(i, -1));
    }
  }
  cout << ma << endl;

  return 0;
}

Fixed submission:
// AUTHOR: ROSHAN SRIVASTAVA
// WEBSITE: https://www.youtube.com/channel/UC6uQdd7kLLOdlHSVklhV7Cw

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

vector<int> G[200009];
bool vis[200009];

int solve(int x, int par) {
  vis[x] = true;
  int ans = 1;
  for (auto n : G[x]) {
    if (n == par)
      continue;
    if (vis[n])
      continue;
    ans += solve(n, x);
  }
  return ans;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  memset(vis, false, sizeof vis);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int ma = -1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ma = max(ma, solve(i, -1));
    }
  }
  cout << ma << endl;

  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 2000015
const ll MOD = 1000000000 + 7;

int parent[MAX], sz[MAX];

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

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

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
      make_set(i);

    for (int i = 0; i <= k; i++) {
      int a, b;
      cin >> a >> b;
      union_sets(a, b);
    }
    int mx = -1;
    for (int i = 1; i <= n; i++)
      mx = max(mx, sz[i]);

    cout << mx << endl;
  }
}


Fixed submission:


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 2000015
const ll MOD = 1000000000 + 7;

int parent[MAX], sz[MAX];

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

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

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
      make_set(i);

    for (int i = 1; i <= k; i++) {
      int a, b;
      cin >> a >> b;
      union_sets(a, b);
    }
    int mx = -1;
    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>
using namespace std;
typedef long long int ll;

map<ll, ll> mp;
vector<ll> v[100];
ll dfs(ll node) {

  if (mp[node] == 0) {
    mp[node]++;
    ll i, l = v[node].size(), s = 0;
    for (i = 0; i < l; i++) {
      s += dfs(v[node][i]);
    }
    return s + 1;
  } else {
    return 0;
  }
}
int main() {
  ll i, j, t = 1;
  // cin>>t;
  for (i = 0; i < t; i++) {
    ll n, m, x, y, mini = 0;
    cin >> n >> m;
    for (j = 0; j < m; j++) {
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    for (j = 1; j <= n; j++) {
      if (mp[j] == 0) {
        mini = max(mini, dfs(j));
      }
    }
    cout << mini;
  }
}

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

map<ll, ll> mp;
vector<ll> v[1000000];
ll dfs(ll node) {

  if (mp[node] == 0) {
    mp[node]++;
    ll i, l = v[node].size(), s = 0;
    for (i = 0; i < l; i++) {
      s += dfs(v[node][i]);
    }
    return s + 1;
  } else {
    return 0;
  }
}
int main() {
  ll i, j, t = 1;
  // cin>>t;
  for (i = 0; i < t; i++) {
    ll n, m, x, y, mini = 0;
    cin >> n >> m;
    for (j = 0; j < m; j++) {
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    for (j = 1; j <= n; j++) {
      if (mp[j] == 0) {
        mini = max(mini, dfs(j));
      }
    }
    cout << mini;
  }
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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;
  }

  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;
    UF.unite(a - 1, b - 1);
  }

  // for(int person : friends[1])
  //     cout<<person<<' ';
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, UF.size(i));

  cout << ans << endl;

  return 0;
}


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

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;
    UF.unite(a - 1, b - 1);
  }

  // for(int person : friends[1])
  //     cout<<person<<' ';
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, UF.size(i));

  cout << ans << endl;

  return 0;
}