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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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;
    UF.unite(a - 1, b - 1);
  }

  // for(int person : friends[1])
  //     cout<<person<<' ';
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, UF.size(i));

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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;
  }

  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;
    UF.unite(a - 1, b - 1);
  }

  // for(int person : friends[1])
  //     cout<<person<<' ';
  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: Time Limit Exceeded

#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

int find_(int x) { return x == a[x] ? x : a[x] = find_(a[x]); }

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = find_(x);
    int fy = find_(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

int find_(int x) { return x == a[x] ? x : a[x] = find_(a[x]); }

int findset(int x) {
  int r = x, i = x, j;
  while (a[r] != r)
    r = a[r];
  while (i != r)
    j = a[i], a[i] = r, i = j;
}

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = findset(x);
    int fy = findset(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

int find_(int x) { return x == a[x] ? x : a[x] = find_(a[x]); }

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = find_(x);
    int fy = find_(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
using namespace std;

int a[maxn], b[maxn];

int find_(int x) { return x == a[x] ? x : find_(a[x]); }

void solve() {
  int n, m, max_ = 0;
  cin >> n >> m;
  for (int j = 0; j <= n; ++j)
    a[j] = j, b[j] = 1;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int fx = find_(x);
    int fy = find_(y);
    if (fx != fy)
      a[fx] = fy, b[fy] += b[fx];
  }
  for (int k = 1; k <= n; ++k)
    if (b[k] > max_)
      max_ = b[k];
  cout << max_;
}

signed main() {
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int f[300000] = {};
int cnt[300000] = {};
int ff(int x) {
  if (f[x] == x)
    return x;
  return f[x] = ff(f[x]);
}
void link(int x, int y) { f[ff(y)] = ff(x); }
int main() {
  int n;
  cin >> n;
  for (int j = 0; j <= n; ++j) {
    f[j] = j;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int p, q;
    scanf("%d%d", &p, &q);
    link(p, q);
  }
  for (int i = 1; i <= n; ++i) {
    cnt[ff(i)]++;
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, cnt[i]);
  }

  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int f[300000];
int cnt[300000] = {};
int find(int x) {
  if (f[x] == x)
    return x;
  return f[x] = find(f[x]);
}
int link(int x, int y) { f[find(y)] = find(x); }
LL s[300000];
int main() {
  int n;
  cin >> n;
  for (int j = 0; j <= n; ++j) {
    f[j] = j;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int p, q;
    scanf("%d%d", &p, &q);
    link(p, q);
  }
  for (int i = 1; i <= n; ++i) {
    cnt[find(i)]++;
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, cnt[i]);
  }

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

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 2e5 + 5, mod = 1e9 + 7, M = 1e5 + 5;

int p[N], sz[N];

void make_set(int v) {
  p[v] = v;
  sz[v] = 1;
}

int find_set(int v) {
  if (p[v] == v)
    return v;
  return p[v] = find_set(p[v]);
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  p[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    make_set(i);
  while (m--) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, sz[i]);
  cout << mx << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 500 + 5, mod = 1e9 + 7, M = 1e5 + 5;

int p[N], sz[N];

void make_set(int v) {
  p[v] = v;
  sz[v] = 1;
}

int find_set(int v) {
  if (p[v] == v)
    return v;
  return p[v] = find_set(p[v]);
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  sz[a] += sz[b];
  p[b] = a;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    make_set(i);
  while (m--) {
    int a, b;
    cin >> a >> b;
    union_set(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, sz[i]);
  cout << mx << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9 + 7;
vector<ll> adj[1111111];
ll cnt = 0;
bool vis[1111111] = {false};
void dfs(ll src) {
  vis[src] = true;
  ++cnt;
  for (auto node : adj[src]) {
    if (!vis[node]) {
      dfs(node);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  set<ll> ss;
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; ++i) {
    ll u, v;
    cin >> u >> v;
    ss.insert(u);
    ss.insert(v);
    if (!mp[{u, v}]) {
      adj[u].push_back(v);
      adj[v].push_back(u);
      ++mp[{u, v}];
      ++mp[{v, u}];
    }
  }
  if (m == 0) {
    cout << 1 << '\\n';
    return 0;
  }
  vector<ll> an;
  for (auto i : ss) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      an.push_back(cnt);
    }
  }
  sort(an.begin(), an.end());
  ll ans = an[0];
  ll mx = an[0];
  for (int i = 1; i < (int)an.size(); ++i) {
    ans += (an[i] - mx);
    mx = max(mx, an[i]);
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9 + 7;
vector<int> adj[1111111];
int cnt = 0;
bool vis[1111111];
void dfs(int src) {
  vis[src] = true;
  ++cnt;
  for (auto node : adj[src]) {
    if (!vis[node]) {
      dfs(node);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  set<int> ss;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    ss.insert(u);
    ss.insert(v);
    if (!mp[{u, v}]) {
      adj[u].push_back(v);
      adj[v].push_back(u);
      ++mp[{u, v}];
      ++mp[{v, u}];
    }
  }
  vector<int> an;
  for (auto i : ss) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      an.push_back(cnt);
    }
  }
  sort(an.begin(), an.end());
  int ans = an[0];
  int mx = an[0];
  for (int i = 1; i < (int)an.size(); ++i) {
    ans += (an[i] - mx);
    mx = max(mx, an[i]);
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using Graph = vector<vector<int>>;
map<ll, ll> mp;

vector<bool> seen;
void dfs(const Graph &G, int v, int c) {
  seen[v] = true;
  mp[c]++;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, c);
  }
}

int main() {
  int N, M;
  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 cnt = 0;
  rep(i, N) {
    if (!seen[i]) {
      dfs(G, i, cnt);
      cnt++;
    }
  }
  ll ans = 0;
  for (auto x : mp)
    chmax(ans, x.second);
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using Graph = vector<vector<int>>;
map<ll, ll> mp;

vector<bool> seen;
void dfs(const Graph &G, int v, int c) {
  seen[v] = true;
  mp[c]++;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v, c);
  }
}

int main() {
  int N, M;
  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 cnt = 0;
  rep(i, N) {
    if (!seen[i]) {
      dfs(G, i, cnt);
      cnt++;
    }
  }
  ll ans = 0;
  while (mp.size()) {
    ans++;
    for (auto x : mp) {
      mp[x.first]--;
    }
    for (auto x : mp)
      if (x.second == 0)
        mp.erase(x.first);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e6 + 1000;
const LL P = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int n, m;
int f[N], sz[N];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
    sz[i] = 1;
  }
  int u, v;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    u = find(u);
    v = find(v);
    if (u == v)
      continue;
    f[u] = v;
    sz[v] += sz[u];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    u = find(i);
    ans = max(ans, sz[u]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e6 + 1000;
const LL P = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int n, m;
int f[N], sz[N];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    f[x] = find(f[x]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
    sz[i] = 1;
  }
  int u, v;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    u = find(u);
    v = find(v);
    if (u == v)
      continue;
    f[u] = v;
    sz[v] += sz[u];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    u = find(i);
    ans = max(ans, sz[u]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

const int maxn = 200005;
int parent[maxn];
int rnk[maxn];

void make_set(int v) {
  parent[v] = v;
  rnk[v] = 1;
}
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (rnk[a] < rnk[b])
      swap(a, b);
    parent[b] = a;
    rnk[a] += rnk[b];
  }
}

void solve() {
  // write here
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    make_set(i);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    union_sets(u, v);
  }
  int mxval = 0;
  for (int i = 0; i < n; i++)
    mxval = max(mxval, rnk[i]);
  cout << mxval << endl;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

const int maxn = 200005;
int parent[maxn];
int rnk[maxn];

void make_set(int v) {
  parent[v] = v;
  rnk[v] = 1;
}
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (rnk[a] < rnk[b])
      swap(a, b);
    parent[b] = a;
    rnk[a] += rnk[b];
  }
}

void solve() {
  // write here
  int n, m;
  for (int i = 0; i < n; i++)
    make_set(i);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    union_sets(u, v);
  }
  int mxval = 0;
  for (int i = 0; i < n; i++)
    mxval = max(mxval, rnk[i]);
  cout << mxval << endl;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

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

#include <algorithm>
#include <cstdio>
using namespace std;

const int sz = 2e5 + 1;
int p[sz], w[sz];

int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void uni(int a, int b) {
  if (a == b)
    return;
  a < b ? p[b] = a : p[a] = b;
  w[a] = w[b] += w[a];
}

int main() {
  int N, M, u, v;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++)
    p[i] = i, w[i] = 1;

  while (M--) {
    scanf("%d %d", &u, &v);
    uni(get(u), get(v));
  }

  int res = 0;
  for (int i = 1; i <= N; i++)
    res = max(res, w[i]);
  printf("%d\\n", res);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

const int sz = 1e5 + 1;
int p[sz], w[sz];

int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void uni(int a, int b) {
  if (a == b)
    return;
  a < b ? p[b] = a : p[a] = b;
  w[a] = w[b] += w[a];
}

int main() {
  int N, M, u, v;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++)
    p[i] = i, w[i] = 1;

  while (M--) {
    scanf("%d %d", &u, &v);
    uni(get(u), get(v));
  }

  int res = 0;
  for (int i = 1; i <= N; i++)
    res = max(res, w[i]);
  printf("%d\\n", res);
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> v(200001);
bitset<200001> seen;
bitset<200001> sg;

int f(int x, bool y = true) {
  if (y) {
    seen.set();
    y = false;
  }
  seen[x] = false;
  sg[x] = false;
  int res = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int tmp = v[x][i];
    if (seen[tmp]) {
      res += f(tmp, y);
      sg[tmp] = false;
    }
  }
  return res;
}

int main() {
  sg.set();
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (sg[i])
      ans = max(ans, f(i));
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

vector<vector<int>> v(200001);
bitset<200001> seen;

int f(int x, bool y = true) {
  if (y) {
    seen.set();
    y = false;
  }
  seen[x] = false;
  int res = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int tmp = v[x][i];
    if (seen[tmp])
      res += f(tmp, y);
  }
  return res;
}

int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, f(i));
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll mod = 1000000007;

int ret = 0;
vector<int> vi;

int bfs(const Graph &g, int i) {
  vi[i] = 1;
  for (auto next : g[i]) {
    if (vi[next] != 0)
      continue;
    ret = bfs(g, next) + 1;
  }
  return ret;
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n);
  vi.assign(n, 0);
  set<P> s;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    P p(a, b);
    P q(b, a);
    if (!s.count(p) && !s.count(q)) {
      G[a].push_back(b);
      G[b].push_back(a);
      s.insert(p);
      s.insert(q);
    }
  }
  int ans = bfs(G, 0);
  ret = 0;
  rep(i, n) {
    if (vi[i] != 0)
      continue;
    ans = max(ans, bfs(G, i));
    ret = 0;
  }
  cout << ans + 1 << endl;
}

Error: TLE

Fixed submission:

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

const ll mod = 1000000007;

int ret = 0;
vector<int> vi;

int bfs(const Graph &g, int i) {
  vi[i] = 1;
  for (auto next : g[i]) {
    if (vi[next] != 0)
      continue;
    ret = bfs(g, next) + 1;
  }
  return ret;
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n);
  vi.assign(n, 0);
  set<P> s;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    P p(a, b);
    P q(b, a);
    if (!s.count(p) && !s.count(q)) {
      G[a].push_back(b);
      G[b].push_back(a);
      s.insert(p);
      s.insert(q);
    }
  }
  int ans = 0;
  rep(i, n) {
    ans = max(ans, bfs(G, i));
    vi.assign(n, 0);
    ret = 0;
  }
  cout << ans + 1 << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define INIT                                                                   \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;
vector<int> g[200010];
bool v[200010];
int dfs(int s) {
  int res = 1;
  v[s] = true;
  for (int i : g[s]) {
    if (!v[i]) {
      v[i] = true;
      res += dfs(i);
    }
  }
  return res;
}

int cnctd() {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      res = max(res, dfs(i));
    }
  }
  return res;
}

int32_t main() {
  INIT cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  int gr = cnctd();
  cout << gr;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INIT                                                                   \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;
vector<int> g[100010];
bool v[100010];
int dfs(int s) {
  int res = 1;
  v[s] = true;
  for (int i : g[s]) {
    if (!v[i]) {
      v[i] = true;
      res += dfs(i);
    }
  }
  return res;
}

int cnctd() {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i]) {
      res = max(res, dfs(i));
    }
  }
  return res;
}

int32_t main() {
  INIT cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  int gr = cnctd();
  cout << gr;
  return 0;
}