alexjercan/bugnet huggingface dataset:
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 vector<int> vi;
typedef vector<pair<int, int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

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

void dfs(vector<vector<int>> &arr, vector<int> &vis, int curr, int col) {
  queue<int> q;
  q.push(curr);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    if (vis[now] != 0)
      continue;
    vis[now] = col;
    for (auto i : arr[now]) {
      if (vis[i] == 0)
        q.push(i);
      // cerr<<i<<" ";
    }
    // cerr<<endl;
  }
}

void solve(int test_case) {
  int n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return;
  }
  vector<vector<int>> arr(n, vector<int>());
  while (m--) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    arr[f].PUB(t);
    arr[t].PUB(f);
  }
  vector<int> vis(n, 0);
  int col = 1;
  for (int i = 0; i < n; i++)
    if (vis[i] == 0)
      dfs(arr, vis, i, col++);
  vector<int> cols(col, 0);
  int ans = 0;
  for (int i : vis) {
    if (i != 0)
      cols[i]++, ans = max(ans, cols[i]);
  }
  // for(int i:cols)cerr<<i<<" ";
  cout << ans;
}

int main() {

  ////// FILE BASED IO////
  // freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
  ///////////////

  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(i); }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

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

void dfs(vector<vector<int>> &arr, vector<int> &vis, int curr, int col) {
  queue<int> q;
  q.push(curr);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    if (vis[now] != 0)
      continue;
    vis[now] = col;
    for (auto i : arr[now]) {
      if (vis[i] == 0)
        q.push(i);
      // cerr<<i<<" ";
    }
    // cerr<<endl;
  }
}

void solve(int test_case) {
  int n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return;
  }
  vector<vector<int>> arr(n, vector<int>(10000));
  while (m--) {
    int f, t;
    cin >> f >> t;
    f--, t--;
    arr[f].PUB(t);
    arr[t].PUB(f);
  }
  vector<int> vis(n, 0);
  int col = 1;
  for (int i = 0; i < n; i++)
    if (vis[i] == 0)
      dfs(arr, vis, i, col++);
  vector<int> cols(col, 0);
  int ans = 0;
  for (int i : vis) {
    if (i != 0)
      cols[i]++, ans = max(ans, cols[i]);
  }
  // for(int i:cols)cerr<<i<<" ";
  cout << ans;
}

int main() {

  ////// FILE BASED IO////
  // freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
  ///////////////

  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(i); }
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  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(void) {
  int N, M;
  cin >> N >> M;

  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree.unite(a, b);
  }

  int ans = 0;
  rep(i, N) ans = max(ans, tree.size(i));
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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 main(void) {
  int N, M;
  cin >> N >> M;

  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree.unite(a, b);
  }

  set<int> pars(tree.par.begin(), tree.par.end());
  vector<int> gm(N);
  for (int i : pars) {
    for (int p : tree.par) {
      gm[i] += tree.same(i, p);
    }
  }
  int ans = *max_element(gm.begin(), gm.end());
  cout << ans << endl;

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

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

Error: 0

Fixed submission:

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

#include <cmath>
#include <iostream>

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

class UnionFind {

  vector<int> par;

public:
  UnionFind(int n) {
    par = vector<int>(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
    }
  }
  bool unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) {
      return false;
    } else {
      par[ry] = rx;
      return true;
    }
  }
  int root(int x) {
    if (par[x] != x) {
      par[x] = root(par[x]);
    }
    return par[x];
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    uf.unite(a[i], b[i]);
  }
  unordered_map<int, int> map;
  for (int i = 0; i < n; ++i) {
    map[uf.root(i)] += 1;
  }
  int max_len = 0;
  for (auto it = map.begin(); it != map.end(); ++it) {
    if (it->second > max_len) {
      max_len = it->second;
    }
  }
  cout << max_len << endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

class UnionFind {

  vector<int> par;

public:
  UnionFind(int n) {
    par = vector<int>(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
    }
  }
  bool unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) {
      return false;
    } else {
      par[ry] = rx;
      return true;
    }
  }
  int root(int x) {
    int rx = par[x];
    while (rx != x) {
      x = rx;
      rx = par[x];
    }
    return x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    uf.unite(a[i], b[i]);
  }
  unordered_map<int, int> map;
  for (int i = 0; i < n; ++i) {
    map[uf.root(i)] += 1;
  }
  int max_len = 0;
  for (auto it = map.begin(); it != map.end(); ++it) {
    if (it->second > max_len) {
      max_len = it->second;
    }
  }
  cout << max_len << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<set<int>> a(n);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    a[b - 1].insert(c - 1);
    a[c - 1].insert(b - 1);
  }
  deque<int> d;
  vector<bool> seen;
  int summ = 0;
  seen.assign(n, false);
  int v;
  int count = 0;
  for (int j = 0; j < n; ++j) {
    v = j;
    if (seen[v])
      continue;
    count = 0;
    seen[v] = true;
    while (1) {
      ++count;
      for (auto i : a[v]) {
        if (!seen[i])
          d.push_back(i);
        seen[i] = true;
      }
      if (!d.empty()) {
        v = d.front();
        d.pop_front();
        //        cout<<v<<" "<<count<<endl;
      } else {
        break;
      }
    }
    summ = max(summ, count);
  }
  cout << summ << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n);
  for (int i = 0; i < m; ++i) {
    int b, c;
    cin >> b >> c;
    a[b - 1].push_back(c - 1);
    a[c - 1].push_back(b - 1);
  }
  deque<int> d;
  vector<bool> seen;
  vector<int> sumsan;
  seen.assign(n, false);
  int v;
  int oldsum = 0;
  for (int j = 0; j < n; ++j) {
    v = j;
    if (seen[v])
      continue;
    while (1) {
      seen[v] = true;
      for (int i = 0; i < (int)a[v].size(); ++i) {
        if (!seen[a[v][i]])
          d.push_back(a[v][i]);
      }
      if (!d.empty()) {
        v = d.front();
        d.pop_front();
      } else {
        break;
      }
    }
    sumsan.push_back(accumulate(seen.begin(), seen.end(), 0) - oldsum);
    oldsum = accumulate(seen.begin(), seen.end(), 0);
  }
  sort(sumsan.begin(), sumsan.end());
  //  for(auto sum:sumsan)cout<<sum<<endl;
  cout << sumsan.back() << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const int Nmax = 2e5 + 1;
const int Nd = 61;
const int INF = 1e5;

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 UF(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    UF.unite(a, b);
  }

  int ans = 0;
  rep(i, n) { ans = max(ans, UF.size(i)); }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const int Nmax = 2e5 + 1;
const int Nd = 61;
const int INF = 1e5;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g(n, 0);       // 人iがグループg[i]に所属
  vector<bool> ok(n, false); // 人iをすでに友人カウントしたか？
  vector<int> a0;
  vector<vector<bool>> nakama(n, vector<bool>(n, false));
  vector<vector<int>> to(n);
  int a, b;

  if (m == 0) {
    cout << 1 << endl;
    return 0;
  }

  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    if (!nakama[a][b]) {
      a0.push_back(a);
      to[a].push_back(b);
      to[b].push_back(a);
    }
    nakama[a][b] = true;
    nakama[b][a] = true;
  }

  int now = 0;
  int ans = 0;

  rep(i, n) {
    now = 0;
    if (!ok[i]) { // まだi番目の人の友人グループを数えてないとき
      queue<int> q;
      q.push(i);
      ok[i] = true;
      while (!q.empty()) {
        int p = q.front();
        q.pop();
        now++;
        for (int j : to[p]) {
          if (!ok[j]) {
            q.push(j);
            ok[j] = true;
          }
        }
      }
    }
    ans = max(now, ans);
    if (2 * ans > n) {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vv vector<ll>
#define MAX 105000
#define rr pow(10, 9) + 7
#define fr(i, x) for (int i = 0; i < x; i++)
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {

  if (a.first == b.first)
    return a.second < b.second;
  return (a.first < b.first);
}
ll ar[MAX];
void divisor() {
  for (int i = 1; i <= MAX; i++) {
    for (int j = i; j <= MAX; j += i) {
      ar[j]++;
    }
  }
}
bool vi[100006] = {false};
ll n, m, x, u, v, c = 0;

void dfs(ll r, vector<vector<ll>> &a) {
  c++;
  vi[r] = true;
  for (int i = 0; i < a[r].size(); i++) {
    if (vi[a[r][i]] == false) {
      // cout << a[r][i] << " ";
      dfs(a[r][i], a);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  vector<vector<ll>> a(n + 1);
  for (ll i = 0; i < m; i++) {
    cin >> u >> v;
    a[u].push_back(v);
    a[v].push_back(u);
  }
  for (ll i = 1; i <= n; i++) {
    if (a[i].size() == 0)
      vi[i] = true;
  }
  ll re = INT_MIN;
  for (ll i = 1; i <= n; i++) {
    if (vi[i] == false) {
      dfs(i, a);
      re = max(c, re);
      c = 0;
    }
  }
  if (m == 0)
    cout << 1 << endl;
  else
    cout << re << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vv vector<ll>
#define MAX 100006
#define rr pow(10, 9) + 7
#define fr(i, x) for (int i = 0; i < x; i++)
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {

  if (a.first == b.first)
    return a.second < b.second;
  return (a.first < b.first);
}
ll ar[MAX];
void divisor() {
  for (int i = 1; i <= MAX; i++) {
    for (int j = i; j <= MAX; j += i) {
      ar[j]++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll d, ns, nt, r = INT_MIN;

  ll n, x, c = 0, y, m;
  ll a[MAX];
  for (int i = 0; i < MAX; i++)
    a[i] = -1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (a[min(x, y)] != -1)
      a[max(x, y)] = a[min(x, y)];
    else
      a[max(x, y)] = min(x, y);
  }
  // for(int i = 0; i <= n; i++) cout << a[i] << " ";

  map<ll, ll> ma;
  for (ll i = 0; i <= n; i++) {
    ma[a[i]]++;
  }
  for (auto i : ma) {
    // cout << i.first << " " << i.second << endl;
    if (i.first != -1)
      r = max(r, i.second);
  }
  cout << r + 1 << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long int ctr = 0;
unordered_map<long long int, bool> visited;
unordered_map<long long int, list<long long int>> m;
void dfs(long long int node) {
  ctr++;
  visited[node] = true;
  for (auto x : m[node]) {
    if (!visited[x]) {
      dfs(x);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, M;
  cin >> n >> M;

  while (M--) {
    long long int a, b;
    cin >> a >> b;

    m[a].push_back(b);
    m[b].push_back(a);
  }
  long long int ans = 0;

  for (long long int i = 1; i <= n; i++) {
    visited[i] = false;
  }

  for (long long int i = 1; i <= n; i++) {
    ctr = 0;
    if (!visited[i]) {
      dfs(i);
      ans = max(ans, ctr);
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int ctr = 0;
unordered_map<long long int, bool> visited;
void dfs(unordered_map<long long int, list<long long int>> m,
         long long int node) {
  ctr++;
  visited[node] = true;
  for (auto x : m[node]) {
    if (!visited[x]) {
      dfs(m, x);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, M;
  cin >> n >> M;
  unordered_map<long long int, list<long long int>> m;
  map<pair<long long int, long long int>, int> check;
  while (M--) {
    long long int a, b;
    cin >> a >> b;
    long long int t1 = min(a, b);
    long long int t2 = max(a, b);
    if (check.count({t1, t2}) == 1) // no need to insert...already there.
    {
      continue;
    }
    check[{t1, t2}] = 1;
    m[a].push_back(b);
    m[b].push_back(a);
  }
  long long int ans = 0;

  for (long long int i = 1; i <= n; i++) {
    visited[i] = false;
  }

  for (long long int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(m, i);
      ans = max(ans, ctr);
      ctr = 0;
    }
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: 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 = 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;
}


Error: WA

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 = 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;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

#define pb push_back
#define ll long long

ll M = 1000000007;

vector<ll> adj[2 * 100005 + 1];
ll vis[2 * 100005 + 1];
vector<ll> cc;
ll ccnt = 0;

void explore(ll v) {
  vis[v] = 1;
  ccnt++;

  for (ll i = 0; i < adj[v].size(); i++) {
    if (vis[adj[v][i]] != 1) {
      explore(adj[v][i]);
    }
  }
}

void dfs(ll n) {

  for (ll i = 0; i < n; i++) {
    if (vis[i] != 1) {
      ccnt = 0;
      explore(i);
      cc.pb(ccnt);
    }
  }
}

int main() {

  ll n, m;
  cin >> n >> m;
  ll ans = 1;
  if (m == 0) {
    ans = 1;
  } else {
    memset(vis, 0, sizeof(vis));
    for (ll i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      x = x - 1;
      y = y - 1;
      auto it = find(adj[x].begin(), adj[x].end(), y);
      if (it == adj[x].end()) {
        adj[x].pb(y);
        adj[y].pb(x);
      }
    }

    dfs(n);

    for (ll i = 0; i < cc.size(); i++) {
      ans = max(ans, cc[i]);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define pb push_back
#define ll long long

ll M = 1000000007;

vector<ll> adj[100005];
ll vis[100005];
vector<ll> cc;
ll ccnt = 0;

void explore(ll v) {
  vis[v] = 1;
  ccnt++;

  for (ll i = 0; i < adj[v].size(); i++) {
    if (vis[adj[v][i]] != 1) {
      explore(adj[v][i]);
    }
  }
}

void dfs(ll n) {

  for (ll i = 0; i < n; i++) {
    if (vis[i] != 1) {
      ccnt = 0;
      explore(i);
      cc.pb(ccnt);
    }
  }
}

int main() {

  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  if (m == 0) {
    ans = 1;
  } else {
    memset(vis, 0, sizeof(vis));
    for (ll i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      x = x - 1;
      y = y - 1;
      auto it = find(adj[x].begin(), adj[x].end(), y);
      if (it == adj[x].end()) {
        adj[x].pb(y);
        adj[y].pb(x);
      }
    }

    dfs(n);

    for (ll i = 0; i < cc.size(); i++) {
      ans = max(ans, cc[i]);
    }
  }
  cout << ans << endl;

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


Error: 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);
  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;
}