alexjercan/bugnet huggingface dataset:
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

//
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n) for (size_t i = 0; i < (n); ++i)

struct UnionFind {
  std::vector<int> par; // par[a] = b ... a の親は b であるという意味.
                        // 根の場合は木の成分数に(-1)をかけた値.
  UnionFind(int n) : par(n, -1) {} // 親なし(-1)として初期化

  // x の根を返す
  int root(int x) {
    // 親がいない場合 x 自身が根
    if (par[x] < 0)
      return x;
    // 親がいる場合はその親を辿り、再帰的に根を求める
    else
      return par[x] = root(par[x]);
  }

  // x と y が同じ木に属しているかどうか
  bool isSame(int x, int y) { return root(x) == root(y); }

  // x が属している木と y が属している木の結合
  void merge(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx == ry)
      return; // 既に結合しているため

    if (par[rx] > par[ry])
      std::swap(x, y);
    par[rx] += par[ry]; // 深さを更新
    par[ry] = rx;       // x の根 rx に y の根 ry を結合
  }

  // x が属する木の深さを返す
  int size(int x) { return -par[root(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  REP(i, M) {
    cin >> A[i] >> B[i];
    --A[i], --B[i];
  }
  int ans = 0;
  UnionFind uf(N);
  REP(i, M) {
    if (!uf.isSame(A[i], B[i]))
      uf.merge(A[i], B[i]);
  }
  REP(i, N) { ans = max(ans, uf.size(i)); }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

//
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n) for (size_t i = 0; i < (n); ++i)

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  REP(i, M) {
    cin >> A[i] >> B[i];
    --A[i], --B[i];
  }
  vector<set<int>> G(N);
  REP(i, M) {
    G[A[i]].insert(B[i]);
    G[B[i]].insert(A[i]);
  }

  int ans = 0;
  REP(s, N) {
    vector<int> visited(N, 0);
    visited[s] = 1;
    stack<int> st;
    st.push(s);
    int cnt = 1;
    while (!st.empty()) {
      int v = st.top();
      st.pop();
      for (auto &nv : G[v]) {
        if (visited[nv] == 0) {
          visited[nv] = 1;
          ++cnt;
          st.push(nv);
        }
      }
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 2e5 + 5;
vector<set<ll>> adj(N);
int vis[N];
ll n, m, f, d, cnt;
void dfs(ll node) {
  vis[node] = 1;
  cnt++;
  for (auto v : adj[node]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].insert(v);
    adj[v].insert(u);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, cnt);
      cnt = 0;
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 2e5 + 5;
vector<ll> adj[N];
int vis[N];
ll n, m, f;
void dfs(int node) {
  vis[node] = 1;
  f++;
  for (auto v : adj[node]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  ll cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i] && sz(adj[i]) > 0) {
      dfs(i);
      cc++;
    }
  }
  cout << (f + cc - 1) / cc;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

using namespace std;
// #define int long long
using ll = long long;
const int MX = 1e6;
const ll inf = 1e13;
const int mod = 1e9 + 7;
struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return (root(x) == root(y)); }
  int size(int x) { return -d[root(x)]; }
};
signed main() {
  int n, m;
  cin >> n >> m;
  UnionFind t(n);
  if (m == 0) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, m) {
    int x, y;
    x--;
    y--;
    cin >> x >> y;
    t.unite(x, y);
  }
  int ans = 0;
  rep(i, n) { ans = max(t.size(i), ans); }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

using namespace std;
#define int long long
using ll = long long;
const int MX = 1e6;
const ll inf = 1e13;
const int mod = 1e9 + 7;
struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return (root(x) == root(y)); }
  int size(int x) { return -d[root(x)]; }
};
signed main() {
  int n, m;
  cin >> n >> m;
  UnionFind t(n);

  rep(i, m) {
    int x, y;
    x--;
    y--;
    cin >> x >> y;
    t.unite(x, y);
  }
  int ans = 0;
  rep(i, n) { ans = max(t.size(i), ans); }
  cout << ans << endl;
  return 0;
}
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;
using ll = long long;

// union-find
// グループ分けを木構造で管理する

// 強み(高速)
// ・xとyが同じグループかを判定する
// ・xとyが同じグループでない場合にこの2グループを併合する

// 小さい方を大きい方にくっつける(長さの情報が必要)
// 経路縮約

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

int main() {
  int n, m;
  cin >> n >> m; // n人m関係
  UnionFind uf(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    uf.unite(a, b);
  }
  int ans = 0;
  rep(i, n) { ans = max(ans, uf.size(i)); }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

// union-find
// グループ分けを木構造で管理する

// 強み(高速)
// ・xとyが同じグループかを判定する
// ・xとyが同じグループでない場合にこの2グループを併合する

// 小さい方を大きい方にくっつける(長さの情報が必要)
// 経路縮約

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

int deg[100005]; // 友達の人数
// vector<int> to[100005];

int main() {
  int n, m;
  cin >> n >> m; // n人m関係
  UnionFind uf(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  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>
using namespace std;

vector<int> par;
vector<int> len;
vector<int> mem;

int root(int a) {
  int p;
  int i = 0;
  int k;
  p = a;
  while (par.at(p) != p) {
    mem.at(i) = p;
    p = par.at(p);
    i++;
  }
  for (int j = 0; j < i; j++) {
    k = mem.at(j);
    par.at(k) = p;
  }
  return p;
}

int ask(int a, int b) {
  if (root(a) == root(b))
    return 1;
  else
    return 0;
}

void join(int a, int b) {
  int c, d;
  if (ask(a, b))
    return;
  else {
    c = root(a);
    d = root(b);
    if (len.at(c) < len.at(d)) {
      par.at(c) = d;
      len.at(d) += len.at(c);
    } else {
      par.at(d) = c;
      len.at(c) += len.at(d);
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  int x, u, v;
  par.resize(N + 1);
  len.resize(N + 1);
  mem.resize(N + 1);
  for (int i = 1; i < N + 1; i++) {
    par.at(i) = i;
  }
  for (int i = 1; i < N + 1; i++) {
    len.at(i) = 1;
  }
  for (int i = 0; i < M; i++) {
    cin >> u >> v;
    join(u, v);
  }
  int max = 0;
  for (int i = 1; i < N + 1; i++) {
    if (len.at(i) > max)
      max = len.at(i);
  }
  cout << max << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> v(N + 1);
  for (int i = 1; i < N + 1; i++) {
    v.at(i) = i;
  }
  vector<int> cont(N + 1);
  for (int i = 1; i < N + 1; i++) {
    cont.at(i) = 1;
  }
  int a, b;
  int aa, bb;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    aa = v.at(a);
    bb = v.at(b);
    if (aa != bb && cont.at(aa) > cont.at(bb)) {
      for (int j = 1; j < N + 1; j++) {
        if (v.at(j) == bb) {
          v.at(j) = aa;
        }
      }
      cont.at(aa) += cont.at(bb);
    }
    if (aa != bb && cont.at(aa) <= cont.at(bb)) {
      for (int j = 1; j < N + 1; j++) {
        if (v.at(j) == aa) {
          v.at(j) = bb;
        }
      }
      cont.at(bb) += cont.at(aa);
    }
  }
  vector<int> count(N + 1);
  for (int i = 1; i < N + 1; i++) {
    count.at(i) = 0;
  }
  int s;
  for (int i = 1; i < N + 1; i++) {
    s = v.at(i);
    count.at(s)++;
  }
  int max = 0;
  for (int i = 1; i < N + 1; i++) {
    if (count.at(i) > max)
      max = count.at(i);
  }
  cout << max << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

// AUTHOR: ROSHAN SRIVASTAVA
// WEBSITE: https://www.youtube.com/channel/UC6uQdd7kLLOdlHSVklhV7Cw

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

vector<int> G[200009];
bool vis[200009];

int solve(int x, int par) {
  vis[x] = true;
  int ans = 1;
  for (auto n : G[x]) {
    if (n == par)
      continue;
    if (vis[n])
      continue;
    ans += solve(n, x);
  }
  return ans;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  memset(vis, false, sizeof vis);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int ma = -1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ma = max(ma, solve(i, -1));
    }
  }
  cout << ma << endl;

  return 0;
}

Error: -11

Fixed submission:

// AUTHOR: ROSHAN SRIVASTAVA
// WEBSITE: https://www.youtube.com/channel/UC6uQdd7kLLOdlHSVklhV7Cw

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

vector<int> G[100009];
bool vis[100009];

int solve(int x, int par) {
  vis[x] = true;
  int ans = 1;
  for (auto n : G[x]) {
    if (n == par)
      continue;
    if (vis[n])
      continue;
    ans += solve(n, x);
  }
  return ans;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  memset(vis, false, sizeof vis);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int ma = -1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ma = max(ma, solve(i, -1));
    }
  }
  cout << ma << 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 repab(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define pb push_back
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
#define endl "\\n"

typedef pair<int, int> P;

const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//--//--//--//--//--//--//--//--//--//--//--//

// UnionFind
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(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 ans = 0;
  rep(i, n) { ans = max(ans, uf.size(i)); }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repab(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define pb push_back
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
#define endl "\\n"

typedef pair<int, int> P;

const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//--//--//--//--//--//--//--//--//--//--//--//

bool v[200001];
P c[200001];
int n, m;
int sum;

void dfs(int k) {
  v[k] = true;
  rep(i, m) {
    if (c[i].ft == k + 1 && !v[c[i].sd - 1]) {
      sum++;
      dfs(c[i].sd - 1);
    }
    if (c[i].sd == k + 1 && !v[c[i].ft - 1]) {
      sum++;
      dfs(c[i].ft - 1);
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    c[i].ft = a;
    c[i].sd = b;
  }
  rep(i, n) { v[i] = false; }

  int ans = 0;
  rep(i, n) {
    sum = 1;
    if (!v[i]) {
      dfs(i);
      ans = max(sum, ans);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;

using Graph = vector<vector<int>>;

int n, m;

// 深さ優先探索
vector<bool> seen; // 探索したか記録

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

  seen.assign(n, false);
  int ans = 2;
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      queue<int> que;
      que.push(i);
      int cnt = 0;
      while (!que.empty()) {
        cnt++;
        int v = que.front();
        que.pop();

        for (int nv : G[v]) {
          if (seen[nv])
            continue;
          seen[nv] = true;
          que.push(nv);
        }
      }

      ans = max(ans, cnt);
    }
  }
  cout << ans - 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;

using Graph = vector<vector<int>>;

int n, m;

// 深さ優先探索
vector<bool> seen; // 探索したか記録
int bfs(Graph &G, int v) {
  vector<int> dist(n, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[0] = 0;
  que.push(0); // 0 を橙色頂点にする

  // BFS 開始 (キューが空になるまで探索を行う)
  int cnt = 0;
  while (!que.empty()) {
    cnt++;
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      if (dist[nv] != -1)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
  return cnt;
}

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

  seen.assign(n, false); // 初期化
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      ans = max(ans, bfs(G, i));
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
struct bcj {
  int f[200001];
  int size[200001];
  int find(int n) {
    if (f[n] == n)
      return n;
    else
      f[n] = find(f[n]);
    return f[n];
  }
  int add(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a == b)
      return 0;
    else {
      f[a] = b;
      size[b] += size[a];
    }
    return 1;
  }
  void clear(int n) {
    for (int i = 1; i <= n; i++) {
      f[i] = i;
      size[i] = 1;
    }
  }
} S;
int n, m, x, y, ans;
int main() {
  scanf("%d %d", &n, &m);
  S.clear(n);
  while (m--) {
    scanf("%d %d", &x, &y);
    S.add(x, y);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, S.size[i]);
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct bcj {
  int f[100001];
  int size[100001];
  int find(int n) {
    if (f[n] == n)
      return n;
    else
      f[n] = find(f[n]);
    return f[n];
  }
  int add(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a == b)
      return 0;
    else {
      f[a] = b;
      size[b] += size[a];
    }
    return 1;
  }
  void clear(int n) {
    for (int i = 1; i <= n; i++) {
      f[i] = i;
      size[i] = 1;
    }
  }
} S;
int n, m, x, y, ans;
int main() {
  scanf("%d %d", &n, &m);
  S.clear(n);
  while (m--) {
    scanf("%d %d", &x, &y);
    S.add(x, y);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, S.size[i]);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int a[200005];
int cnt[200005];
int findroot(int v) { return a[v] == v ? v : a[v] = findroot(a[v]); }
int main() {
  int n, m;
  cin >> n;
  cin >> m;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    cnt[i] = 1;
  }
  int max = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    int findrootx = findroot(x);
    int findrooty = findroot(y);
    if (findrootx != findrooty) {
      a[findrooty] = findrootx;
      cnt[findrootx] += cnt[findrooty];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (findroot(i) == i) {
      if (max < cnt[i]) {
        max = cnt[i];
      }
    }
  }
  cout << max;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int a[200005];
int cnt[200005];
int findroot(int v) { return a[v] == v ? v : findroot(a[v]); }
int main() {
  int n, m;
  cin >> n;
  cin >> m;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    cnt[i] = 1;
  }
  int max = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    int findrootx = findroot(x);
    int findrooty = findroot(y);
    if (findrootx != findrooty) {
      a[findrooty] = findrootx;
      cnt[findrootx] += cnt[findrooty];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (findroot(i) == i) {
      if (max < cnt[i]) {
        max = cnt[i];
      }
    }
  }
  cout << max;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, m, ans = 0, father[N], sum[N];
int Findfather(int x) {
  return father[x] == x ? x : father[x] = Findfather(father[x]);
}

void Union(int x, int y) {
  x = Findfather(x), y = Findfather(y);
  if (x != y)
    father[y] = x, sum[x] += sum[y];
}

void init() {
  for (int i = 0; i <= n; i++)
    father[i] = i, sum[i] = 1;
}

int main() {
  cin >> n >> m;
  init();
  while (m--) {
    int u, v;
    cin >> u >> v;
    Union(u, v);
  }
  for (int i = 1; i <= n; i++)
    if (Findfather(i) == i)
      ans = max(ans, sum[i]);
  cout << ans;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
map<int, int> mp;
int a[200005];
int findroot(int v) {
  if (a[v] == v) {
    return v;
  }
  return findroot(a[v]);
}
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    mp[i] = 1;
  }
  int max = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int findrootx = findroot(x);
    int findrooty = findroot(y);
    if (findrootx != findrooty) {
      a[findrooty] = findrootx;
      mp[findrootx] += mp[findrooty];
      //			mp.erase(findrooty);
    }
  }
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it == mp.begin()) {
      max = it->second;
    } else {
      if (max < it->second) {
        max = it->second;
      }
    }
  }

  printf("%d", max);
}