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

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
const int mod = 1e9 + 7;

int a[N];
map<int, int> mp;

int find(int x) {
  if (a[x] == x)
    return x;
  return a[x] = find(a[x]);
}

void add(int x, int y) {
  int px = find(x);
  int py = find(y);
  if (px != py)
    a[px] = py;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  mp.clear();
  for (int i = 1; i <= n; i++)
    a[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    add(x, y);
  }
  for (int i = 1; i <= n; i++)
    find(i);
  /*for (int i = 1; i <= n; i++)
          cout << i << ' ' << a[i] << endl;*/
  for (int i = 1; i <= n; i++)
    mp[a[i]]++;
  int ans = 0;
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    ans = max(ans, it->second);
    // cout << "mp:" << it->first << ' ' << it->second << endl;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define pi acos(-1)
#define valid(x, y) x >= 1 && x <= 8 && y >= 1 && y <= 8
#define pb push_back
#define pll pair<ll, ll>
#define pii <pair<ll , ll>
#define mp make_pair
#define M (1e9 + 7)
#define sl1(t) scanf("%lld", &t)
#define si1(t) scanf("%d", &t)
#define sl3(t, u, v) scanf("%lld %lld %lld", &t, &u, &v)
#define si3(t, u, v) scanf("%d %d %d", &t, &u, &v)
#define sl2(t, u) scanf("%lld %lld", &t, &u)
#define si2(t, u) scanf("%d %d", &t, &u)
#define el printf("\\n")
#define sp printf(" ")
#define pri(t) printf("%d ", t)
#define prl(t) printf("%lld ", t)
#define pc(x) printf("Case %lld: ", x)
// int dx[] = {1 , -1  , 0 , 0 , 1 , -1 , 1 , -1};
// int dy[] = {0 , 0 , 1 , -1 , -1 , 1 , 1 , -1};

vector<ll> v[100005];
ll vis[100005];

ll cnt = 0;
ll cnt2 = 0;

void dfs(ll n) {
  if (vis[n] != 0)
    return;
  vis[n] = cnt;
  cnt2++;
  for (ll i = 0; i < v[n].size(); i++) {
    dfs(v[n][i]);
  }
}

int main() {
// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
///*
#ifndef ONLINE_JUDGE
  freopen("input.cpp", "r", stdin);
// freopen("output.txt" , "w" , stdout);
#endif // ONLINE_JUDGE
  //*/
  ll n, e;
  cin >> n >> e;
  for (ll i = 0; i < e; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      cnt++;
      cnt2 = 0;
      dfs(i);
      ans = max(ans, cnt2);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
vector<ll> adj[200005];
ll cnt = 0;
bool status[200005];
void dfs(int s) {
  cnt++;
  status[s] = true;
  for (auto it : adj[s]) {
    if (!status[it]) {
      dfs(it);
    }
  }
}
void solve() {
  ll n, m;
  cin >> n >> m;
  ll a, b;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  memset(status, false, sizeof(status));
  ll maxx = -1;
  for (int i = 1; i <= n; i++) {
    if (!status[i]) {
      cnt = 0;
      dfs(i);
      maxx = max(maxx, cnt);
    }
  }
  cout << maxx << endl;
}
int main() { solve(); }


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
vector<ll> adj[2001];
ll cnt = 0;
bool status[2001];
void dfs(int s) {
  cnt++;
  status[s] = true;
  for (auto it : adj[s]) {
    if (!status[it]) {
      dfs(it);
    }
  }
}
void solve() {
  ll n, m;
  cin >> n >> m;
  memset(status, false, sizeof(status));
  ll a, b;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ll maxx = -1;
  for (int i = 1; i <= n; i++) {
    if (!status[i]) {
      cnt = 0;
      dfs(i);
      maxx = max(maxx, cnt);
    }
  }
  cout << maxx << endl;
}
int main() { solve(); }
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) {
    par = vector<int>(n);
    rep(i, n) par[i] = -1;
  }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int x_ = root(x);
    int y_ = root(y);
    if (x_ == y_)
      return;
    par[y_] += par[root(x_)];
    par[x_] = root(y_);
  }

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

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) {
    par = vector<int>(n);
    rep(i, n) par[i] = i;
  }

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

  void unite(int x, int y) {
    int x_ = root(x);
    int y_ = root(y);
    if (x_ == y_)
      return;
    par[x_] = y_;
  }

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

  int size(int x) {
    int size = 0;
    rep(i, par.size()) {
      if (root(par[i]) == x)
        size++;
    }
    return size;
  }
};

int main() {
  int n, m;
  cin >> n >> m;

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

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

  cout << ans << endl;

  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define NMAX 300005

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
vector<ll> edg[NMAX];
bitset<NMAX> visited;
ll currCnt = 0;
void dfs(ll node) {
  if (!visited[node])
    currCnt++;
  visited[node] = 1;
  for (auto it : edg[node]) {
    if (!visited[it]) {
      currCnt++;
      visited[it] = 1;
      dfs(it);
    }
  }
}
int main() {
  ll ans = 0, prevCnt = 0;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    edg[a].push_back(b);
    edg[b].push_back(a);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      ans = max(ans, currCnt - prevCnt);
      prevCnt = currCnt;
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define NMAX 300005

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
vector<ll> edg[NMAX];
bitset<NMAX> visited;
void dfs(ll node) {
  visited[node] = 1;
  for (auto it : edg[node]) {
    if (!visited[it]) {
      visited[it] = 1;
      dfs(it);
    }
  }
}
int main() {
  ll ans = 0, prevCnt = 0;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    edg[a].push_back(b);
    edg[b].push_back(a);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      ans = max(ans, (ll)(visited.count() - prevCnt));
      prevCnt = visited.count();
    }
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int bfs(int s, vector<vector<int>> &adj, vector<int> &visited) {

  queue<int> q;
  q.push(s);

  visited[s] = 1;

  int size = 1;

  while (!q.empty()) {

    int t = q.front();
    q.pop();

    for (auto x : adj[t]) {
      if (visited[x] == 0) {
        q.push(x);
        visited[x] = 1;
        size++;
      }
    }
  }

  return size;
}

void solve() {

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

  vector<vector<int>> adj(n + 1);

  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    // cout<<l<<r ;

    adj[l].push_back(r);
    adj[r].push_back(l);
  }

  vector<int> visited(n + 1);

  int res = 0;

  for (int i = 1; i <= n; i++) {

    if (visited[i] == 0) {
      int answer = bfs(i, adj, visited);
      res = max(res, answer);
    }
  }

  cout << res;
}
int main() {

  int y;

  y = 1;
  // cin>>y ;

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

Error: TLE

Fixed submission:

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

int bfs(int s, vector<vector<int>> &adj, vector<int> visited) {

  queue<int> q;
  q.push(s);

  visited[s] = 1;

  int size = 1;

  while (!q.empty()) {

    int t = q.front();
    q.pop();

    for (auto x : adj[t]) {
      if (visited[x] == 0) {
        q.push(x);
        visited[x] = 1;
        size++;
      }
    }
  }

  return size;
}

void solve() {

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

  vector<vector<int>> adj(n + 1);

  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    // cout<<l<<r ;

    adj[l].push_back(r);
    adj[r].push_back(l);
  }

  vector<int> visited(n + 1);

  int res = 0;

  for (int i = 1; i <= n; i++) {

    if (visited[i] == 0) {
      int answer = bfs(i, adj, visited);
      res = max(res, answer);
    }
  }

  cout << res;
}
int main() {

  int y;

  y = 1;
  // cin>>y ;

  while (y--) {
    solve();
  }
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
using namespace std;
vector<bool> seen;
int dfs(const vector<vector<int>> &G, int v) {
  int size = 1, p = 0;
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue; // next_v が探索済だったらスルー
                // dfs(G, next_v); // 再帰的に探索
    p = dfs(G, next_v);
    size += p;
  }
  return size;
}
int main() {
  int n, i, j, m, ans = 0;
  int a[200010], b[200010];
  cin >> n >> m;
  vector<vector<int>> group(n);
  seen.assign(n, false);
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i] -= 1;
    b[i] -= 1;
    group.at(a[i]).push_back(b[i]);
    group.at(b[i]).push_back(a[i]);
  }
  for (i = 0; i < n; i++) {
    if (seen[i] == false) {
      // ans=dfs(group,i);
      ans = max(ans, dfs(group, i));
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
using namespace std;
vector<bool> seen;
int dfs(const vector<vector<int>> &G, int v) {
  int size = 1, p = 0;
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue; // next_v が探索済だったらスルー
                // dfs(G, next_v); // 再帰的に探索
    p = dfs(G, next_v);
    size += p;
  }
  return size;
}
int main() {
  int n, i, j, m, ans = 0;
  int a[100000], b[100000];
  cin >> n >> m;
  vector<vector<int>> group(n);
  seen.assign(n, false);
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i] -= 1;
    b[i] -= 1;
    group.at(a[i]).push_back(b[i]);
    group.at(b[i]).push_back(a[i]);
  }
  for (i = 0; i < n; i++) {
    if (seen[i] == false) {
      // ans=dfs(group,i);
      ans = max(ans, dfs(group, i));
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

int par[200000], siz[200000];

int find(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find(par[a]);
}

void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  par[b] = a;
  siz[a] += siz[b];
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    siz[i] = 1;
  }
  int ans = 1;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a - 1, b - 1);
    ans = max(ans, siz[par[a - 1]]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int par[200000], siz[200000];

int find(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find(a);
}

void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  par[b] = a;
  siz[a] += siz[b];
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    siz[i] = 1;
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a - 1, b - 1);
    ans = max(ans, siz[a - 1]);
  }
  cout << ans << endl;
  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 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: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class UnionFind {
private:
  vector<int> par;

public:
  UnionFind(int N) { par.resize(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[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

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

  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 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;

  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++)

struct UnionFind {
  vector<int> Parent;
  vector<int> Size;

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

  int root(int A) {
    if (Parent[A] == A)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  bool merge(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (Size[A] < Size[B])
      swap(A, B);

    Size[A] += Size[B];
    Parent[B] = A;
    return true;
  }

  bool issame(int A, int B) { //
    return root(A) == root(B);
  }

  int findsize(int A) { return Size[root(A)]; }
};

signed main() {
  int n, m;
  cin >> n >> m;

  UnionFind Tree(n);
  vector<int> a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Tree.merge(a[i], b[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int s = Tree.findsize(i);
    ans = max(ans, s);
  }

  cout << ans << '\\n';
}


Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> Parent;
  vector<int> Size;

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

  int root(int A) {
    if (Parent[A] == A)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  bool merge(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (Size[A] < Size[B])
      swap(A, B);

    Size[A] += Size[B];
    Parent[B] = A;
    return true;
  }

  bool issame(int A, int B) { //
    return root(A) == root(B);
  }

  int findsize(int A) { return Size[root(A)]; }
};

signed main() {
  int n, m;
  cin >> n >> m;

  UnionFind Tree(n);
  vector<int> a(n), b(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Tree.merge(a[i], b[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int s = Tree.findsize(i);
    ans = max(ans, s);
  }

  cout << ans << '\\n';
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int SZ = 2 * 1e5 + 1;

vector<int> parent(SZ, -1);

int root(int x) { return parent[x] == -1 ? x : root(parent[x]); }

void merge(int a, int b) {
  int x = root(a);
  int y = root(b);
  if (x == y)
    return;
  if (x < y) {
    parent[y] = x;
  } else {
    parent[x] = y;
  }
}

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

  int N, M;
  cin >> N >> M;
  set<pair<int, int>> inp;
  set<int> st;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    inp.emplace(a, b);
    st.insert(a);
    st.insert(b);
  }

  for (const pair<int, int> &p : inp) {
    int a = p.first, b = p.second;
    merge(a, b);
  }

  int ans = 1;
  vector<int> cnt(N + 1, 0);
  for (int f : st) {
    int idx = root(f);
    cnt[idx]++;
    ans = max(ans, cnt[idx]);
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

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

const int SZ = 2 * 1e5 + 1;

vector<vector<int>> fg(SZ);

vector<int> parent(SZ, -1);

int root(int x) { return parent[x] == -1 ? x : root(parent[x]); }

void merge(int a, int b) {
  int x = root(a);
  int y = root(b);
  if (x == y)
    return;
  if (x < y) {
    parent[y] = x;
  } else {
    parent[x] = y;
  }
}

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

  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    fg[a].push_back(b);
    fg[b].push_back(a);
    merge(a, b);
  }

  int ans = 0;
  vector<int> cnt(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    int idx = root(i);
    cnt[idx]++;
    ans = max(ans, cnt[idx]);
  }
  cout << ans << "\\n";
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int getParent(vector<int> &parent, int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = getParent(parent, parent[x]);
}

void uniteParent(vector<int> &parent, int a, int b) {
  a = getParent(parent, a);
  b = getParent(parent, b);
  if (a == b)
    return;
  if (a > b)
    swap(a, b);
  // 항상 a < b
  parent[a] += parent[b];
  parent[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> parent(n + 1, -1);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    uniteParent(parent, a, b);
  }

  int cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    cnt = max(cnt, -parent[i]);
  }
  cout << cnt;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int getParent(vector<int> &parent, int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = getParent(parent, parent[x]);
}

void uniteParent(vector<int> &parent, int a, int b) {
  a = getParent(parent, a);
  b = getParent(parent, b);
  if (a == b)
    return;
  if (a > b)
    swap(a, b);
  // 항상 a < b
  parent[a] += parent[b];
  parent[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> parent(n + 1, -1);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    uniteParent(parent, a, b);
  }

  int cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    cnt = max(cnt, -parent[parent[i]]);
  }
  cout << cnt;

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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\\n'
#define maxn 200010
const int MOD = 1000000007;

int visited[maxn];
set<int> g[maxn];

int dfs(int u) {
  int cur = 1;
  visited[u] = true;
  for (auto x : g[u]) {
    if (!visited[x]) {
      cur += dfs(x);
    }
  }
  return cur;
}

void solve() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;

    g[a].insert(b);
    g[b].insert(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\\n'
#define maxn 100010
const int MOD = 1000000007;

int visited[maxn];
set<int> g[maxn];

int dfs(int u) {
  int cur = 1;
  visited[u] = true;
  for (auto x : g[u]) {
    if (!visited[x]) {
      cur += dfs(x);
    }
  }
  return cur;
}

void solve() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;

    g[a].insert(b);
    g[b].insert(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <math.h>
#include <vector>;
using namespace std;

vector<int> r;

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[y] += r[x];
  r[x] = y;
  return true;
}

int main() {

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

  r = vector<int>(n, -1);

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

  int ans = -1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, -r[root(i)]);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <math.h>
#include <vector>;
using namespace std;

vector<int> uf;

int find(int x) {
  if (uf[x] == x)
    return x;
  return find(uf[x]);
}

int unite(int x, int y) {
  int rx = find(x);
  int ry = find(y);
  uf[rx] = uf[ry];
  return 0;
}

int main() {

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

  uf = vector<int>(n);
  vector<int> fr(n);
  for (int i = 0; i < n; i++) {
    uf[i] = i;
  }

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

  int ans = -1;
  for (int i = 0; i < n; i++) {
    fr[find(i)]++;
  }

  sort(fr.begin(), fr.end());
  cout << fr[n - 1] << endl;

  return 0;
}