alexjercan/bugnet huggingface dataset:
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

  cout << ans << endl;
}

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


Error: 0

Fixed submission:

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

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

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

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

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

  cout << ans << endl;
}

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

typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define MOD 1000000007
#define M_PI 3.14159265358979323846 /* pi */
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define entire(a) (a).begin(), (a).end()
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
std::vector<int> g[200005];
int vis[200005];
int n, m;
int dfs(int node) {
  // cout<<node<<endl;
  vis[node] = 1;

  int cnt = 1;
  for (auto el : g[node]) {
    if (!vis[el])
      cnt += dfs(el);
  }
  return cnt;
}
void solve() {
  cin >> n >> m;
  std::map<pii, bool> map;
  memset(vis, 0, sizeof(vis));
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    if (map[{u, v}])
      continue;
    map[{u, v}] = map[{v, u}] = true;
    g[u].pb(v);
    g[v].pb(u);
  }

  int res = 1;
  // for(auto el :g[1])cout<<el<<" ";
  // 	cout<<endl;
  rep(i, 200005) {
    if (!vis[i] && !g[i].empty())
      res = max(res, dfs(i));
  }

  cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define MOD 1000000007
#define M_PI 3.14159265358979323846 /* pi */
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define entire(a) (a).begin(), (a).end()
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
std::vector<int> g[100005];
int vis[100005];
int n, m;
int dfs(int node) {
  // cout<<node<<endl;
  vis[node] = 1;

  int cnt = 1;
  for (auto el : g[node]) {
    if (!vis[el])
      cnt += dfs(el);
  }
  return cnt;
}
void solve() {
  cin >> n >> m;
  std::map<pii, bool> map;
  memset(vis, 0, sizeof(vis));
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    if (map[{u, v}])
      continue;
    map[{u, v}] = map[{v, u}] = true;
    g[u].pb(v);
    g[v].pb(u);
  }

  int res = -1;
  // for(auto el :g[1])cout<<el<<" ";
  // 	cout<<endl;
  rep(i, 100005) {
    if (!vis[i] && !g[i].empty())
      res = max(res, dfs(i));
  }

  cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1e9 + 7;

int N, M;
struct parent {
  int p;
  int cnt;
};
vector<parent> par;

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

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x != y) {
    par[y].cnt += par[x].cnt;
    par[x].p = y;
  }
}

int main() {
  int a, b;
  cin >> N >> M;
  par.resize(N);
  for (int i = 0; i < N; i++) {
    par[i].p = i;
    par[i].cnt = 1;
  }
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    a--, b--;
    unite(a, b);
  }
  int cnt_max = 0;
  for (int i = 0; i < N; i++) {
    cnt_max = max(cnt_max, par[i].cnt);
  }
  cout << cnt_max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1e9 + 7;

int count(int num, vector<vector<int>> friends, vector<bool> &checked) {
  int ret = 1;
  for (int i = 0; i < (int)friends.at(num).size(); i++) {
    if (checked[friends.at(num).at(i)] == true) {
      continue;
    } else {
      checked[friends.at(num).at(i)] = true;
      ret += count(friends.at(num).at(i), friends, checked);
    }
  }
  return ret;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> friends(n + 1, vector<int>(0));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (a < b)
      friends.at(a).push_back(b);
    else
      friends.at(b).push_back(a);
  }
  vector<bool> cheched(n + 1, false);
  int max_num = 0;
  for (int i = 1; i <= n; i++) {
    if (cheched[i] == false) {
      cheched[i] = true;
      max_num = max(max_num, count(i, friends, cheched));
    }
  }
  cout << max_num << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;

int find(vi &parent, int i) {
  if (parent[i] == i)
    return i;
  parent[i] = find(parent, parent[i]);
  return parent[i];
}

void Union(vi &parent, int x, int y) {
  int xset = find(parent, x);
  int yset = find(parent, y);
  if (xset != yset) {
    parent[xset] = yset;
  }
}

int main() {
  int N, M;
  int x, y;
  unordered_map<int, int> counter;
  cin >> N >> M;
  vi parent(N);
  rep(i, 0, N - 1) { parent[i] = i; }
  rep(i, 0, M - 1) {
    cin >> x >> y;
    x--;
    y--;
    Union(parent, x, y);
  }
  int j, res{0};
  rep(i, 0, N - 1) {
    j = find(parent, i);
    counter[j]++;
    res = max(res, counter[j]);
  }
  cout << res;
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;

int find(vi &parent, int i) {
  if (parent[i] == i)
    return i;
  return find(parent, parent[i]);
}

void Union(vi &parent, int x, int y) {
  int xset = find(parent, x);
  int yset = find(parent, y);
  if (xset != yset) {
    parent[xset] = yset;
  }
}

int main() {
  int N, M;
  int x, y;
  unordered_map<int, int> counter;
  cin >> N >> M;
  vi parent(N);
  rep(i, 0, N - 1) parent[i] = i;
  rep(i, 0, M - 1) {
    cin >> x >> y;
    x--;
    y--;
    Union(parent, x, y);
  }
  int j, res{0};
  rep(i, 0, N - 1) {
    j = find(parent, i);
    counter[j]++;
    res = max(res, counter[j]);
  }
  cout << res;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define forA(i, a, n, x) for (auto i = a; i < n; i += x)
#define forD(i, a, n, x) for (auto i = n - 1; i >= 0; i -= x)
#define ranit(it, n) for (auto it : n)
#define testCases                                                              \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define init(a, x) memset(a, x, sizeof(a))

vll adj[200003];
ll visited[200003];
ll connected = 0;

void dfs(ll v) {
  visited[v] = 1;
  connected++;
  ranit(it, adj[v]) {
    if (visited[it] == 0)
      dfs(it);
    else
      continue;
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, u, v;
  cin >> n >> m;
  ll ans = -1;
  forA(i, 0, m, 1) cin >> u >> v, adj[u].pb(v), adj[v].pb(u);
  // forA(i,1,n+1,1)	{cout<<i<<" : "; ranit(it,adj[i])cout<<it.F<<" ";
  // cout<<"\\n";}
  forA(i, 1, n + 1, 1) {
    connected = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, connected);
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define forA(i, a, n, x) for (auto i = a; i < n; i += x)
#define forD(i, a, n, x) for (auto i = n - 1; i >= 0; i -= x)
#define ranit(it, n) for (auto it : n)
#define testCases                                                              \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define init(a, x) memset(a, x, sizeof(a))

vll adj[100003];
ll visited[100003];
ll connected = 0;

void dfs(ll v) {
  visited[v] = 1;
  connected++;
  ranit(it, adj[v]) {
    if (visited[it] == 0)
      dfs(it);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, u, v;
  cin >> n >> m;
  ll ans = -1;
  forA(i, 0, m, 1) cin >> u >> v, adj[u].pb(v), adj[v].pb(u);
  // forA(i,1,n+1,1)	{cout<<i<<" : "; ranit(it,adj[i])cout<<it.F<<" ";
  // cout<<"\\n";}
  forA(i, 1, n + 1, 1) {
    connected = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, connected);
    }
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 1e18;
const int INF = 1e9;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) 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) {
    if (root(x) == root(y))
      return;
    par[root(x)] = root(y);
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int ROOT[N];
  rep(i, N) ROOT[i] = 0;
  rep(i, N) {
    int r = tree.root(i);
    ROOT[r]++;
  }
  sort(ROOT, ROOT + N);
  cout << ROOT[N - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 1e18;
const int INF = 1e9;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) 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) {
    if (root(x) == root(y))
      return;
    par[root(x)] = root(y);
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int ROOT[N];
  rep(i, N) ROOT[i] = 0;
  rep(i, N) {
    int r = tree.root(i + 1);
    ROOT[r]++;
  }
  sort(ROOT, ROOT + N);
  cout << ROOT[N - 1] << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: 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 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";
}


Error: TLE

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 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, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int dfs(vector<vector<int>> &g, vector<bool> &b, int idx) {
  b[idx] = false;
  int cnt = 1;
  for (int i = 0, len = g[idx].size(); i < len; i++)
    if (b[g[idx][i]])
      cnt += dfs(g, b, g[idx][i]);
  return cnt;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> g;
  g.resize(n);

  for (int i = 0, j, k; i < m; i++) {
    cin >> j >> k;
    g[j - 1].push_back(k - 1);
    g[k - 1].push_back(j - 1);
  }

  vector<bool> b;
  b.resize(n);
  for (int i = 0; i < n; i++)
    b[i] = true;

  int max = 0, cnt;
  for (int i = 0; i < n; i++)
    if (b[i]) {
      cnt = dfs(g, b, i);
      max = max < cnt ? cnt : max;
    }

  cout << max;

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

void dfs(vector<vector<int>> &g, vector<bool> &b, int idx) {
  b[idx] = false;
  for (int i = 0, len = g[idx].size(); i < len; i++)
    if (b[i])
      dfs(g, b, i);
}

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

  int n, m;
  cin >> n >> m;

  vector<vector<int>> g;
  g.resize(n);

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

  vector<bool> b;
  b.resize(n);
  for (int i = 0; i < n; i++)
    i = true;

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (b[i]) {
      cnt++;
      dfs(g, b, i);
    }
  }
  cout << cnt;

  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> adj[200005];
bool vis[200005];
int maxi = 0;

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

void solve() {

  // ifstream myfile;
  // myfile.open("sample.txt");
  int n, m;
  cin >> n >> m;
  memset(vis, false, sizeof(vis));

  int a;
  int b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

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

int main() {
  solve();
  return 0;
}


Error: -11

Fixed submission:

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

vector<int> adj[200005];
bool vis[200005];
int maxi = 0;

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

void solve() {
  // ifstream myfile;
  // myfile.open("sample.txt");
  int n, m;
  cin >> n >> m;
  memset(vis, false, sizeof(vis));

  int a;
  int b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

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

int main() {
  solve();
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: 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;
    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;
  }
}


Error: -11

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;

    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, verdict: 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 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();
}

Error: 0

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