alexjercan/bugnet huggingface dataset:
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) {
    sum += a[i];
    sum %= mod;
  }
  ll ans = 0;
  rep(i, n) {
    sum -= a[i];
    if (sum < 0)
      sum += mod;
    ans += a[i] * sum;
    ans %= mod;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, a.size() - 1) for (int j = i + 1; j < a.size(); ++j) {
    sum += a[i] * a[j];
    // cout << sum << endl;
    sum %= mod;
  }
  cout << sum << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = a; i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> fd(n + 1);
  rep(i, 0, m) {
    int t1, t2;
    cin >> t1 >> t2;
    fd[t1].push_back(t2);
    fd[t2].push_back(t1);
  }
  int ans = 0;
  vector<bool> checked(n + 1, 0);
  rep(i, 0, n) {
    queue<int> q;
    if (!checked[i]) {
      q.push(i);
      checked[i] = 1;
    }
    int count = 0;
    while (!q.empty()) {
      int cr = q.front();
      q.pop();
      count++;
      for (int x : fd[cr]) {
        if (!checked[x]) {
          checked[x] = 1;
          q.push(x);
        }
      }
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = a; i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> fd(n + 1);
  rep(i, 0, m) {
    int t1, t2;
    cin >> t1 >> t2;
    fd[t1].push_back(t2);
    fd[t2].push_back(t1);
  }
  int ans = 0;
  rep(i, 0, n) {
    vector<bool> checked(n + 1, 0);
    queue<int> q;
    q.push(i);
    checked[i] = 1;
    int count = 0;
    while (!q.empty()) {
      int cr = q.front();
      q.pop();
      count++;
      for (int x : fd[cr]) {
        if (!checked[x]) {
          checked[x] = 1;
          q.push(x);
        }
      }
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

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 a, b;

  for (int i = 0; i < m; i++) {
    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 << endl;
}


Error: 0

Fixed submission:

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

ll x = 1e9 + 7;
const int INF = 1001001001;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : 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 deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k, l;
  cin >> n >> m >> k;
  k = 0;

  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }

  int kotae = 0;

  rep(i, n) {
    int ans = uf.size(i);

    kotae = max(kotae, ans);
  }
  cout << kotae << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

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

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

constexpr int MAX = 2 * 1e5 + 5;

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

bool used[MAX];

int dfs(int v) {

  int res = 1;

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

  return res;
}

int main() {
  int n, m;

  std::cin >> n >> m;

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

  int ans = 0;

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

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

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

constexpr int MAX = 1e5 + 5;

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

bool used[MAX];

int dfs(int v) {

  int res = 1;

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

  return res;
}

int main() {
  int n, m;

  std::cin >> n >> m;

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

  int ans = 0;

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

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
struct UnionFind {
  vector<int> r;
  UnionFind(int N) {
    r = vector<int>(N, -1); // 全員の友達の数を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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    UF.unite(a, b);
  }
  int ans = 0;
  rep(i, 0, n) { ans = max(ans, UF.size(i)); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
struct UnionFind {
  vector<int> r;
  UnionFind(int N) {
    r = vector<int>(N, -1); // 全員の友達の数を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); // 2人を友達にする
    if (x == y)               // 2人が同じ人の場合は何もしない
      return false;
    if (r[x] >
        r[y]) // xの友達の数がyよりも少ないときはxとyを交換する(xのほうがyよりも友達の数が多くなるようにする)
      swap(x, y);
    r[x] += r[y]; // xの友達の数を増やす
    r[y] = r[x];  // yの友達の数はx
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    UF.unite(a, b);
  }
  int ans = 0;
  rep(i, 0, n) { ans = max(ans, UF.size(i)); }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<long long ll>
#define pb push_back
#define f(i, x, n) for (i = x; i < n; i++)
ll visited[1000000];
vector<ll> a[1000000];
ll c = 0;
void dfs(ll k) {
  if (visited[k] != 1) {
    visited[k] = 1;
    c = c + 1;
    for (ll i = 0; i < (ll)a[k].size(); i++) {
      if (visited[a[k][i]] != 1)
        dfs(a[k][i]);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    c = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, c);
    }
    // cout << c <<" ";
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<long long ll>
#define pb push_back
#define f(i, x, n) for (i = x; i < n; i++)
ll visited[1000];
vector<ll> a[1000];
ll c = 0;
void dfs(ll k) {
  if (visited[k] != 1) {
    visited[k] = 1;
    c = c + 1;
    for (ll i = 0; i < (ll)a[k].size(); i++) {
      if (visited[a[k][i]] != 1)
        dfs(a[k][i]);
    }
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    c = 0;
    if (visited[i] == 0) {
      dfs(i);
      ans = max(ans, c);
    }
    // cout << c <<" ";
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  while (t--)
    solve();
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h> //---------------------------------------------------------------

#define ll long long
#define mod 1000000007
#define MAX 100005
#define PI 3.141592653589793238 // shortcut keys

#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fr(i, a, b) for (int i = a; i <= b; i++)

#define decimal(k) fixed << setprecision(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); //---------------------------------------------------------------
using namespace std;
vector<int> g[200005];
int done[200005] = {};
int x = 0;
void dfs(int u) {
  done[u] = 1;
  x++;
  for (auto v : g[u]) {
    if (done[v])
      continue;
    dfs(v);
  }
}
int main() {

  int n, m;
  cin >> n >> m;
  fr(i, 1, m) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans = 0;
  fr(i, 1, n) {
    x = 0;
    if (done[i])
      continue;
    dfs(i);
    ans = max(ans, x);
  }
  cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h> //---------------------------------------------------------------

#define ll long long
#define mod 1000000007
#define MAX 100005
#define PI 3.141592653589793238 // shortcut keys

#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fr(i, a, b) for (int i = a; i <= b; i++)

#define decimal(k) fixed << setprecision(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); //---------------------------------------------------------------
using namespace std;
vector<int> g[100005];
int done[100005] = {};
int x = 0;
void dfs(int u) {
  done[u] = 1;
  x++;
  for (auto v : g[u]) {
    if (done[v])
      continue;
    dfs(v);
  }
}
int main() {

  int n, m;
  cin >> n >> m;
  fr(i, 1, m) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans;
  fr(i, 1, n) {
    x = 0;
    if (done[i])
      continue;
    dfs(i);
    ans = max(ans, x);
  }
  cout << ans << '\\n';

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

#include <bits/stdc++.h>

using namespace std;

#define MAX_SIZE 200005

int parent[MAX_SIZE];

int Find(int x) {
  if (parent[x] == -1) {
    return x;
  }
  return parent[x] = Find(parent[x]);
}

void Union(int x, int y) {
  int px = Find(x), py = Find(y);
  if (px != py) {
    parent[py] = px;
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  memset(parent, -1, sizeof(parent));

  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(x, y);
  }

  unordered_map<int, int> group;
  for (int i = 1; i <= n; ++i) {
    group[Find(i)]++;
  }

  int ans = 0;
  for (const auto &p : group) {
    if (p.second > ans) {
      ans = p.second;
    }
  }

  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_SIZE 200005

int parent[MAX_SIZE];

int Find(int x) {
  if (parent[x] == -1) {
    return x;
  }
  return parent[x] = Find(parent[x]);
}

int Union(int x, int y) {
  int px = Find(x), py = Find(y);
  if (px != py) {
    parent[py] = px;
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  memset(parent, -1, sizeof(parent));

  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(x, y);
  }

  unordered_map<int, int> group;
  for (int i = 1; i <= n; ++i) {
    group[Find(i)]++;
  }

  int ans = 0;
  for (const auto &p : group) {
    if (p.second > ans) {
      ans = p.second;
    }
  }

  printf("%d\\n", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, sum = 0;
bool vis[200005];
vector<int> fr[200005];
void dfs(int a) {
  vis[a] = true;
  sum++;
  for (int i = 0; i < fr[a].size(); ++i)
    if (!vis[fr[a][i]])
      dfs(fr[a][i]);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (find(fr[x].begin(), fr[x].end(), y) == fr[x].end())
      fr[x].push_back(y);
    if (find(fr[y].begin(), fr[y].end(), x) == fr[y].end())
      fr[y].push_back(x);
  }
  for (int i = 0; i < 200005; ++i)
    vis[i] = false;
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, sum);
      sum = 0;
    }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, sum = 0;
bool vis[200005];
vector<int> fr[200005];
void dfs(int a) {
  if (vis[a])
    return;
  vis[a] = true;
  sum++;
  for (int i = 0; i < fr[a].size(); ++i)
    if (!vis[fr[a][i]])
      dfs(fr[a][i]);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    if (find(fr[x].begin(), fr[x].end(), y) == fr[x].end())
      fr[x].push_back(y);
    if (find(fr[y].begin(), fr[y].end(), x) == fr[y].end())
      fr[y].push_back(x);
  }
  for (int i = 0; i < 200005; ++i)
    vis[i] = false;
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, sum);
      sum = 0;
    }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, f, t) for (int i = f; i < t; i++)
#define ll long long int
using namespace std;

int pr[200010];
int sz[200010];

void make_set() {
  rep(i, 0, 200005) {
    pr[i] = i;
    sz[i] = 1;
  }
}

int find_set(int a) {
  if (pr[a] == a)
    return a;
  return pr[a] = find_set(pr[a]);
}

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

int solve() {
  make_set();
  ll n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int maxi = 0;
  rep(i, 1, n + 1) { maxi = max(maxi, sz[i]); }
  cout << maxi << endl;
  return 0;
}
int main() {
  //    waste;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ll long long
#define mkp make_pair
#define pll pair<ll, ll>
#define rep(i, from, to) for (int i = from; i < to; i++)
#define repd(i, from, till) for (int i = from; i >= till; i--)
#define waste                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define inf 1e9 + 1
#define mod 1e9 + 7
#define inf1 1e18 + 1
#define pie 3.14159265358979323846
#define N 100005
using namespace std;

vector<int> pr(200010);
vector<int> sz(200010);

void make_set() {
  rep(i, 0, 200005) {
    pr[i] = i;
    sz[i] = 1;
  }
}

int find_set(int a) {
  if (pr[a] == a)
    return a;
  else
    return pr[a] = find_set(pr[a]);
}

int union_set(int a, int b) {
  int p = find_set(a);
  int q = find_set(b);
  if (p != q) {
    if (sz[p] < sz[q])
      swap(p, q);
    sz[p] += sz[q];
    pr[q] = p;
  }
}

int solve() {
  make_set();
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int maxi = 0;
  rep(i, 1, n + 1) {
    maxi = max(maxi, sz[i]);
    //		cout<<sz[i]<<endl;
  }
  cout << maxi << endl;
  return 0;
}
int main() {
  waste;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

struct group {
  int num;
  int size;
};

int root(struct group *data, int x) {
  if ((data + x)->num == x)
    return x;
  else {
    int rx = root(data, (data + x)->num);
    (data + x)->num = rx;
    return rx;
  }
}

void unite(struct group *data, int x, int y) {
  int rx = root(data, x), ry = root(data, y);
  if (rx == ry)
    return;
  (data + rx)->num = ry;
  (data + ry)->size += (data + rx)->size;
}

int main() {
  int N, M;
  cin >> N >> M;
  struct group data[N];

  rep(i, 0, N) {
    data[i].num = i;
    data[i].size = 1;
  }

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(data, a, b);
  }

  int ans = 0;
  rep(i, 0, N) { ans = _max(ans, data[i].size); }

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

struct group {
  int num;
  int size;
};

int root(struct group *data, int x) {
  if ((data + x)->num == x)
    return x;
  else
    return root(data, (data + x)->num);
}

void unite(struct group *data, int x, int y) {
  int rx = root(data, x), ry = root(data, y);
  if (rx == ry)
    return;
  (data + rx)->num = ry;
  (data + ry)->size += (data + rx)->size;
}

int main() {
  int N, M;
  cin >> N >> M;
  struct group data[N];

  rep(i, 0, N) {
    data[i].num = i;
    data[i].size = 1;
  }

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(data, a, b);
  }

  int ans = 0;
  rep(i, 0, N) { ans = _max(ans, data[i].size); }

  cout << ans;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  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 tree(N);

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

  // 最大の友達集合を求める
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, tree.size(i));
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  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 tree(N);

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

  // 最大の友達集合を求める
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, tree.size(i));
  }

  cout << ans << endl;
}