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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct Node {
  int pos;
  int flag;
  std::vector<int> connects;
};

Node nodes[200010];
int n, m;

int curFlag;
int maxCount;

int checkAround(int pos, int curFlag) {
  int count = 0;
  std::queue<int> q;
  q.push(pos);
  nodes[pos].flag = curFlag;
  count++;

  int newPos;
  while (!q.empty()) {
    int curPos = q.front();
    q.pop();
    if (nodes[curPos].flag != curFlag) {
      nodes[curPos].flag = curFlag;
      count++;
    }

    for (int j = 0; j < nodes[curPos].connects.size(); j++) {
      int newPos = nodes[curPos].connects[j];

      if (nodes[newPos].flag == -1) {
        nodes[newPos].flag = curFlag;
        count++;
        q.push(newPos);
      }
    }
  }

  return count;
}

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

  for (int i = 1; i <= n; i++) {
    nodes[i].flag = -1;
    nodes[i].pos = i;
  }

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);

    nodes[u].connects.push_back(v);
    nodes[v].connects.push_back(u);
  }

  maxCount = 0;
  curFlag = 1;
  for (int i = 1; i <= n; i++) {
    if (nodes[i].flag == -1) {
      int count = checkAround(i, curFlag);
      if (count > maxCount)
        maxCount = count;
      curFlag++;
    }
  }

  printf("%d\\n", maxCount);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct Node {
  int pos;
  int flag;
  std::vector<int> connects;
};

Node nodes[200010];
int n, m;

int curFlag;
int maxCount;

int checkAround(int pos, int curFlag) {
  int count = 0;
  std::queue<int> q;
  q.push(pos);
  nodes[pos].flag = curFlag;
  count++;

  int newPos;
  while (!q.empty()) {
    int curPos = q.front();
    q.pop();
    if (nodes[curPos].flag != curFlag) {
      nodes[curPos].flag = curFlag;
      count++;
    }

    for (int j = 0; j < nodes[curPos].connects.size(); j++) {
      int newPos = nodes[pos].connects[j];

      if (nodes[newPos].flag == -1) {
        nodes[newPos].flag = curFlag;
        count++;
        q.push(newPos);
      }
    }
  }

  return count;
}

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

  for (int i = 1; i <= n; i++) {
    nodes[i].flag = -1;
    nodes[i].pos = i;
  }

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);

    nodes[u].connects.push_back(v);
    nodes[v].connects.push_back(u);
  }

  maxCount = 0;
  curFlag = 1;
  for (int i = 1; i <= n; i++) {
    if (nodes[i].flag == -1) {
      int count = checkAround(i, curFlag);
      if (count > maxCount)
        maxCount = count;
      curFlag++;
    }
  }

  printf("%d\\n", maxCount);

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

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

#define ll long long
#define ar array

const int mxN = 2e5 + 5, INF = 999999999;
const int modr = 1e9 + 7;
int n, m, a[mxN];

/* 1. If you can't keep solution in mind, then visualize it on a paper.
 * 2. Try to identify the type of problem - DP, Greedy, Graph, Constructive,
 * 	  Prefix/Suffix, Difference Array, BIT/Segment Tree, etc.
 * 3. Look at the problems from different perspectives - reverse the problem,
 * 	  add instead of delete (maybe?)
 * 4. Divide difficult solutions into cases and/or sequences.
 * 5. Use math rules to solve Nested Summation.
 * 6. Optimisation problems are generally solved with DP, greedy, binary search
 * or brute-force.
 * ------------------------------------------------------------------
 */

bool vis[mxN];
vector<int> adj[mxN];
int t;
void dfs(int s) {
  vis[s] = true;
  t++;
  for (auto child : adj[s]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

void solve() {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i <= n; i++) {
    adj[i].clear();
    vis[i] = false;
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int maxm = -1;
  for (int i = 0; i <= n; i++) {
    if (!vis[i]) {
      t = 0;
      dfs(i);
      maxm = max(maxm, t);
    }
  }

  cout << maxm;
}

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

  int t = 1;

  for (int i = 1; i <= t; ++i) {
    //	cout << "Case #" << i << ": ";
    solve();
  }
}


Error: WA

Fixed submission:

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

#define ll long long
#define ar array

const int mxN = 1e5, INF = 999999999;
const int modr = 1e9 + 7;
int n, m, a[mxN];

/* 1. If you can't keep solution in mind, then visualize it on a paper.
 * 2. Try to identify the type of problem - DP, Greedy, Graph, Constructive,
 * 	  Prefix/Suffix, Difference Array, BIT/Segment Tree, etc.
 * 3. Look at the problems from different perspectives - reverse the problem,
 * 	  add instead of delete (maybe?)
 * 4. Divide difficult solutions into cases and/or sequences.
 * 5. Use math rules to solve Nested Summation.
 * 6. Optimisation problems are generally solved with DP, greedy, binary search
 * or brute-force.
 * ------------------------------------------------------------------
 */

bool vis[mxN];
vector<int> adj[mxN];
int t;
void dfs(int s) {
  vis[s] = true;
  t++;
  for (auto child : adj[s]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

void solve() {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i <= n; i++) {
    adj[i].clear();
    vis[i] = false;
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int maxm = -1;
  for (int i = 0; i <= n; i++) {
    if (!vis[i]) {
      t = 0;
      dfs(i);
      maxm = max(maxm, t);
    }
  }

  cout << maxm;
}

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

  int t = 1;

  for (int i = 1; i <= t; ++i) {
    //	cout << "Case #" << i << ": ";
    solve();
  }
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

class union_find {
private:
  vector<u64> roots;

public:
  union_find(const u64 numel) {
    roots.resize(numel);
    iota(roots.begin(), roots.end(), 0);
  }

  u64 root(u64 x) {
    if (roots[x] != x) {
      roots[x] = root(roots[x]);
    }
    return roots[x];
  }

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

  void unite(u64 x, u64 y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      roots[x] = y;
    };
  }
};

int main() {
  u64 N, M;
  cin >> N >> M;

  union_find friends(N);

  for (u64 i = 0; i < M; i++) {
    u64 A, B;
    cin >> A >> B;
    A--;
    B--;

    friends.unite(A, B);
  }

  vector<u64> num_friends(N);
  for (u64 i = 0; i < N; i++) {
    num_friends[friends.root(i)]++;
  }

  u64 ans = *max_element(num_friends.begin(), num_friends.end());

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

u64 dfs(const vector<set<u64>> &AB, vector<bool> &visited, const u64 i) {
  if (visited[i]) {
    return 0;
  } else {
    visited[i] = true;
    u64 fellows = 1;

    for (const u64 &fid : AB[i]) {
      fellows += dfs(AB, visited, fid);
    }

    return fellows;
  }
}

int main() {
  u64 N, M;
  cin >> N >> M;

  vector<set<u64>> AB(N, set<u64>());

  for (u64 i = 0; i < M; i++) {
    u64 A, B;
    cin >> A >> B;
    A--;
    B--;
    AB[A].insert(B);
    AB[B].insert(A);
  }

  vector<bool> visited(M);
  u64 ans = 0;

  for (u64 i = 0; i < N; i++) {
    u64 tans = dfs(AB, visited, i);
    ans = max(ans, tans);
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL
int n, m;
// vector<int> a,b;
vector<int> gidx;
vector<set<int>> g;

void g_merge(int a, int b) {
  // printf("merge(%d, %d)\\n", a, b);
  int ga = gidx[a];
  int gb = gidx[b];
  // printf("ga = %d, gb = %d\\n", ga, gb);
  if (ga == gb)
    return;

  if (g[ga].size() > g[gb].size()) {
    for (auto it = g[gb].begin(); it != g[gb].end(); it++) {
      // printf("gidx[%d] = %d\\n", *it , gidx[a]);
      gidx[*it] = ga;
      // printf("g[gidx[%d]].insert(%d)\\n", a, *it);
      g[ga].insert(*it);
    }
    g[gb].clear();
  } else {
    for (auto it = g[ga].begin(); it != g[ga].end(); it++) {
      // printf("gidx[%d] = %d\\n", *it , gidx[a]);
      gidx[*it] = gb;
      // printf("g[gidx[%d]].insert(%d)\\n", a, *it);
      g[gb].insert(*it);
    }
    g[ga].clear();
  }
}

int main() {
  int a, b;
  ll ans;

  cin >> n >> m;

  g.resize(n + 1);
  gidx.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    set<int> gx;
    gx.insert(i);
    g[i] = gx;
    gidx[i] = i;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g_merge(a, b);
  }

  ans = 0;
  for (auto it = g.begin(); it != g.end(); it++) {
    if (ans < (ll)it->size())
      ans = it->size();
  }

  cout << ans << endl;
  return 0;
}
/*
  {1,2,3,4}

  {1,3,4}{5,9}{2,6}
  */


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL
int n, m;
// vector<int> a,b;
vector<int> gidx;
vector<set<int>> g;

void g_merge(int a, int b) {
  // printf("merge(%d, %d)\\n", a, b);
  int ga = gidx[a];
  int gb = gidx[b];
  // printf("ga = %d, gb = %d\\n", ga, gb);
  if (ga == gb)
    return;

  for (auto it = g[gb].begin(); it != g[gb].end(); it++) {
    // printf("gidx[%d] = %d\\n", *it , gidx[a]);
    gidx[*it] = ga;
    // printf("g[gidx[%d]].insert(%d)\\n", a, *it);
    g[ga].insert(*it);
  }
  g[gb].clear();
}

int main() {
  int a, b;
  ll ans;

  cin >> n >> m;

  g.resize(n + 1);
  gidx.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    set<int> gx;
    gx.insert(i);
    g[i] = gx;
    gidx[i] = i;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g_merge(a, b);
  }

  ans = 0;
  for (auto it = g.begin(); it != g.end(); it++) {
    if (ans < (ll)it->size())
      ans = it->size();
  }

  cout << ans << endl;
  return 0;
}
/*
  {1,2,3,4}

  {1,3,4}{5,9}{2,6}
  */
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll Size = 1e6 + 7;
ll parent[1000010];
ll Rank[1000010];
ll SIZE[1000010];
ll mx = 0;
void make_set() {
  for (ll i = 0; i < Size; i++) {
    parent[i] = i;
    Rank[i] = 0;
    SIZE[i] = 1;
  }
}
ll Find_set(ll v) {
  if (parent[v] == v)
    return v;
  return parent[v] = Find_set(parent[v]);
}
void union_set(ll a, ll b) {
  a = Find_set(a);
  b = Find_set(b);
  if (a != b) {
    if (SIZE[a] < SIZE[b])
      swap(a, b);
    parent[b] = a;
    SIZE[a] += SIZE[b];
    // cout<<SIZE[a]<<endl;
    mx = max(SIZE[a], mx);
  }
}
int main() {
  ll n, m;
  ll i, j, k;
  make_set();
  ll a, b;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    union_set(a, b);
  }
  if (mx)
    cout << mx << endl;
  else
    cout << "1" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll a, b, c;
  ll n, m;
  cin >> n >> m;
  vector<ll> v[m + 10];
  ll mx = 0, i;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
    a = v[a].size();
    b = v[b].size();
    mx = max(a, mx);
    mx = max(b, mx);
  }
  cout << min(mx + 1, n) << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
#define ll long long
#define inf 0x3f3f
#define mod 1000000007
#define endl '\\n'
int fa[maxn];
int find(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = find(fa[x]);
}
void Union(int x, int y) {
  int fx = find(x), fy = find(y);
  fa[fx] = fy;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Union(x, y);
  }
  map<int, int> mp;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = find(i);
    mp[t]++;
    ans = max(ans, mp[t]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
#define ll long long
#define inf 0x3f3f
#define mod 1000000007
#define endl '\\n'
int fa[maxn];
int find1(int x) { return fa[x] == x ? x : find1(fa[x]); }
void Union(int x, int y) {
  int fx = find1(x), fy = find1(y);
  fa[fx] = fy;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Union(x, y);
  }
  map<int, int> mp;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = find1(i);
    mp[t]++;
    ans = max(ans, mp[t]);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
#define ll long long
#define inf 0x3f3f
#define mod 1000000007
#define endl '\\n'
int fa[maxn];
int find1(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = find1(fa[x]);
}
void Union(int x, int y) {
  int fx = find1(x), fy = find1(y);
  fa[fy] = fx;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Union(x, y);
  }
  map<int, int> mp;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = find1(i);
    mp[t]++;
    ans = max(ans, mp[t]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
#define ll long long
#define inf 0x3f3f
#define mod 1000000007
#define endl '\\n'
int fa[maxn];
int find(int x) { return fa[x] == x ? x : find(fa[x]); }
void Union(int x, int y) {
  int fx = find(x), fy = find(y);
  fa[fx] = fy;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Union(x, y);
  }
  map<int, int> mp;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = find(i);
    mp[t]++;
    ans = max(ans, mp[t]);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
using namespace std;
const int MOD = 1000000007;
const long long INF = 1LL << 60;
// union by size + path having
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_ + 1), siz(sz_ + 1, 1LL) {
    for (ll i = 1; i <= sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
    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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  UnionFind tree(n);
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    tree.merge(x, y);
  }
  map<ll, ll> f;
  for (ll i = 1; i <= n; i++) {
    ll num = tree.root(i);
    f[num]++;
  }

  ll ans = -1;
  auto begin = f.begin();
  auto end = f.end();
  for (auto iter = begin; iter != end; iter++) {
    ans = max(ans, iter->second);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
using namespace std;
const int MOD = 1000000007;
const long long INF = 1LL << 60;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 1; i < N + 1; 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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  UnionFind tree(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x, y);
  }
  map<int, int> f;
  for (int i = 1; i <= n; i++) {
    int num = tree.root(i);
    f[num]++;
  }

  int ans = -1;
  auto begin = f.begin();
  auto end = f.end();
  for (auto iter = begin; iter != end; iter++) {
    ans = max(ans, iter->second);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

class UnionFind {
private:
  ll num;
  vector<ll> par;
  vector<ll> sizes;
  vector<ll> rank;

public:
  UnionFind(ll n) { init(n); }

  ~UnionFind() {}

  void init(ll n) {
    num = n;
    par.resize(n);
    sizes.resize(n);
    rank.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }

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

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    ll temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  ll get_size(ll x) { return sizes[root(x)]; }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  ll max_size = 0;
  rep(i, n) { max_size = max(max_size, u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

class UnionFind {
private:
  ll num;
  vector<ll> par;
  vector<ll> sizes;
  vector<ll> rank;

public:
  UnionFind(ll n) { init(n); }

  ~UnionFind() {}

  ll init(ll n) {
    num = n;
    par.resize(n);
    sizes.resize(n);
    rank.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }

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

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    ll temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  ll get_size(ll x) { return sizes[root(x)]; }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  ll max_size = 0;
  rep(i, n) { max_size = max(max_size, u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
using ll = long long;

int num[200005] = {};

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

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  else {
    num[y] = x;
    return;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, n) num[i] = i;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    unite(min(a, b), max(a, b));
  }
  vector<int> c(n);
  rep(i, n) c[root(i)]++;
  int ans = 0;
  rep(i, n) ans = max(ans, c[i]);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;

int num[200005] = {};

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

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  else {
    num[x] = y;
    return;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, n) num[i] = i;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    unite(min(a, b), max(a, b));
  }
  vector<int> c(n);
  rep(i, n) c[root(i)]++;
  int ans = 0;
  rep(i, n) ans = max(ans, c[i]);
  cout << ans << endl;
  return 0;
}