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

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
int n, m, fa[200005], mp[200005], ans;
vector<int> v;
pii rel[200005];
int findfa(int x) {
  if (fa[x] == x)
    return x;
  return fa[x] = findfa(fa[x]);
}
void unite(int x, int y) {
  int fx = findfa(x), fy = findfa(y);
  if (fx > fy) {
    int c = fy;
    fy = fx;
    fx = c;
  }
  fa[fy] = fx;
}
int main() {
  cin >> n >> m;
  memset(mp, -1, sizeof(mp));
  for (int i = 0; i < n; i++)
    fa[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (b < a) {
      int c = b;
      b = a;
      a = c;
    }
    --a;
    --b;
    rel[i].F = a;
    rel[i].S = b;
  }
  sort(rel, rel + m);
  for (int i = 0; i < m; i++) {
    int a = rel[i].F, b = rel[i].S;
    unite(a, b);
  }
  for (int i = 0; i < n; i++)
    if (fa[i] == i) {
      mp[i] = (int)(v.size());
      v.pb(0);
    }
  for (int i = 0; i < n; i++)
    v[mp[findfa(i)]]++;
  for (int i = 0; i < v.size(); i++)
    ans = max(ans, v[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int n, m, fa[200005], mp[200005], ans;
vector<int> v;
int findfa(int x) {
  if (fa[x] == -1)
    return x;
  return fa[x] = findfa(fa[x]);
}
int main() {
  memset(fa, -1, sizeof(fa));
  memset(mp, -1, sizeof(mp));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (b < a) {
      int c = b;
      b = a;
      a = c;
    }
    --a;
    --b;
    fa[b] = findfa(a);
  }
  for (int i = 0; i < n; i++) {
    if (fa[i] == -1) {
      mp[i] = (int)(v.size());
      v.pb(1);
    } else
      v[mp[fa[i]]]++;
  }

  for (int i = 0; i < v.size(); i++)
    ans = max(ans, v[i]);
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)

class UnionFind {
public:
  vector<int> par; // 各元の親を表す配列
  vector<int> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
    for (int i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }
  void init(int sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1);
    for (int i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  int root(int x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける）
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y) { // 連結判定
    return root(x) == root(y);
  }

  int size(int x) { // 素集合のサイズ
    return siz[root(x)];
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    if (!(uf.issame(a, b))) {
      uf.merge(a, b);
    }
  }
  for (int i = 0; i < n; i++)
    ans = max(ans, uf.size(i));
  cout << ans << endl;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)

class UnionFind {
public:
  vector<ll> par;  // 各元の親を表す配列
  vector<int> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  ll root(ll x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける）
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { // 連結判定
    return root(x) == root(y);
  }

  int size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};
int main() {
  ll n, m;
  cin >> n >> m;
  UnionFind uf(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    if (!(uf.issame(a, b))) {
      uf.merge(a, b);
    }
  }
  for (ll i = 0; i < n; i++)
    ans = max(ans, uf.size(i));
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

/*author @dhanush*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define pb push_back
#define se second
#define fi first
#define INF INT_MAX

int parent[200001];

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

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}

int main() {

  int n, m, a, b;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    parent[i] = -1; // initializing

  while (m--) {
    cin >> a >> b;
    a = find(a), b = find(b);

    if (a != b)
      Union(a, b);
  }

  int res = -1;

  for (int i = 1; i <= n; i++)
    if (parent[i] < 0)
      res = min(res, parent[i]);

  cout << abs(res);
}


Error: TLE

Fixed submission:

/*author @dhanush*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define pb push_back
#define se second
#define fi first
#define INF INT_MAX

int parent[200001];

int find(int a) {
  while (parent[a] > 0)
    a = parent[a];

  return a;
}

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}

int main() {

  int n, m, a, b;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    parent[i] = -1; // initializing

  while (m--) {
    cin >> a >> b;
    a = find(a), b = find(b);

    if (a != b)
      Union(a, b);
  }

  int res = -1;

  for (int i = 1; i <= n; i++)
    if (parent[i] < 0)
      res = min(res, parent[i]);

  cout << abs(res);
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, ans;
int fa[maxn], siz[maxn];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    fa[i] = i, siz[i] = 1;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    u = getfa(u), v = getfa(v);
    if (u == v)
      continue;
    fa[u] = v;
    siz[v] += siz[u];
  }
  for (int i = 1; i <= n; ++i)
    if (getfa(i) == i)
      ans = max(ans, siz[i]);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int n, m, ans, fa[N], siz[N];

inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    w *= ch == '-' ? -1 : 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    s = s * 10 + ch - '0';
  return s * w;
}

int findf(int x) {
  if (x == fa[x])
    return x;
  return fa[x] == findf(fa[x]);
}

int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; ++i)
    fa[i] = i, siz[i] = 1;
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read();
    x = findf(x), y = findf(y);
    if (x == y)
      continue;
    fa[x] = y, siz[y] += siz[x];
  }
  for (int i = 1; i <= n; ++i)
    if (fa[i] == i)
      ans = max(ans, siz[i]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int findSet(vector<int> &a, int i) {
  if (a[i] < 0)
    return i;
  return a[i] = findSet(a, a[i]);
}

void mergeSets(vector<int> &a, int i, int j) {
  int x = findSet(a, i);
  int y = findSet(a, j);
  if (x == y) {
    return;
  }
  if (a[x] > a[y]) {
    int t = x;
    x = y;
    y = t;
  }
  a[x] += a[y];
  a[y] = x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mergeSets(v, a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << v[i] << " " << i << endl;
    ans = min(ans, v[i]);
  }
  ans = abs(ans);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int findSet(vector<int> a, int i) {
  if (a[i] < 0)
    return i;
  return a[i] = findSet(a, a[i]);
}

void mergeSets(vector<int> &a, int i, int j) {
  int x = findSet(a, i);
  int y = findSet(a, j);
  if (x == y) {
    return;
  }
  if (a[x] > a[y]) {
    int t = x;
    x = y;
    y = t;
  }
  a[x] += a[y];
  a[y] = x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mergeSets(v, a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << v[i] << " " << i << endl;
    ans = min(ans, v[i]);
  }
  ans = abs(ans);
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#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;
    A -= 1;
    B -= 1;
    UF.unite(A, B);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

void mergeSets(vector<int> &a, int i, int j, int n) {
  if (abs(a[i]) < abs(a[j])) {
    a[j] += a[i];
    a[i] = j;
  } else {
    a[i] += a[j];
    a[j] = i;
  }
}

int findSet(vector<int> a, int i, int n) {
  int j = i;
  while (a[i] > 0) {
    i = a[i];
  }
  while (a[j] > 0) {
    int t = a[j];
    a[j] = i;
    j = t;
  }
  return i;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 1);
  for (int i = 0; i <= n; i++) {
    v[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int f1 = findSet(v, a, n);
    int f2 = findSet(v, b, n);
    if (f1 != f2) {
      mergeSets(v, f1, f2, n);
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    // cout << v[i] << " " << i << endl;
    ans = min(ans, v[i]);
  }
  ans = abs(ans);
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

/******
        ~site:    atcoder.jp
        ~user:    Turkhuu
        ~problem: AtCoder Beginner Contest 177 - D (abc177_d)
** **/

#include <bits/stdc++.h>

#define FFOR(i, s, e, k) for (int i = s; (k == 1 ? i <= e : i >= e); i += k)
#define FOR(i, n) FFOR(i, 0, n - 1, 1)
#define FORR(i, s, e) FFOR(i, s, e, 1)
#define ll long long
#define PQ priority_queue
#define pb push_back
#define ppb pop_back()
#define pp push
#define MIN(v) *min_element(v.begin(), v.end())
#define MAX(v) *max_element(v.begin(), v.end())
#define SORT(v) sort(v.begin(), v.end())
#define TROS(v) sort(v.rbegin(), v.rend())
#define CNT(v, x) count(v.begin(), v.end(), x)
#define FIND(v, x) find(v.begin(), v.end(), x)

using namespace std;

int link[200001], sz[200001];

int find(int x) {
  while (x != link[x])
    x = link[x];
  return x;
}

void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  link[b] = a;
}

bool same(int a, int b) { return find(a) == find(b); }

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

  int n, m;
  cin >> n >> m;
  FOR(i, n) {
    link[i] = i;
    sz[i] = 1;
  }

  FOR(i, m) {
    int a, b;
    cin >> a >> b;
    if (same(a, b))
      continue;
    unite(a, b);
  }

  int ans = 0;
  FOR(i, n) ans = max(ans, sz[i]);
  cout << ans;
}

Error: TLE

Fixed submission:

/******
        ~site:    atcoder.jp
        ~user:    Turkhuu
        ~problem: AtCoder Beginner Contest 177 - D (abc177_d)
** **/

#include <bits/stdc++.h>

#define FFOR(i, s, e, k) for (int i = s; (k == 1 ? i <= e : i >= e); i += k)
#define FOR(i, n) FFOR(i, 0, n - 1, 1)
#define FORR(i, s, e) FFOR(i, s, e, 1)
#define ll long long
#define PQ priority_queue
#define pb push_back
#define ppb pop_back()
#define pp push
#define MIN(v) *min_element(v.begin(), v.end())
#define MAX(v) *max_element(v.begin(), v.end())
#define SORT(v) sort(v.begin(), v.end())
#define TROS(v) sort(v.rbegin(), v.rend())
#define CNT(v, x) count(v.begin(), v.end(), x)
#define FIND(v, x) find(v.begin(), v.end(), x)

using namespace std;

int n, m;
vector<int> v[200001];
bool used[200001] = {0};
int ans = 0, cnt;

void dfs(int i, int k) {
  used[i] = 1;
  for (auto it : v[i]) {
    if (used[it] == 0) {
      dfs(it, k + 1);
      cnt++;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  FOR(i, m) {
    int a, b;
    cin >> a >> b;
    if (FIND(v[a - 1], b - 1) == v[a - 1].end())
      v[a - 1].pb(b - 1);
    if (FIND(v[b - 1], a - 1) == v[b - 1].end())
      v[b - 1].pb(a - 1);
  }
  FOR(i, n) {
    if (used[i] == 0) {
      cnt = 1;
      dfs(i, 1);
      ans = max(ans, cnt);
    }
  }
  cout << ans;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double db;

#define MOD 1000000007
#define mi 18446744073709551615
#define ff first
#define ss second
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>

int n, c;
vii a[200005];
bool vis[200005] = {};

void dfs(int s) {
  if (vis[s] == true)
    return;
  vis[s] = true;
  c++;
  for (int i : a[s])
    dfs(i);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin>>T;
  while (T--) {
    int m, t1, t2, ans = 0;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      cin >> t1 >> t2;
      a[t1].push_back(t2);
      a[t2].push_back(t1);
    }
    for (int i = 1; i <= n; i++) {
      c = 0;
      if (!vis[i])
        dfs(i);
      ans = max(ans, c);
    }
    cout << ans;
  }
  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double db;

#define MOD 1000000007
#define mi 18446744073709551615
#define ff first
#define ss second
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>

int n, c;
vii a[100001];
bool vis[100001] = {};

void dfs(int s, int e) {
  if (vis[s] == true)
    return;
  vis[s] = true;
  c++;
  for (int i : a[s])
    dfs(i, s);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin>>T;
  while (T--) {
    int m, t1, t2, ans = 0;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      cin >> t1 >> t2;
      a[t1].push_back(t2);
      a[t2].push_back(t1);
    }
    for (int i = 1; i <= n; i++) {
      c = 0;
      if (!vis[i])
        dfs(i, -1);
      ans = max(ans, c);
    }
    cout << ans;
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll read() {
  ll x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int f[500005], n, m, maxn, cnt[500005];
int find(int x) {
  if (x == f[x])
    return f[x];
  return f[x] = find(f[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    int x = find(a), y = find(b);
    if (x != y)
      f[x] = y;
  }
  for (int i = 1; i <= n; i++)
    cnt[find(i)]++;
  for (int i = 1; i <= n; i++)
    maxn = max(maxn, cnt[i]);
  cout << maxn;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll read() {
  ll x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int f[100005], n, m, maxn, cnt[100005];
int find(int x) {
  if (x == f[x])
    return f[x];
  return f[x] = find(f[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    int x = find(a), y = find(b);
    if (x != y)
      f[x] = y;
  }
  for (int i = 1; i <= n; i++)
    cnt[find(i)]++;
  for (int i = 1; i <= n; i++)
    maxn = max(maxn, cnt[i]);
  cout << maxn;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int p[200005], n, m, g[200005], ans = 0, nc = 0;
vector<int> v;
int fa(int x) { return (p[x] == x) ? x : (p[x] = fa(p[x])); }
void merge(int x, int y) {
  int fx = fa(x), fy = fa(y);
  p[fx] = fy;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    merge(a, b);
  }
  for (int i = 1; i <= n; i++) {
    g[fa(i)]++;
  }
  for (int i = 1; i <= n; i++)
    if (g[i] != 0)
      v.push_back(g[i]);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 && v[i] == v[i - 1])
      continue;
    ans += (v[i] - nc);
    nc += (v[i] - nc);
    // cout<<i<<' '<<ans<<' '<<nc<<endl;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int p[100005], n, m, g[100005], ans = 0, nc = 0;
vector<int> v;
int fa(int x) { return (p[x] == x) ? x : (p[x] = fa(p[x])); }
void merge(int x, int y) {
  int fx = fa(x), fy = fa(y);
  p[fx] = fy;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    merge(a, b);
  }
  for (int i = 1; i <= n; i++) {
    g[fa(i)]++;
  }
  for (int i = 1; i <= n; i++)
    if (g[i] != 0)
      v.push_back(g[i]);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 && v[i] == v[i - 1])
      continue;
    ans += (v[i] - nc);
    nc += (v[i] - nc);
    // cout<<i<<' '<<ans<<' '<<nc<<endl;
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

struct UnionFind {
  vector<int> par;

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

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

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind UN(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    UN.unite(a, b);
  }
  int ans = 0;
  rep(i, n) { ans = max(ans, UN.size(i)); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> frdnm(n, 0);
  vector<pair<int, int>> p(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    p.at(i) = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  rep(i, m) {
    int a, b;
    tie(a, b) = p.at(i);
    if (i == 0) {
      frdnm.at(a)++;
      frdnm.at(b)++;
      continue;
    }
    int a1, b1;
    tie(a1, b1) = p.at(i - 1);
    if (a != a1 && b != b1) {
      frdnm.at(a)++;
      frdnm.at(b)++;
    }
  }
  int M = frdnm.at(0);
  rep(i, n - 1) { M = max(M, frdnm.at(i + 1)); }
  cout << M + 2 << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
typedef long long ll;
using namespace std;

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

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

  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;
    size[ry] += size[rx];
    size[rx] = 0;
  }

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

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 ans = 0;

  rep(i, n) ans = ans < tree.size[i] ? tree.size[i] : ans;

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
typedef long long ll;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a(m);
  rep(i, m) { cin >> a[i].first >> a[i].second; }
  vector<int> dict(n);
  rep(i, n) dict[i] = i;

  queue<int> que;
  int ans = 0;

  while (dict.size() >= 1) {
    que.push(dict[0]);
    set<int> s{dict[0]};
    dict.erase(dict.begin());
    while (!que.empty()) {
      int targ = que.front();
      que.pop();
      rep(i, m) {
        if (a[i].first == targ) {
          que.push(a[i].second);
          s.insert(a[i].second);
          a[i].first = 0;
          a[i].second = 0;
          rep(j, dict.size()) {
            if (dict[j] == a[j].second) {
              dict.erase(dict.begin() + j);
              break;
            }
          }
        }
        if (a[i].second == targ) {
          que.push(a[i].first);
          s.insert(a[i].first);
          a[i].first = 0;
          a[i].second = 0;
          rep(j, dict.size()) {
            if (dict[j] == a[j].first) {
              dict.erase(dict.begin() + j);
              break;
            }
          }
        }
      }
    }

    ans = s.size() > ans ? s.size() : ans;
  }

  cout << ans << endl;
}