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

#include <bits/stdc++.h>

using namespace std;

#define int64 long long
const int mod = (int)1e9 + 7;

const int nax = 2e5 + 5;
vector<int> par(nax), siz(nax);

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

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

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i, siz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    union_set(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == par[i]) {
      ans = max(ans, siz[i]);
    }
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int64 long long
const int mod = (int)1e9 + 7;

const int nax = 1e5 + 5;
vector<int> par(nax), siz(nax);

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

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

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i, siz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    union_set(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == par[i]) {
      ans = max(ans, siz[i]);
    }
  }
  cout << ans;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int back(int tree[], int start) {
  int root;
  if (tree[start] == -1)
    return start;
  root = back(tree, tree[start]);
  tree[start] = root;
  return root;
}

int main() {
  int n, m;
  cin >> n >> m;
  int h1[n], h2[n];
  rep(i, n) {
    h1[i] = -1;
    h2[i] = 0;
  }
  int a, b, aroot, broot;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    aroot = back(h1, a);
    broot = back(h1, b);
    if (aroot != broot)
      h1[aroot] = broot;
  }
  rep(i, n) h2[back(h1, i)]++;
  int mm = 0;
  rep(i, n) mm = max(mm, h2[i]);
  cout << mm;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int back(int tree[], int start) {
  int root;
  if (tree[start] == -1)
    return start;
  root = back(tree, tree[start]);
  tree[start] = root;
  return root;
}

int main() {
  int n, m;
  cin >> n >> m;
  int h1[n], h2[n];
  rep(i, n) {
    h1[i] = -1;
    h2[i] = 0;
  }
  int a, b, aroot, broot;
  rep(i, m) {
    cin >> a >> b;
    aroot = back(h1, a);
    broot = back(h1, b);
    if (aroot != broot)
      h1[aroot] = broot;
  }
  rep(i, n) h2[back(h1, i)]++;
  int mm = 0;
  rep(i, n) mm = max(mm, h2[i]);
  cout << mm;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<int> g[200005];

void dfs(int v, int &ans, vector<bool> seen) {
  seen[v] = true;

  for (int next_v : g[v]) {
    if (seen[next_v] == true)
      continue;
    ans++;
    dfs(next_v, ans, seen);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> fre(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> dist(n + 1, -1);
  queue<int> que;

  int ans = 0;

  for (int i = 1; i <= n; i++) {
    if (dist[i] != -1)
      continue;
    dist[i] = 0;
    que.push(i);
    int count = 1;
    while (!que.empty()) {
      int v = que.front();
      que.pop();

      for (auto nv : g[v]) {
        if (dist[nv] != -1)
          continue;
        dist[nv] = dist[v] + 1;
        que.push(nv);
        count++;
      }
    }
    if (ans < count)
      ans = count;
  }

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


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<int> g[200005];

void dfs(int v, int &ans, vector<bool> seen, int d = 1) {
  seen[v] = true;
  for (int next_v : g[v]) {
    if (seen[next_v] == true)
      continue;
    dfs(next_v, ans, seen, d + 1);
  }
  if (ans < d)
    ans = d;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> aa(m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    aa[i] = a;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<bool> seen;

  int ans = 0;
  for (int i = 0; i < m; i++) {
    seen.assign(n + 1, false);
    dfs(aa[i], ans, seen);
  }

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

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

// union by size + path having
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; 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() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  vector<int> A(M, 0);
  vector<int> B(M, 0);

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

  for (int i = 0; i < M; ++i) {
    uf.merge(A.at(i) - 1, B.at(i) - 1);
  }

  ll cnt = 0;
  for (int i = 0; i < N; ++i) {
    ll t = uf.size(i);
    cnt = max(cnt, t);
  }

  cout << cnt << endl;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

// union by size + path having
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; 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() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  vector<int> A(M);
  vector<int> B(M);

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

  for (int i = 0; i < M; ++i) {
    uf.merge(A.at(i), B.at(i));
  }

  ll cnt = 0;
  for (int i = 0; i < M; ++i) {
    ll t = uf.size(i);
    cnt = max(cnt, t);
  }

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

#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;

int visited[200005];
vector<int> ar[200005];
int cc;

void dfs(int employee) {
  visited[employee] = 1;
  cc++;
  for (int emp : ar[employee]) {
    if (!visited[emp]) {
      dfs(emp);
    }
  }
}

int main() {
  // your code goes here
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    visited[i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    ar[a].push_back(b);
    ar[b].push_back(a);
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      cc = 0;
      dfs(i);
      ans = max(ans, cc);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;

long visited[100001];
vector<int> ar[100001];
long long cc;

void dfs(long long employee) {
  visited[employee] = 1;
  cc++;
  for (long long emp : ar[employee]) {
    if (!visited[emp]) {
      dfs(emp);
    }
  }
}

int main() {
  long long n, m, a, b;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    visited[i] = 0;
  }
  for (long long i = 1; i <= m; i++) {
    cin >> a >> b;
    ar[a].push_back(b);
    ar[b].push_back(a);
  }
  long long ans = -1;
  for (long long i = 1; i <= n; i++) {
    if (!visited[i]) {
      cc = 0;
      dfs(i);
      ans = max(ans, cc);
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[300001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}
int main() {
  int a, b, ans = 0;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a = find(a);
    b = find(b);
    if (a != b) {
      merge(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].parent == i) {
      ans = max(ans, g[i].node);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[100001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}
int main() {
  int a, b, ans = 0;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a = find(a);
    b = find(b);
    if (a != b) {
      merge(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].parent == i) {
      ans = max(ans, g[i].node);
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using namespace std;
vector<vector<int>> adj;
vector<bool> vis;
int bfs(int s) {
  vis[s] = true;
  queue<int> q;
  q.push(s);
  int cnt = 1;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto i : adj[t]) {
      if (!vis[i]) {
        vis[i] = true;
        q.push(i);
        cnt++;
      }
    }
  }
  return cnt;
}
void solve() {
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  vis.resize(n + 1);
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      cnt = max(cnt, bfs(i));
    }
  }
  cout << cnt;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using namespace std;
vector<int> adj[100005];
bool vis[100005];
int bfs(int s) {
  vis[s] = true;
  queue<int> q;
  q.push(s);
  int cnt = 1;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto i : adj[t]) {
      if (!vis[i]) {
        vis[i] = true;
        q.push(i);
        cnt++;
      }
    }
  }
  return cnt;
}
void solve() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      cnt = max(cnt, bfs(i));
    }
  }
  cout << cnt;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mxn = 2e5 + 5;
// check for forloop
// intialize variable
// overflow
// go for easy solution
#define mod 1000000007
ll INF = 1000000000000000005LL;
#define endl '\\n'
void rishabh() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[mxn];
bool vis[mxn];
int dfs(int i) {
  vis[i] = true;
  int temp = 0;
  for (auto x : adj[i]) {
    if (!vis[x])
      temp = temp + dfs(x);
  }
  return temp + 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      int temp = dfs(i);
      ans = max(ans, temp);
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mxn = 1e5 + 5;
// check for forloop
// intialize variable
// overflow
// go for easy solution
#define mod 1000000007
ll INF = 1000000000000000005LL;
#define endl '\\n'
void rishabh() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[mxn];
bool vis[mxn];
int dfs(int i) {
  vis[i] = true;
  int temp = 0;
  for (auto x : adj[i]) {
    if (!vis[x])
      temp = temp + dfs(x);
  }
  return temp + 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      int temp = dfs(i);
      ans = max(ans, temp);
    }
  }
  cout << ans;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int n, m;
int f[maxn], mp[maxn];
int find(int x) {
  int y = x;
  while (x != f[x])
    x = f[x];
  while (y != x)
    f[y] = x, y = f[y];
  return x;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    int root1 = find(x), root2 = find(y);
    if (root1 != root2)
      f[root2] = root1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    mp[find(i)]++, ans = max(ans, mp[find(i)]);
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int n, m;
int f[maxn];
int find(int x) {
  while (x != f[x])
    x = f[x];
  return x;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    int root1 = find(x), root2 = find(y);
    if (root1 != root2)
      f[root2] = root1;
  }
  int ans = 0;
  map<int, int> mp;
  for (int i = 1; i <= n; i++)
    mp[find(i)]++, ans = max(ans, mp[find(i)]);
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> p;
  vector<int> r;
  UnionFind(int n) {
    p.resize(n, -1);
    r.resize(n, 1);
  }
  int find(int x) {
    if (p[x] == -1)
      return x;
    else
      return p[x] = find(p[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    p[x] = y;
    r[y] += r[x];
  }
  int size(int x) { return r[x]; }
};

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

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  int maxi = 0;

  rep(i, n) maxi = max(maxi, uf.size(i));
  cout << maxi << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> p;
  vector<int> r;
  UnionFind(int n) {
    p.resize(n, -1);
    r.resize(n, 1);
  }
  int find(int x) {
    if (p[x] == -1)
      return x;
    else
      return p[x] = find(p[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (r[x] > r[y])
      swap(x, y);
    if (r[x] == r[y])
      r[y]++;
    p[x] = y;
  }
};

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

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  map<int, int> num;
  int maxi = 0;
  for (int i = 1; i <= n; i++) {
    num[uf.find(i)]++;
  }
  for (auto p : num) {
    maxi = max(maxi, p.second);
  }
  cout << maxi << endl;

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

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

int parent_find(int x, vector<int> &parent) {
  while (parent[x] != x) {
    x = parent[x];
  }
  return x;
}

void combine(int x, int y, vector<int> &parent, vector<int> &rank) {
  int t = parent_find(x, parent);
  int w = parent_find(y, parent);
  if (t != w) {

    if (rank[w] > rank[t]) {
      parent[t] = w;
      rank[w] += rank[t];
    } else {
      parent[w] = t;
      rank[t] += rank[w];
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> group(n + 1, 0);
  vector<int> rank(n + 1, 1);
  for (int i = 0; i <= n; i++)
    group[i] = i;

  int t;
  cin >> t;
  while (t--) {
    int x, y;
    cin >> x >> y;
    combine(x, y, group, rank);
  }

  cout << *max_element(rank.begin(), rank.end()) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int parent_find(int x, vector<int> &parent) {
  while (parent[x] != x) {
    x = parent[x];
  }
  return x;
}

void combine(int x, int y, vector<int> &parent, vector<int> &rank) {
  int t = parent_find(x, parent);
  int w = parent_find(y, parent);
  if (t != w) {
    parent[t] = w;
    rank[w] += rank[t];
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> group(n + 1, 0);
  vector<int> rank(n + 1, 1);
  for (int i = 0; i <= n; i++)
    group[i] = i;

  int t;
  cin >> t;
  while (t--) {
    int x, y;
    cin >> x >> y;
    combine(x, y, group, rank);
  }

  cout << *max_element(rank.begin(), rank.end()) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007
using Graph = vector<vector<int>>;
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
vector<int> seen(200010);
void dfs(const Graph &G, int v, int count) {
  seen[v] = count;
  for (int next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, count); // 再帰的に探索
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  Graph friends(N);
  rep(i, M) {
    int x, y, z = 0;
    cin >> x >> y;
    /*   rep(j,friends[x-1].size()){
         if(friends[x-1][j]==y-1)z=1;
       }
       if(z==0){*/
    friends[x - 1].push_back(y - 1);
    friends[y - 1].push_back(x - 1);
    //}
  }
  int count = 1;
  //  seen.assign(N, 0);
  for (int v = 0; v < N; ++v) {
    if (seen[v])
      continue;             // v が探索済みだったらスルー
    dfs(friends, v, count); // v が未探索なら v を始点とした DFS を行う
    ++count;
  }
  int ans = 0;
  vector<int> aaa(count, 0);
  rep(i, N) { aaa[seen[i]]++; }
  rep(i, count) { ans = max(ans, aaa[i]); }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007
using Graph = vector<vector<int>>;
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
vector<int> seen;
void dfs(const Graph &G, int v, int count) {
  seen[v] = count;
  for (int next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, count); // 再帰的に探索
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  Graph friends(N);
  rep(i, M) {
    int x, y, z = 0;
    cin >> x >> y;
    /*   rep(j,friends[x-1].size()){
         if(friends[x-1][j]==y-1)z=1;
       }
       if(z==0){*/
    friends[x - 1].push_back(y - 1);
    friends[y - 1].push_back(x - 1);
    //}
  }
  int count = 1;
  seen.assign(N, 0);
  for (int v = 0; v < N; ++v) {
    if (seen[v])
      continue;             // v が探索済みだったらスルー
    dfs(friends, v, count); // v が未探索なら v を始点とした DFS を行う
    ++count;
  }
  int ans = 0;
  for (int i = 1; i < count; i++) {
    int x = 0;
    rep(j, N) {
      if (seen[j] == i)
        x++;
    }
    ans = max(ans, x);
  }
  cout << ans;
}