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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

bool seen[1000000];

ll dfs(vector<set<int>> &v, int i) {
  seen[i] = true;
  ll cnt = 1;
  for (auto next : v[i]) {
    if (seen[next]) {
      continue;
    }
    cnt += dfs(v, next);
  }
  return cnt;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<set<int>> v(n, set<int>());
  ll ans = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].insert(b);
    v[b].insert(a);
  }
  rep(i, n) {
    if (!seen[i]) {
      ll cnt = dfs(v, i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  ll n, m;
  cin >> n >> m;
  map<ll, ll> mp;
  rep(i, n) { mp[i] = -1; }
  ll cnt = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    if (mp[a] == -1 && mp[b] == -1) {
      mp[a] = cnt;
      mp[b] = cnt;
      cnt++;
    } else if (mp[a] == -1) {
      mp[a] = mp[b];
    } else if (mp[b] == -1) {
      mp[b] = mp[a];
    }
  }
  vector<ll> v(cnt);
  rep(i, n) { v[mp[i]]++; }
  cout << *max_element(v.begin(), v.end()) << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  int tmp[n] = {};
  UnionFind tree(n);
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    tree.unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    tmp[tree.root(i)]++;
  }
  int MAX = -1;
  for (int i = 0; i < n; i++) {
    if (MAX < tmp[i])
      MAX = tmp[i];
  }
  cout << MAX << endl;
  ;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

#define REP(i, n) for (ll i = 0; i < n; i++) // 0 ~ n-1
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> 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);
  }

  ll size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  ll a, b;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    uf.merge(a, b);
  }
  int MAX = -1, tmp;
  for (int i = 0; i < n; i++) {
    tmp = uf.size(i);
    if (MAX < tmp) {
      MAX = tmp;
    }
  }
  cout << MAX << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

using graph = vector<vector<ll>>;

vector<ll> ans;

// UnionFind実装
struct UnionFind {
  vector<ll> d;
  UnionFind(ll n = 0) : d(n, -1) {}
  ll root(ll x) {
    if (d[x] < 0)
      return x;               // 根は-サイズでここだけが負の値
    return d[x] = root(d[x]); // 親を根に移す、経路縮約
  }
  bool unite(ll x, ll 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(ll x, ll y) { return root(x) == root(y); }
  ll size(ll x) { return -d[root(x)]; }
};

int deg[200005];
vector<int> to[200005];
int main() {
  /*UnionFind uf(100);
  if (uf.same(0,1)){cout << "sw" << endl;}
  else{cout << "ss" << endl;}*/
  ll n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    if (!uf.same(a, b)) {
      deg[a]++;
      deg[b]++;
      uf.unite(a, b);
    }
  }
  ll ans = -1;
  rep(i, n) { ans = max(ans, (ll)uf.size(i)); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

using graph = vector<vector<ll>>;

vector<ll> ans;

// UnionFind実装
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;               // 根は-サイズでここだけが負の値
    return d[x] = root(d[x]); // 親を根に移す、経路縮約
  }
  bool unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y); // 小さい方を大きい方につける
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int deg[100005];
vector<int> to[100005];
int main() {
  /*UnionFind uf(100);
  if (uf.same(0,1)){cout << "sw" << endl;}
  else{cout << "ss" << endl;}*/
  ll n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    if (!uf.same(a, b)) {
      deg[a]++;
      deg[b]++;
      uf.unite(a, b);
    }
  }
  ll ans = -1;
  rep(i, n) { ans = max(ans, (ll)uf.size(i)); }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int f[200010];
int ans[200010];
int vis[200010];
int cnt = 1;
vector<int> ve[200010];
map<int, int> ma[200010];
int val = 0;
void dfs(int x) {
  int sz = ve[x].size();
  for (int i = 0; i < sz; i++) {
    if (!vis[ve[x][i]]) {
      vis[ve[x][i]] = 1;
      val++;
      dfs(ve[x][i]);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    if (!ma[a][b]) {
      ma[a][b] = 1;
      ve[a].push_back(b);
      ve[b].push_back(a);
    }
  }
  int re = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      val = 0;
      dfs(i);
      re = max(val, re);
    }
  }
  cout << re;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int f[200010];
int ans[200010];
int find(int x) {
  if (x == f[x])
    return x;
  else
    return find(f[x]);
}
void unite(int x, int y) {
  // ans[find(f[y])]+=ans[find(x)];
  // ans[find(x)]=0;
  // cout<<find(f[y])<<"   "<<find(x)<<endl;
  f[find(x)] = find(f[y]);
  // cout<<find(f[y])<<"  "<<find(x)<<endl;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    if (find(a) != find(b))
      unite(a, b);
  }
  int re = 0;
  for (int i = 1; i <= n; i++) {
    // cout<<i<<" "<<ans[i]<<" "<<f[i]<<endl;
    ans[find(f[i])]++;
    re = max(re, ans[find(f[i])]);
  }
  cout << re;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

struct UnionFind {
  vector<int> data;

  UnionFind(int N) { data.assign(N, -1); }

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

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

UnionFind uf(200000);

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int ans = 1;

  rep(i, m) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    ans = max(ans, uf.size(a));
  }
  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

struct UnionFind {
  vector<int> data;

  UnionFind(int N) { data.assign(N, -1); }

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

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

UnionFind uf(2);

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int ans = 1;

  rep(i, m) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    ans = max(ans, uf.size(a));
  }
  cout << ans << endl;

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

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d.at(x) < 0) {
      return x;
    }
    return d.at(x) = find(d.at(x));
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    if (d.at(x) > d.at(y)) {
      swap(x, y);
    }
    d.at(x) += d.at(y);
    d.at(y) = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d.at(find(x)); }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }

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

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)


Fixed submission:

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d.at(x) < 0) {
      return x;
    }
    return d.at(x) = find(d.at(x));
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    if (d.at(x) > d.at(y)) {
      swap(x, y);
    }
    d.at(x) += d.at(y);
    d.at(y) = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d.at(find(x)); }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

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

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

int c = 0;
int vs[200004];
int fun(int x) {
  if (vs[x] == x)
    return x;
  return vs[x] = fun(vs[x]);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(30) << fixed;

  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < 200004; i++)
    vs[i] = i;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    vs[fun(max(x, y))] = vs[fun(min(y, x))];
    // for(int i=1;i<=n;i++)
    // 	cout<<vs[i]<<" ";
    // cout<<endl;
  }
  int a[200005] = {0};
  int mx = 0;
  // for(int i=1;i<=n;i++)
  // 	cout<<vs[i]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n; i++) {
    a[fun(i)]++;
    mx = max(a[vs[i]], mx);
  }
  cout << mx << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int c = 0;
int vs[100004];
void fun(vector<vector<int>> v, int x) {
  vs[x] = 1;
  c++;
  for (int y : v[x])
    if (vs[y] == 0)
      fun(v, y);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(30) << fixed;

  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n + 1);
  int x, y;
  memset(vs, 0, sizeof(vs));
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (vs[i] == 0) {
      c = 0;
      fun(v, i);
      mx = max(mx, c);
    }
  }
  cout << mx << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int a[200020];
int si[200020];

int ff(int x) { return a[x] = a[x] == x ? x : ff(a[x]); }

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    a[i] = i, si[i] = 1;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    x = ff(x);
    y = ff(y);
    if (x != y) {
      a[y] = x;
      si[x] += si[y];
    }
  }
  int ma = 1;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, si[i]);
  }
  cout << ma << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int a[200020];
int si[200020];

int ff(int x) { return a[x] == x ? x : ff(a[x]); }

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    a[i] = i, si[i] = 1;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    x = ff(x);
    y = ff(y);
    if (x != y) {
      a[y] = x;
      si[x] += si[y];
    }
  }
  int ma = 1;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, si[i]);
  }
  cout << ma << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back

#define f(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define int long long
using namespace std;
#define mod 1000000007
vector<int> adj[200001];
int visited[200001];
int dfs(int s) {
  visited[s] = 1;
  int count = 1;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      count += dfs(adj[s][i]);
    }
  }
  return count;
}
signed main() {

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

  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<int> arr;
  int r, sum1 = 0, ans = 0;
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      r = dfs(i);
      if (r != 1) {
        arr.pb(r);
      }
    }
  }
  if (arr.size() != 0) {
    sort(arr.rbegin(), arr.rend());
    cout << arr[0] << endl;
  } else {
    cout << 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back

#define f(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define int long long
using namespace std;
#define mod 1000000007
vector<int> adj[200001];
int visited[200001];
int dfs(int s) {
  visited[s] = 1;
  int count = 1;
  for (int i = 0; i < adj[s].size(); i++) {
    if (visited[adj[s][i]] == 0) {
      count += dfs(adj[s][i]);
    }
  }
  return count;
}
signed main() {

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

  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  vector<int> arr;
  int r, sum1 = 0, ans = 0;
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      r = dfs(i);
      if (r != 1) {
        arr.pb(r);
      }
    }
  }
  sort(arr.rbegin(), arr.rend());
  cout << arr[0] << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vvi vector<vi>
#define pai(k) pair<k, k>
#define mod 1000000007
#define mpai make_pair
#define p1 first
#define p2 second
#define pb push_back
#define umap(x, y) unordered_map<x, y>
static void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

class graph {
  vector<int> adj[200001];

public:
  // graph(int n){
  // 	for(int i=0;i<n;i++)
  // 		adj[i]=vector<int>();
  // }
  void addedge(int a, int b) {
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int dfs(int node, vector<int> &visited) {
    visited[node] = 1;
    int ans = 0;
    for (auto i : adj[node]) {
      if (!visited[i])
        ans += dfs(i, visited);
    }
    return 1 + ans;
  }
  int solve(int n) {
    vector<int> visited(n + 1, 0);
    int maxans = 0;
    for (int i = 0; i < n; i++) {
      if (!visited[i]) {
        // cout<<"g";
        // exit(0);
        maxans = max(maxans, dfs(i, visited));
        // count+=1;
      }
    }
    return maxans;
  }
};

int main() {
  fast();

  int n = 0, a, b, m;
  cin >> n >> m;
  if (m == 0)
    cout << "1\\n";
  else {
    graph g;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      g.addedge(a - 1, b - 1);
    }
    // cout<<"h";
    // exit(0);
    int ans = g.solve(n);
    cout << ans << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vvi vector<vi>
#define pai(k) pair<k, k>
#define mod 1000000007
#define mpai make_pair
#define p1 first
#define p2 second
#define pb push_back
#define umap(x, y) unordered_map<x, y>
static void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

class graph {
  vector<int> adj[200];

public:
  // graph(int n){
  // 	for(int i=0;i<n;i++)
  // 		adj[i]=vector<int>();
  // }
  void addedge(int a, int b) {
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int dfs(int node, vector<int> &visited) {
    visited[node] = 1;
    int ans = 0;
    for (auto i : adj[node]) {
      if (!visited[i])
        ans += dfs(i, visited);
    }
    return 1 + ans;
  }
  int solve(int n) {
    vector<int> visited(n + 1, 0);
    int maxans = 0;
    for (int i = 0; i < n; i++) {
      if (!visited[i]) {
        // cout<<"g";
        // exit(0);
        maxans = max(maxans, dfs(i, visited));
        // count+=1;
      }
    }
    return maxans;
  }
};

int main() {
  fast();

  int n = 0, a, b, m;
  cin >> n >> m;
  graph g;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g.addedge(a - 1, b - 1);
  }
  // cout<<"h";
  // exit(0);
  int ans = g.solve(n);
  cout << ans << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
vector<long long> g[200100];
bool ind[200107];
long long n, m, mx, ans;
void dfs(long long i) {
  ind[i] = 1;
  mx++;
  for (long long v : g[i]) {
    if (ind[v] == 0)
      dfs(v);
  }
}
int main() {
  cin >> n >> m;
  long long x, y;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) {
    if (ind[i] == 0) {
      mx = 0;
      dfs(i);
      ans = max(mx, ans);
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<long long> g[100100];
bool ind[100107];
long long n, m, mx, ans;
void dfs(long long i) {
  ind[i] = 1;
  mx++;
  for (long long v : g[i]) {
    if (ind[v] == 0)
      dfs(v);
  }
}
int main() {
  cin >> n >> m;
  long long x, y;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) {
    if (ind[i] == 0) {
      mx = 0;
      dfs(i);
      ans = max(mx, ans);
    }
  }
  cout << ans;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define int long long int
#define pll pair<int, int>

#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define ones(x) __builtin_popcount(x)
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
#define hell 998244353
const int maxn = 2e5 + 1;

vector<vector<int>> a(maxn);

int tot;

int vis[maxn];

void dfs(int u) {
  vis[u] = 1;

  tot++;

  for (auto v : a[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

void solve() {
  int n, m;

  cin >> n >> m;

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

    cin >> x >> y;

    a[x].pb(y);
    a[y].pb(x);
  }

  int ans = 0;

  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      tot = 0;

      dfs(i);

      // cout << i <<" " << tot << endl;

      ans = max(ans, tot);
    }
  }

  cout << ans;
}

signed main() {
  fast;

  int t = 1;

  // cin >> t;

  while (t--) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long int
#define pi 3.141592653589793238
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mod 1000000007
int val = 0, ans = INT_MIN;
void dfs(int node, vector<vector<int>> adj, vector<bool> &visited) {
  visited[node] = true;
  val++;
  for (auto u : adj[node])
    if (!visited[u])
      dfs(u, adj, visited);
}
int main() {
  fast;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(n + 1);
  vector<bool> visited(n + 1, false);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    val = 0;
    if (!visited[i])
      dfs(i, adj, visited);
    ans = max(ans, val);
  }
  cout << ans;
  return 0;
}