alexjercan/bugnet huggingface dataset:
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<vector<int>> number(N, vector<int>(0));
  vector<bool> seen(N, false);
  vector<int> A(M);
  vector<int> B(M);

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  for (int i = 0; i < M; i++) {
    number[A[i] - 1].push_back(B[i] - 1);
    number[B[i] - 1].push_back(A[i] - 1);
  }

  int ans = 0;
  queue<int> stack;

  for (int i = 0; i < N; i++) {
    int count = 0;
    if (seen[i])
      continue;
    seen[i] = true;
    stack.push(i);
    while (!stack.empty()) {
      int now = stack.front();
      stack.pop();
      for (auto nv : number[now]) {
        if (!seen[nv]) {
          seen[nv] = true;
          stack.push(nv);
        }
      }
      count++;
    }
    ans = max(ans, count);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> number(N, vector<int>(0));
  vector<bool> seen(N, false);
  vector<int> A(M);
  vector<int> B(M);

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  for (int i = 0; i < M; i++) {
    number[A[i] - 1].push_back(B[i] - 1);
    number[B[i] - 1].push_back(A[i] - 1);
  }

  int ans = 0;
  vector<int> stack(0);

  for (int j = 0; j < N; j++) {
    if (seen[j])
      continue;
    stack.push_back(j);
    int count = 0;
    while (!stack.empty()) {
      int now = stack.back();
      seen[now] = true;
      stack.pop_back();
      for (int i = 0; i < number[now].size(); i++) {
        if (seen[number[now][i]])
          continue;
        stack.push_back(number[now][i]);
      }
      sort(stack.begin(), stack.end());
      stack.erase(unique(stack.begin(), stack.end()), stack.end());
      count++;
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MOD (ll)1000000007

vector<int> graph[200001], group[200001];
vector<pii> ans;
int scc[200001], sccnumber, dfsn[200001], dfsnumber, finished[200001];
stack<int> s;

int dfs(int node) {
  dfsn[node] = ++dfsnumber;
  s.push(node);

  int topmost = dfsn[node];
  for (int i = 0; i < graph[node].size(); i++) {
    if (!dfsn[graph[node][i]])
      topmost = min(topmost, dfs(graph[node][i]));
    else if (!finished[graph[node][i]])
      topmost = min(topmost, dfsn[graph[node][i]]);
  }

  if (topmost == dfsn[node]) {
    int t;
    sccnumber++;
    while (true) {
      t = s.top();
      s.pop();
      finished[t] = true;
      scc[t] = sccnumber;
      group[sccnumber].push_back(t);
      if (t == node)
        break;
    }
  }

  return topmost;
}

int main(void) {
  int n, m, a, b;
  scanf("%d %d", &n, &m);
  while (m--) {
    scanf("%d %d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!dfsn[i])
      dfs(i);

  int ans = 0;
  for (int i = 1; i <= sccnumber; i++)
    ans = max(ans, (int)group[i].size());
  printf("%d", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MOD (ll)1000000007

vector<int> graph[301], group[301];
vector<pii> ans;
int scc[301], sccnumber, dfsn[301], dfsnumber, finished[301];
bool adj[301][301], visited[301];
stack<int> s;

int dfs(int node) {
  dfsn[node] = ++dfsnumber;
  s.push(node);

  int topmost = dfsn[node];
  for (int i = 0; i < graph[node].size(); i++) {
    if (!dfsn[graph[node][i]])
      topmost = min(topmost, dfs(graph[node][i]));
    else if (!finished[graph[node][i]])
      topmost = min(topmost, dfsn[graph[node][i]]);
  }

  if (topmost == dfsn[node]) {
    int t;
    sccnumber++;
    while (true) {
      t = s.top();
      s.pop();
      finished[t] = true;
      scc[t] = sccnumber;
      group[sccnumber].push_back(t);
      if (t == node)
        break;
    }
  }

  return topmost;
}

int main(void) {
  int n, m, a, b;
  scanf("%d %d", &n, &m);
  while (m--) {
    scanf("%d %d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    if (!dfsn[i])
      dfs(i);

  int ans = 0;
  for (int i = 1; i <= sccnumber; i++)
    ans = max(ans, (int)group[i].size());
  printf("%d", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
#define ONF 0xc0c0c0c0
using namespace std;
typedef long long ll;
int par[10000005], num[10000005];
void init(int n) {
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    num[i] =
        1; // 这里是求最大集合的个数，如果求最后集合中权值最大为多少就让num数组初始化为最开始各个元素的权值。
  }
}
int get_par(int x) { return par[x] == x ? x : par[x] = get_par(par[x]); }
void doit(int x, int y) {
  int dx = get_par(x);
  int dy = get_par(y);
  if (dx != dy) {
    par[dx] = dy;
    num[dy] = num[dx] + num[dy];
  }
}
int main() {
  int n, x, y, m;
  cin >> n >> m;
  init(n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    doit(x, y);
  }
  int Max = 0;
  for (int i = 1; i <= n; i++)
    Max = max(Max, num[i]);
  cout << Max << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second

#define pb push_back
#define es erase
#define in insert

#define pii pair<ll, ll>
#define ll long long
#define lb long double

#define ioss                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define m_p(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define endl "\\n"
#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll maxn = 6e5 + 7;
ll n, a[maxn], fa[maxn], m, num[maxn];
int ifind(int x) {
  if (x == fa[x])
    return x;
  return fa[x] = ifind(fa[x]);
}
int cmb(int x, int y) {
  int fx = ifind(x), fy = ifind(y);
  if (fx != fy) {
    fa[fx] = fy;
    num[fy] += num[fx];
  }
}
int main() {
  ioss;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    num[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    cmb(x, y);
  }
  cout << *max_element(num + 1, num + 1 + n) << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dfs(int u, vector<vector<int>> &g, vector<int> &vis) {
  vis[u] = 1;
  int ans = 1;

  for (int v : g[u])
    if (!vis[v])
      ans += dfs(v, g, vis);

  return ans;
}

void solve() {
  int N, E;
  cin >> N >> E;

  vector<vector<int>> adj(N + 1);
  set<pair<int, int>> edges;

  for (int i = 0; i < E; i++) {
    int u, v;
    cin >> u >> v;

    if (edges.find({min(u, v), max(u, v)}) != edges.end())
      continue;

    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }

  int ans = 0;
  vector<int> visited(N + 1, 0);

  for (int i = 0; i < N; i++)
    if (!visited[i])
      ans = max(ans, dfs(i, adj, visited));

  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/***** author :  C0d1ngPhenomena *****/

#include <bits/stdc++.h>
#define endl "\\n"
#define ll long long int
#define TestCases                                                              \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define revrep(i, a, b) for (ll i = b - 1; i >= a; i--)
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp(x, y) make_pair(x, y)
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define alld(c) c.begin(), c.end(), greater<int>()
#define mem(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
#define pb push_back
using namespace std;

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

ll maxi = 0;
bool visited[1000000];
vector<ll> G[1000000];

void dfs(int x) {
  visited[x] = true;

  for (auto &i : G[x]) {
    if (visited[i] == false) {
      dfs(i);
      maxi++;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  ll m;
  cin >> n >> m;

  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    G[x].pb(y);
    G[y].pb(x);
  }

  ll cnt = -1;

  rep(i, 1, n + 1) {
    dfs(i);
    cnt = max(cnt, maxi);
    mem(visited, 0);
    maxi = 0;
  }
  cout << cnt + 1;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
int N = 1e5 + 9;
vector<vector<int>> vtx;
vector<bool> vis;
void dfs(int u, int &k) {
  k++;
  vis[u] = 1;
  for (auto v : vtx[u])
    if (!vis[v])
      dfs(v, k);
}
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vis.resize(n + 1);
  vtx.resize(n + 1);
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    vtx[u].pb(v);
    vtx[v].pb(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int k = 0;
      dfs(i, k);
      ans = max(ans, k);
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
int N = 1e5 + 9;
vector<vector<int>> vtx;
vector<bool> vis(N, 0);
void dfs(int u, int &k) {
  k++;
  vis[u] = 1;
  for (auto v : vtx[u])
    if (!vis[v])
      dfs(v, k);
}
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vtx.resize(n + 1);
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    vtx[u].pb(v);
    vtx[v].pb(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int k = 0;
      dfs(i, k);
      ans = max(ans, k);
    }
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

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 same(int x, int y) { return root(x) == root(y); }
};

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>
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
const int MOD = 1000000007;

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 same(int x, int y) { return root(x) == root(y); }
};

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 res = 0;
  for (int i = 1; i <= N; i++) {
    res = max(res, UF.size(i));
  }

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

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

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 same(int x, int y) { return root(x) == root(y); }
};

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

  // 友達集合を作る
  UnionFind UF(N + 1);
  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 = 1; i <= N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

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 same(int x, int y) { return root(x) == root(y); }
};

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 = 1; i <= N; i++) {
    ans = max(ans, UF.size(i));
  }

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

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define mes(a, b) memset((a), (b), sizeof((a)))
#define syio                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0);
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
typedef long double real;
typedef pair<int, int> pii;

const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 200033;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-' ? -1 : 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int n, m, f[mxn], cnt[mxn];
void init() {
  for (int i = 1; i <= n; ++i)
    f[i] = i, cnt[i] = 1;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void join(int u, int v) {
  int fu = find(u), fv = find(v);
  if (fu != fv) {
    f[fu] = f[fv];
    cnt[fv] += cnt[fu];
  }
}
int main() {
  syio;
  cin >> n >> m;
  init();
  int a, b;
  while (m--) {
    cin >> a >> b;
    join(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; ++i)
    if (find(i) == i)
      mx = max(mx, cnt[i]);
  cout << mx << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define mes(a, b) memset((a), (b), sizeof((a)))
#define syio                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0);
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
typedef long double real;
typedef pair<int, int> pii;

const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 200033;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-' ? -1 : 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int n, m, f[mxn], cnt[mxn];
void init() {
  for (int i = 1; i <= n; ++i)
    f[i] = i, cnt[i] = 1;
}
int find(int x) { return f[x] == x ? x : find(f[x]); }
void join(int u, int v) {
  int fu = find(u), fv = find(v);
  if (fu != fv) {
    f[fu] = f[fv];
    cnt[fv] += cnt[fu];
  }
}
int main() {
  syio;
  cin >> n >> m;
  init();
  int a, b;
  while (m--) {
    cin >> a >> b;
    join(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; ++i)
    if (find(i) == i)
      mx = max(mx, cnt[i]);
  cout << mx << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll par[200002];
ll rk[200002];
ll sz[200002];
void init(ll n) {
  for (ll i = 0; i < n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

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

void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rk[x] < rk[y]) {
    par[x] = y;
    sz[y] += sz[x];
  } else {
    par[y] = x;
    sz[x] += sz[y];
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

bool same(ll x, ll y) { return find(x) == find(y); }
int main() {
  ll n, m;
  cin >> n >> m;
  ll ans[20002];
  ll a[200002], b[200002];
  for (ll i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  init(n);
  ll c = 0;
  ll cnt;
  cnt = 0;
  for (ll i = m - 1; i >= 0; i--) {
    ans[i] = c;
    ll x = a[i], y = b[i];
    if (same(x, y)) {
      continue;
    }
    x = find(x), y = find(y);
    unite(x, y);
  }
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    mp[find(i + 1)]++;
  }
  ll max = 0;
  for (auto p : mp) {
    if (max < p.second) {
      max = p.second;
    }
  }
  cout << max << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int par[100002];
int rk[100002];
int sz[100002];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rk[x] < rk[y]) {
    par[x] = y;
    sz[y] += sz[x];
  } else {
    par[y] = x;
    sz[x] += sz[y];
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }
int main() {
  int n, m;
  cin >> n >> m;
  ll ans[100002];
  int a[100002], b[100002];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  init(n);
  ll c = 0;
  ll cnt;
  cnt = 0;
  for (int i = m - 1; i >= 0; i--) {
    ans[i] = c;
    int x = a[i], y = b[i];
    if (same(x, y)) {
      continue;
    }
    x = find(x), y = find(y);
    unite(x, y);
  }
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    mp[find(i + 1)]++;
  }
  ll max = 0;
  for (auto p : mp) {
    if (max < p.second) {
      max = p.second;
    }
  }
  cout << max << endl;

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

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

struct UnionFind {
  vector<int> tree;

  UnionFind(int n) : tree(n, -1) {}

  int root(int n) {
    if (tree[n] < 0)
      return n;
    return tree[n] = root(tree[n]);
  }

  void unite(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return;
    if (tree[a] > tree[b])
      swap(a, b);
    tree[a] += tree[b];
    tree[b] = a;
  }

  int size(int n) { return -tree[root(n)]; }
};

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


Error: TLE

Fixed submission:

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

struct UnionFind {
  vector<int> tree;

  UnionFind(int n) : tree(n) {
    for (size_t i = 0; i < n; i++) {
      tree[i] = i;
    }
  }

  int root(int n) {
    if (tree[n] == n)
      return n;
    return root(tree[n]);
  }

  void unite(int a, int b) {
    a = root(a);
    b = root(b);
    tree[a] = b;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (size_t i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  vector<int> roots(n, 0);
  for (int i = 0; i < n; i++) {
    uf.tree[i] = uf.root(uf.tree[i]);
    roots[uf.tree[i]]++;
  }
  cout << *max_element(roots.begin(), roots.end()) << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> vec(N, vector<ll>(0));
  ll ans = 0;
  for (ll i = 0; i < M; i++) {
    ll A, B;
    cin >> A >> B;
    vec[A - 1].push_back(B);
    vec[B - 1].push_back(A);
  }

  vector<bool> note(N, false);
  for (ll i = 0; i < N; i++) {
    if (!note[i]) {
      ll count = 1;
      queue<ll> S;
      S.push(i + 1);
      note[i] = true;

      while (!S.empty()) {
        ll index = S.front();
        S.pop();
        for (ll x : vec[index - 1]) {
          if (!note[x - 1]) {
            count++;
            note[x - 1] = true;
            S.push(x);
          }
        }
      }

      ans = max(ans, count);
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<bool>> vec(N, vector<bool>(N, false));
  for (ll i = 0; i < M; i++) {
    ll A, B;
    cin >> A >> B;
    vec[A - 1][B - 1] = true;
    vec[B - 1][A - 1] = true;
  }
  ll ans = 0;
  vector<bool> d(N, false);
  for (ll i = 1; i <= N; i++) {
    if (d[i - 1]) {
      continue;
    }

    set<ll> S;
    S.insert(i);
    for (ll j = 0; j < N; j++) {
      if (vec[i - 1][j]) {
        S.insert(j + 1);
      }
    }
    d[i - 1] = true;
    for (ll x : S) {
      if (!d[x - 1]) {
        d[x - 1] = true;
        for (ll k = 0; k < N; k++) {
          if (vec[x - 1][k]) {
            S.insert(k + 1);
          }
        }
      }
    }
    ll count = S.size();
    ans = max(count, ans);
  }

  cout << ans << endl;
}