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

#include <bits/stdc++.h>
#define ll long long int
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll, ll>
#define pb push_back
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const int N = 2e5 + 5;

vector<int> vis(N, 0);
vector<int> adj[N];
int mx = 0;

int dfs(int u) {
  int ans = 0;
  if (!vis[u]) {
    vis[u] = true;
    ans++;
    for (auto v : adj[u]) {
      ans += dfs(v);
    }
  }
  return ans;
}

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

int main() {
  fast;
  solve();
  // int t;
  // cin >> t;
  // while(t--){
  //     solve();
  // }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll, ll>
#define pb push_back
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const int N = 1e5 + 5;

vector<int> vis(N, 0);
vector<int> adj[N];
int mx = 0;

int dfs(int u) {
  int ans = 0;
  if (!vis[u]) {
    vis[u] = true;
    ans++;
    for (auto v : adj[u]) {
      ans += dfs(v);
    }
  }
  return ans;
}

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

int main() {
  fast;
  solve();
  // int t;
  // cin >> t;
  // while(t--){
  //     solve();
  // }
  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;

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 (x < y)
      swap(x, y);
    // サイズの更新
    d[x] += d[y];
    // 結合
    d[y] = x;
    return true;
  }
  // サイズの取得
  int size(int x) { return -d[find(x)]; }
  // 同じ集合に属しているか
  bool same(int x, int y) { return (find(x) == find(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);
  }

  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;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> ab(n, vector<bool>(n));

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    ab[a - 1][b - 1] = true;
    ab[b - 1][a - 1] = true;
  }

  rep(i, n) rep(j, n) {
    if (i < j) {
      if (ab[i][j]) {
        for (int k = j; k < n; ++k) {
          if (ab[j][k]) {
            ab[i][k] = true;
            ab[k][i] = true;
          }
        }
      }
    }
  }

  int ans = 0;
  rep(i, n) {
    int cnt = 0;
    rep(j, n) {
      if (ab[i][j])
        ++cnt;
    }
    ans = max(ans, cnt);
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
typedef struct {
  int Parent, Rank;
} UnionFind;
UnionFind uft[200000];
static inline int Root(int x) {
  return (uft[x].Parent != x) ? uft[x].Parent = Root(uft[x].Parent) : x;
}
static inline int Unite(int x, int y) {
  x = Root(x), y = Root(y);
  if (x != y)
    uft[x].Parent = y, uft[y].Rank += uft[x].Rank;
  return x != y;
}
static inline int Size(int x) { return uft[Root(x)].Rank; }
static inline int Belong(int x, int y) { return Root(x) == Root(y); }
int main(void) {
  int A, B, N, M, i = 0, estrange = 0;
  if (!scanf("%d %d", &N, &M))
    return -1;
  while (i < N) {
    uft[i].Parent = i;
    uft[i++].Rank = 1;
  }
  while (M--) {
    if (!scanf("%d %d", &A, &B)) {
      return -1;
    }
    A--, B--;
    Unite(A, B);
  }
  while (N--) {
    estrange = estrange > Size(N - 1) ? estrange : Size(N - 1);
  }
  return printf("%d\\n", estrange), 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
typedef struct {
  int Parent, Rank;
} UnionFind;
UnionFind uft[100000];
static inline int Root(int x) {
  return (uft[x].Parent != x) ? uft[x].Parent = Root(uft[x].Parent) : x;
}
static inline int Unite(int x, int y) {
  x = Root(x), y = Root(y);
  if (x != y)
    uft[x].Parent = y, uft[y].Rank += uft[x].Rank;
  return x != y;
}
static inline int Size(int x) { return uft[Root(x)].Rank; }
static inline int Belong(int x, int y) { return Root(x) == Root(y); }
int main(void) {
  int A, B, N, M, i = 0, estrange = 0;
  if (!scanf("%d %d", &N, &M))
    return -1;
  while (i < N) {
    uft[i].Parent = i;
    uft[i++].Rank = 1;
  }
  while (M--) {
    if (!scanf("%d %d", &A, &B)) {
      return -1;
    }
    A--, B--;
    Unite(A, B);
  }
  while (N--) {
    estrange = estrange > Size(N - 1) ? estrange : Size(N - 1);
  }
  return printf("%d\\n", estrange), 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

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


Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    a--;
    b--;
    UF.unite(a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, UF.size(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool vis[200005];
vector<ll> adj[200005];
set<ll> chk[200005];
vector<ll> vec;
ll counter = 0;
void dfs(ll a) {
  vis[a] = 1;
  counter++;
  for (int i = 0; i < (ll)adj[a].size(); i++) {
    if (vis[adj[a][i]] == 0) {
      dfs(adj[a][i]);
    }
  }
}
int main() {
  ll n, m, a, b, ans = 0;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    if (chk[a].count(b) == 0) {
      adj[a].push_back(b);
      chk[a].insert(b);
    }
    if (chk[b].count(a) == 0) {
      adj[b].push_back(a);
      chk[b].insert(a);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      counter = 0;
      dfs(i);
      vec.push_back(counter);
    }
  }
  sort(vec.begin(), vec.end());
  if (vec.size() == 1) {
    cout << vec[0] << '\\n';
  } else {
    ans += vec[0];
    for (int i = 1; i < (ll)vec.size(); i++) {
      ans += (vec[i] - vec[i - 1]);
    }
    cout << ans << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool vis[200005];
vector<ll> adj[200005];
set<ll> chk[200005];
vector<ll> vec;
ll counter = 0;
ll dfs(ll a) {
  vis[a] = 1;
  counter++;
  for (int i = 0; i < (ll)adj[a].size(); i++) {
    if (vis[adj[a][i]] == 0) {
      dfs(adj[a][i]);
    }
  }
}
int main() {
  ll n, m, a, b, ans = 0;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    if (chk[a].count(b) == 0) {
      adj[a].push_back(b);
      chk[a].insert(b);
    }
    if (chk[b].count(a) == 0) {
      adj[b].push_back(a);
      chk[b].insert(a);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      counter = 0;
      dfs(i);
      vec.push_back(counter);
    }
  }
  sort(vec.begin(), vec.end());
  if (vec.size() == 1) {
    cout << vec[0] << '\\n';
  } else {
    ans += vec[0];
    for (int i = 1; i < (ll)vec.size(); i++) {
      ans += vec[i] - vec[i - 1];
    }
    cout << ans << '\\n';
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define N ((int)2e5 + 5)
using namespace std;
bool vis[N];
int dis[N];
vector<int> vec[N];
int bfs(int src) {
  int cnt = 1;
  queue<int> que;
  que.push(src);
  vis[src] = 1;
  dis[src] = 0;
  while (!que.empty()) {
    int a = que.front();
    que.pop();
    for (int b : vec[a])
      if (vis[b] == 0) {
        cnt++;
        que.push(b);
        vis[b] = 1;
        dis[b] = dis[a] + 1;
      }
  }
  return cnt;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      mx = max(mx, bfs(i));
    }
  }
  cout << mx << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define N ((int)2e5 + 5)
using namespace std;
bool vis[N];
int dis[N];
vector<int> vec[N];
int bfs(int src) {
  memset(vis, 0, sizeof vis);
  int cnt = 1;
  queue<int> que;
  que.push(src);
  vis[src] = 1;
  dis[src] = 0;
  while (!que.empty()) {
    int a = que.front();
    que.pop();
    for (int b : vec[a])
      if (vis[b] == 0) {
        cnt++;
        que.push(b);
        vis[b] = 1;
        dis[b] = dis[a] + 1;
      }
  }
  return cnt;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      mx = max(mx, bfs(i));
    }
  }
  cout << mx << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<vector<ll>> graph;

ll dfs(ll u, graph &g, vector<bool> &used) {
  if (used[u] == true)
    return 1;
  used[u] = true;

  ll res = 1;
  for (auto &&v : g[u]) {
    if (used[v] == false) {
      res += dfs(v, g, used);
    }
  }
  return res;
}

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  /*
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  sort(B.begin(), B.end());
  B.erase(unique(B.begin(), B.end()), B.end());
  */
  graph g = graph(N);
  for (ll i = 0; i < M; i++) {
    g[A[i]].push_back(B[i]);
    g[B[i]].push_back(A[i]);
  }
  vector<bool> used(N, false);
  ll size_max = 0;
  // cout << "debug" << endl;

  for (ll i = 0; i < N; i++) {
    ll tmp = dfs(i, g, used);
    size_max = max(size_max, tmp);
    // cout << tmp << endl;
  }
  cout << size_max << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    A[i]--;
    scanf("%lld", &B[i]);
    B[i]--;
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

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

typedef vector<vector<ll>> graph;

ll dfs(ll u, graph g, vector<bool> &used) {
  if (used[u] == true)
    return 1;

  used[u] = true;
  ll res = 1;
  for (auto &&v : g[u]) {
    if (used[v] == false) {
      res += dfs(v, g, used);
    }
  }
  return res;
}

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  /*
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  sort(B.begin(), B.end());
  B.erase(unique(B.begin(), B.end()), B.end());
  */
  graph g = graph(N);
  for (ll i = 0; i < M; i++) {
    g[A[i]].push_back(B[i]);
    g[B[i]].push_back(A[i]);
  }
  vector<bool> used(N, false);
  ll size_max = 0;
  // cout << "debug" << endl;

  for (ll i = 0; i < N; i++) {
    ll tmp = dfs(i, g, used);
    size_max = max(size_max, tmp);
    // cout << tmp << endl;
  }
  cout << size_max << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    A[i]--;
    scanf("%lld", &B[i]);
    B[i]--;
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int getID(vector<int> &v, int idx) {
  if (v[idx] == idx)
    return idx;
  v[idx] = getID(v, v[idx]);
  return v[idx];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> d(N, 0);
  vector<int> v(N, -1);
  for (int i = 0; i < M; i++) {
    int s[2];
    cin >> s[0] >> s[1];
    s[0]--;
    s[1]--;

    if (v[s[0]] == -1 && v[s[1]] == -1) {
      v[s[0]] = s[0];
      v[s[1]] = s[0];
      d[s[0]] = 2;
    } else if (v[s[0]] == -1 || v[s[1]] == -1) {
      for (int j = 0; j < 2; j++) {
        if (v[s[j]] == -1) {
          int id = getID(v, s[(j + 1) % 2]);
          v[s[j]] = id;
          d[id]++;
        }
      }
    } else {
      int r1 = getID(v, s[0]);
      int r2 = getID(v, s[1]);
      if (r1 == r2)
        continue;
      v[r1] = r2;
      d[r2] += d[r1];
      d[r1] = 0;
    }
  }
  int ans = 1;
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int getID(vector<int> &v, int idx) {
  if (v[idx] == -1)
    return -1;
  int cnt = 0;
  while (1) {
    if (v[idx] == idx)
      break;
    idx = v[idx];
    if (cnt++ == 10000)
      break;
  }
  return idx;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> d(N, 0);
  vector<int> v(N, -1);
  for (int i = 0; i < M; i++) {
    int s[2];
    cin >> s[0] >> s[1];
    s[0]--;
    s[1]--;

    if (v[s[0]] == -1 && v[s[1]] == -1) {
      v[s[0]] = s[0];
      v[s[1]] = s[0];
      d[s[0]] = 2;
    } else if (v[s[0]] == -1 || v[s[1]] == -1) {
      for (int j = 0; j < 2; j++) {
        if (v[s[j]] == -1) {
          int id = getID(v, s[(j + 1) % 2]);
          v[s[j]] = id;
          d[id]++;
        }
      }
    } else {
      int r1 = getID(v, s[0]);
      int r2 = getID(v, s[1]);
      if (r1 == r2)
        continue;
      v[r1] = r2;
      d[r2] += d[r1];
      d[r1] = 0;
    }
  }
  int ans = 1;
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define re return
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<int> v[200005];
int vis[200005];
int dfs(int now) {
  vis[now] = 1;
  int num = 1;
  for (int i : v[now])
    if (!vis[i])
      num += dfs(i);
  re num;
}
signed main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    v[b].pb(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
  re 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define re return
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<int> v[100005];
int vis[100005];
int dfs(int now) {
  vis[now] = 1;
  int num = 1;
  for (int i : v[now])
    if (!vis[i])
      num += dfs(i);
  re num;
}
signed main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].pb(b);
    v[b].pb(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
  re 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define per(i, a, b) for (int i = (a); i >= (int)(b); i--)
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int fa[maxn], siz[maxn];
int Find(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = Find(fa[x]);
}
void Union(int a, int b) {
  int f1 = Find(a), f2 = Find(b);
  siz[f1] += siz[f2];
  fa[f2] = f1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) {
    fa[i] = i;
    siz[i] = 1;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    if (Find(a) != Find(b))
      Union(a, b);
  }
  int ans = 0;
  rep(i, 1, n) {
    if (i == fa[i])
      ans = max(ans, siz[i]);
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define per(i, a, b) for (int i = (a); i >= (int)(b); i--)
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int fa[maxn], siz[maxn];
int Find(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = Find(fa[x]);
}
void Union(int a, int b) {
  int f1 = Find(a), f2 = Find(b);
  siz[f1] += siz[f2];
  fa[f2] = f1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) {
    fa[i] = i;
    siz[i] = 1;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    if (Find(a) != Find(b))
      Union(a, b);
  }
  int ans = 0;
  rep(i, 1, n) {
    if (i == fa[i])
      ans = max(ans, siz[i]);
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded


// Problem: D - Friends
// Contest: AtCoder - AtCoder Beginner Contest 177
// URL: https://atcoder.jp/contests/abc177/tasks/abc177_d
// Parsed on: 2020-08-29 17:30:05
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

using namespace std;

int root(vector<int> &dsu, int a) {
  if (dsu[a] == a) {
    return a;
  }
  return dsu[a] = root(dsu, dsu[a]);
}

int findRoot(vector<int> const &dsu, int node) {
  if (dsu[node] == node)
    return node;
  else
    return findRoot(dsu, dsu[node]);
}

void insert(vector<int> &dsu, int to, int from, vector<int> &size) {
  to = root(dsu, to);
  from = root(dsu, from);

  if (to != from) {
    if (size[to] < size[from])
      swap(to, from);

    dsu[from] = to;
    size[to] += size[from];
  }
}

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vector<int> dsu(n), size(n);

  for (int a = 0; a < n; a++) {
    dsu[a] = a;
    size[a] = 1;
  }

  while (m--) {
    int aa, ba;
    cin >> aa >> ba;
    aa--;
    ba--;
    insert(dsu, aa, ba, size);
  }

  for (auto &e : dsu)
    e = findRoot(dsu, e);

  unordered_map<int, int> ds;
  for (auto &e : dsu)
    ds[e]++;

  int ans = ds[0];
  for (auto &e : ds)
    if (e.second > ans)
      ans = e.second;
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:


// Problem: D - Friends
// Contest: AtCoder - AtCoder Beginner Contest 177
// URL: https://atcoder.jp/contests/abc177/tasks/abc177_d
// Parsed on: 2020-08-29 17:30:05
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

using namespace std;

int root(vector<int> &dsu, int a) {
  if (dsu[a] == a) {
    return a;
  }
  return dsu[a] = root(dsu, dsu[a]);
}

int findRoot(vector<int> const &dsu, int node) {
  if (dsu[node] == node)
    return node;
  else
    return findRoot(dsu, dsu[node]);
}

void insert(vector<int> &dsu, int to, int from) {
  to = root(dsu, to);
  from = root(dsu, from);

  if (to != from)
    dsu[to] = from;
}

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vector<int> dsu(n);

  for (int a = 0; a < n; a++)
    dsu[a] = a;

  while (m--) {
    int aa, ba;
    cin >> aa >> ba;
    aa--;
    ba--;
    insert(dsu, aa, ba);
  }

  for (auto &e : dsu)
    e = findRoot(dsu, e);

  unordered_map<int, int> ds;
  for (auto &e : dsu)
    ds[e]++;

  int ans = ds[0];
  for (auto &e : ds)
    if (e.second > ans)
      ans = e.second;
  cout << ans;

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

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

map<ll, ll> mp;
vector<ll> v[1000000];
ll dfs(ll node) {

  if (mp[node] == 0) {
    mp[node]++;
    ll i, l = v[node].size(), s = 0;
    for (i = 0; i < l; i++) {
      s += dfs(v[node][i]);
    }
    return s + 1;
  } else {
    return 0;
  }
}
int main() {
  ll i, j, t = 1;
  // cin>>t;
  for (i = 0; i < t; i++) {
    ll n, m, x, y, mini = 0;
    cin >> n >> m;
    for (j = 0; j < m; j++) {
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    for (j = 1; j <= n; j++) {
      if (mp[j] == 0) {
        mini = max(mini, dfs(j));
      }
    }
    cout << mini;
  }
}

Error: WA

Fixed submission:

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

map<ll, ll> mp;
vector<ll> v[100];
ll dfs(ll node) {

  if (mp[node] == 0) {
    mp[node]++;
    ll i, l = v[node].size(), s = 0;
    for (i = 0; i < l; i++) {
      s += dfs(v[node][i]);
    }
    return s + 1;
  } else {
    return 0;
  }
}
int main() {
  ll i, j, t = 1;
  // cin>>t;
  for (i = 0; i < t; i++) {
    ll n, m, x, y, mini = 0;
    cin >> n >> m;
    for (j = 0; j < m; j++) {
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    for (j = 1; j <= n; j++) {
      if (mp[j] == 0) {
        mini = max(mini, dfs(j));
      }
    }
    cout << mini;
  }
}