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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mp make_pair
#define pb push_back
#define pll pair<ll, ll>

ll INF = 1000000007;

//-----------------//
#define amin main
//-----------------//
vector<ll> adj[300005];
ll vis[300005] = {0};
vector<ll> con;
ll cnt = 0;
ll app[300005] = {0};
void dfs(ll u) {

  vis[u] = 1;
  cnt++;
  for (auto v : adj[u]) {
    if (vis[v] == 0) {
      dfs(v);
    }
  }
}

int amin() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, i, j;

  ll n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;

    adj[a].pb(b);
    adj[b].pb(a);

    app[a] = 1;
    app[b] = 1;
  }
  ll g = 0;
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0 && app[i] == 1) {
      dfs(i);
      con.pb(cnt);
      cnt = 0;
    }
  }
  if (con.size() > 0)
    sort(con.begin(), con.end());
  if (con.size() > 0)
    cout << con[con.size() - 1] << endl;
  else
    cout << 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mp make_pair
#define pb push_back
#define pll pair<ll, ll>

ll INF = 1000000007;

//-----------------//
#define amin main
//-----------------//
vector<ll> adj[300005];
map<pll, ll> visi;
ll vis[300005] = {0};
vector<ll> con;
ll cnt = 0;
ll app[300005] = {0};
void dfs(ll u) {

  vis[u] = 1;
  cnt++;
  for (auto v : adj[u]) {
    if (vis[v] == 0) {
      dfs(v);
    }
  }
}

int amin() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, i, j;

  ll n, m;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;

    adj[a].pb(b);
    adj[b].pb(a);

    app[a] = 1;
    app[b] = 1;
  }
  ll g = 0;
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0 && app[i] == 1) {
      dfs(i);
      con.pb(cnt);
      cnt = 0;
    }
  }
  sort(con.begin(), con.end());

  cout << con[con.size() - 1] << endl;

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

#include <iostream>
#include <vector>

using namespace std;

class Group {
public:
  vector<int> root;
  int maxSize = -1;

  Group(int n) { root = vector<int>(n, -1); }

  int getRoot(int c) { return root[c] < 0 ? c : root[c] = getRoot(root[c]); }

  void merge(int a, int b) {
    int rootA = getRoot(a);
    int rootB = getRoot(b);
    if (rootA != rootB) {
      root[rootA] += root[rootB];
      root[rootB] = rootA;
      maxSize = root[rootA] < maxSize ? root[rootA] : maxSize;
    }
  }

  int getMax() { return -maxSize; }
};

int main() {
  int n, m;
  cin >> n >> m;
  Group group(n);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    group.merge(--a, --b);
  }

  cout << group.getMax();
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

class Group {
public:
  vector<int> root;
  int maxSize = -1;

  Group(int n) { root = vector<int>(n, -1); }

  int getRoot(int c) { return root[c] < 0 == c ? c : getRoot(root[c]); }

  void merge(int a, int b) {
    int rootA = getRoot(a);
    int rootB = getRoot(b);
    if (rootA != rootB) {
      root[rootA] += root[rootB];
      root[rootB] = rootA;
      maxSize = root[rootA] < maxSize ? root[rootA] : maxSize;
    }
  }

  int getMax() { return -maxSize; }
};

int main() {
  int n, m;
  cin >> n >> m;
  Group group(n);

  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    group.merge(--a, --b);
  }

  cout << group.getMax();
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define ALL(x) x.begin(), x.end()
#define MOD (1000000000 + 7)
using namespace std;
#define PI 3.14159265359
#define tMOD 998244353
#define mkpr(x, y) make_pair(x, y)
int N, M;
int v[200400];
int vs[203000] = {0};

int root(int i) {
  if (v[i] == i) {
    return i;
  } else {
    return root(v[i]);
  }
}

int margego(int tibi, int deka) {
  int rtibi = root(tibi), rdeka = root(deka);
  if (rtibi == rdeka) {
    return 0;
  } else {
    if (vs[rtibi] < vs[rdeka])
      swap(rtibi, rdeka);
    vs[rtibi] = vs[rdeka] + vs[rtibi];
    v[rdeka] = rtibi;
  }
  return 0;
}

int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    v[i] = i;
    vs[i] = 1;
  }

  for (int i = 0; i < M; i++) {
    int tmp, tmp1;
    cin >> tmp >> tmp1;
    margego(tmp - 1, tmp1 - 1);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, vs[i]);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ALL(x) x.begin(), x.end()
#define MOD (1000000000 + 7)
using namespace std;
#define PI 3.14159265359
#define tMOD 998244353
#define mkpr(x, y) make_pair(x, y)
int N, M;
int v[200400];
int vs[203000] = {0};

int root(int i) {
  if (v[i] == i) {
    return i;
  } else {
    return root(v[i]);
  }
}

int margego(int tibi, int deka) {
  int rtibi = root(tibi), rdeka = root(deka);
  if (rtibi == rdeka) {
    return 0;
  } else {
    if (vs[rtibi] > vs[rdeka])
      swap(rtibi, rdeka);
    vs[rtibi] = vs[rdeka] + vs[rtibi];
    v[rdeka] = rtibi;
  }
  return 0;
}

int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    v[i] = i;
    vs[i] = 1;
  }

  for (int i = 0; i < M; i++) {
    int tmp, tmp1;
    cin >> tmp >> tmp1;
    margego(tmp - 1, tmp1 - 1);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, vs[i]);
  }

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

#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define pb push_back
using namespace std;
int fun(vector<vector<int>> &a, int p, vector<int> &v, int n) {
  v[p] = 1;
  int c = 1;
  for (int i = 0; i < a[p].size(); i++) {
    if (v[a[p][i]] == 0)
      c += fun(a, a[p][i], v, n);
  }
  return c;
}
int dfs(vector<vector<int>> &a, int p, int n) {
  int m = 0;
  vector<int> v(n, 0);
  for (int i = 0; i < n; i++) {
    if (v[i] == 0) {
      int t = fun(a, i, v, n);
      // cout<<t<<" ";
      if (t > 0 && t > m)
        m = t;
    }
  }
  return m;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n);
  set<pair<int, int>> t;
  for (int i = 0; i < m; i++) {
    int p, q;
    cin >> p >> q;
    if (t.find(make_pair(p - 1, q - 1)) == t.end() &&
        t.find(make_pair(q - 1, p - 1)) == t.end()) {
      a[p - 1].pb(q - 1);
      a[q - 1].pb(p - 1);
      t.insert(make_pair(p - 1, q - 1));
      t.insert(make_pair(q - 1, p - 1));
    }
  }
  cout << dfs(a, 0, n) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define pb push_back
using namespace std;
int fun(vector<vector<int>> &a, int p, vector<int> &v) {
  v[p] = 1;
  int n = a.size(), c = 1;
  for (int i = 0; i < n; i++) {
    if (a[p][i] == 1 && v[i] == 0) {
      c += fun(a, i, v);
    }
  }
  return c;
}
int dfs(vector<vector<int>> &a, int p) {
  int n = a.size(), m = 0;
  vector<int> v(n, 0);
  for (int i = 0; i < n; i++) {
    if (v[i] == 0) {
      int t = fun(a, i, v);
      // cout<<t<<" "
      if (t > 0 && t > m)
        m = t;
    }
  }
  return m;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(n, 0));
  for (int i = 0; i < m; i++) {
    int p, q;
    cin >> p >> q;
    a[p - 1][q - 1] = a[q - 1][p - 1] = 1;
  }
  cout << dfs(a, 0) << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

vector<bool> used(200000, false);
vector<vector<int>> relation(200000);

int dfs(int target) {
  used.at(target) = true;
  int sum = 0;
  for (int next_target : relation.at(target)) {
    if (used.at(next_target))
      continue;
    sum += dfs(next_target);
  }
  return sum + 1;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    relation.at(a - 1).push_back(b - 1);
    relation.at(b - 1).push_back(a - 1);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (used.at(i))
      continue;
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> used(20000, 0);
vector<vector<int>> relation(20000);

int dfs(int target) {
  used.at(target) = 1;
  int sum = 0;
  for (int next_target : relation.at(target)) {
    if (used.at(next_target))
      continue;
    sum += dfs(next_target);
  }
  return sum + 1;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    relation.at(a - 1).push_back(b - 1);
    relation.at(b - 1).push_back(a - 1);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (used.at(i))
      continue;
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

class UnionFind {
public:
  vector<int> parent;
  vector<int> sizes;
  UnionFind(int n) : parent(n), sizes(n, 1) {
    for (int i = 0; i < n; i++)
      parent[i] = i;
  }
  int find(int x) {
    if (x == parent[x])
      return x;
    return parent[x] = find(parent[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sizes[x] < sizes[y])
      swap(x, y);
    parent[y] = x;
    sizes[x] += sizes[y];
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sizes[find(x)]; }
};

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

  return 0;
}

// EOF

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

class UnionFind {
public:
  vector<int> parent;
  vector<int> sizes;
  UnionFind(int n) : parent(n), sizes(n, 1) {
    for (int i = 0; i < n; i++)
      parent[i] = i;
  }
  int find(int x) {
    if (x == parent[x])
      return x;
    return parent[x] = find(parent[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sizes[x] < sizes[y])
      swap(x, y);
    parent[y] = x;
    sizes[x] += sizes[y];
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sizes[find(x)]; }
};

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

  return 0;
}

// EOF
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll dep[200001];
ll visited[200001] = {0};
vector<vector<ll>> v(200001);
void dfs(ll node) {
  visited[node] = 1;
  dep[node] = 1;
  for (ll child : v[node]) {
    if (visited[child] == 1)
      continue;
    dfs(child);
    dep[node] += dep[child];
  }
}

int main() {
  fastio ll n, t, i, j, x, k, m;
  cin >> n >> m;
  set<pair<ll, ll>> st;

  ll a, b;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    if (!st.count({a, b}) && !st.count({b, a})) {
      v[a].push_back(b);
      v[b].push_back(a);
      st.insert({a, b});
    }
  }

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (visited[i] == 0)
      dfs(i);
    ans = max(ans, dep[i]);
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll dep[200001];
ll visited[200001] = {0};
void dfs(ll node, vector<vector<ll>> v) {
  visited[node] = 1;
  dep[node] = 1;
  for (ll child : v[node]) {
    if (visited[child])
      continue;
    dfs(child, v);
    dep[node] += dep[child];
  }
}

int main() {
  fastio ll n, t, i, j, x, k, m;
  cin >> n >> m;
  set<pair<ll, ll>> st;

  ll a, b;
  vector<vector<ll>> v(n + 1);
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    if (!st.count({a, b}) && !st.count({b, a})) {
      v[a].push_back(b);
      v[b].push_back(a);
      st.insert({a, b});
    }
  }

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (visited[i] == 0)
      dfs(i, v);
    ans = max(ans, dep[i]);
  }
  cout << ans;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

class UnionFind {
public:
  UnionFind(int &n) { r = vector<int>(n + 1, -1); }

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

  void unite(int &x, int &y) {
    x = findRoot(x);
    y = findRoot(y);
    if (x == y)
      return;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return;
  }

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

private:
  vector<int> r;
};

int main() {
  int n, m, a, b;
  cin >> n >> m;

  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, uf.size(i));
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

class UnionFind {
public:
  UnionFind(int &n) { r = vector<int>(n + 1, -1); }

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

  void unite(int &x, int &y) {
    x = findRoot(x);
    y = findRoot(y);
    if (x == y)
      return;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return;
  }

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

private:
  vector<int> r;
};

int main() {
  int n, m, a, b;
  cin >> n >> m;

  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, uf.size(i));
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error


/*
ID: Bipin Singh [bipinsingh061]
LANG: C++
TASK: gift1
*/
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << "\\n" << (#x) << " = " << (x) << "\\n"
#define int long long int

void inputoutput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void dfs(int src, bool *vis, vector<int> v[], int &ans) {
  vis[src] = true;
  // cout<<src<<" ";
  ++ans;

  for (auto it : v[src]) {
    if (!vis[it])
      dfs(it, vis, v, ans);
  }
}

int mod = 1e9 + 7;

void update(vector<int> &D, int l, int r, int x) {
  D[l] += x;
  D[r + 1] -= x;
}

int32_t main() {
  inputoutput();

  int n, m;
  cin >> n;
  cin >> m;

  vector<int> v[200500];
  bool vis[200500];

  memset(vis, false, sizeof(vis));

  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  // for(int i=1 ; i<=n ; ++i)
  // {
  // 	cout<<i<<"->";
  // 	for(auto it : v[i])
  // 		cout<<it<<" ";
  // 	cout<<"\\n";
  // }
  int comp = 0;

  // unordered_set<int> result;

  int sol = INT_MIN;

  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      int ans = 0;
      dfs(i, vis, v, ans);
      sol = max(sol, ans);
      ++comp;
    }
  }

  cout << sol;

  return 0;
}


Error: WA

Fixed submission:


/*
ID: Bipin Singh [bipinsingh061]
LANG: C++
TASK: gift1
*/
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << "\\n" << (#x) << " = " << (x) << "\\n"
#define int long long int

void inputoutput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void dfs(int src, bool *vis, vector<int> v[], int &ans) {
  vis[src] = true;
  // cout<<src<<" ";
  ++ans;

  for (auto it : v[src]) {
    if (!vis[it])
      dfs(it, vis, v, ans);
  }
}

int mod = 1e9 + 7;

void update(vector<int> &D, int l, int r, int x) {
  D[l] += x;
  D[r + 1] -= x;
}

int32_t main() {
  inputoutput();

  int n, m;
  cin >> n;
  cin >> m;

  vector<int> v[200000];
  bool vis[200000];

  memset(vis, false, sizeof(vis));

  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  // for(int i=1 ; i<=n ; ++i)
  // {
  // 	cout<<i<<"->";
  // 	for(auto it : v[i])
  // 		cout<<it<<" ";
  // 	cout<<"\\n";
  // }
  int comp = 0;

  // unordered_set<int> result;

  int sol = INT_MIN;

  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      int ans = 0;
      dfs(i, vis, v, ans);
      sol = max(sol, ans);
      ++comp;
    }
  }

  cout << sol;

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

#include <bits/stdc++.h>
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;
    A--;
    B--;
    UF.unite(A, B);
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

struct Node {
  vector<int> f;

  Node(int N) { f = vector<int>(N, -1); }

  int fat(int x) {
    if (f[x] < 0)
      return x;
    return f[x] = fat(f[x]);
  }

  bool unite(int x, int y) {
    x = fat(x), y = fat(y);
    if (x == y)
      return false;

    if (f[x] > f[y])
      swap(x, y);

    f[x] += f[y];
    f[y] += x;

    return true;
  }

  int size(int x) { return -f[fat(x)]; }
};

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

  Node node(N);

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--, B--;
    node.unite(A, B);
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, node.size(i));
  }

  cout << ans << "\\n";
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

// 無向グラフ
using Graph = vector<vector<int>>;

void DFS(Graph &g, vector<bool> &seen, int p, int &counter) {
  seen[p] = true;
  for (auto &&gi : g[p]) {
    if (seen[gi])
      continue;
    counter++;
    DFS(g, seen, gi, counter);
  }
}

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

  vector<bool> seen(n, false);
  vector<int> csizes;
  for (int v = 0; v < n; v++) {
    if (seen[v])
      continue;
    int nowsize = 1;
    DFS(g, seen, v, nowsize);
    csizes.push_back(nowsize);
  }

  // printvec(csizes);

  int maxsize = 0;
  for (auto &&csize : csizes) {
    maxsize = max(csize, maxsize);
  }

  int ans = maxsize;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

// 無向グラフ
using Graph = vector<vector<int>>;

void DFS(Graph &g, vector<bool> &seen, int p) {
  seen[p] = true;
  for (auto &&gi : g[p]) {
    if (seen[gi])
      continue;
    DFS(g, seen, gi);
  }
}

// 連結成分数
int consize(vector<bool> &seen) {
  int ret = 0;
  for (auto &&s : seen) {
    if (s)
      ret++;
  }
  return ret;
}

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

  vector<bool> seen(n, false);
  vector<int> csizes;
  int prevconsize = 0;
  for (int v = 0; v < n; v++) {
    if (seen[v])
      continue;
    DFS(g, seen, v);
    int nowsize = consize(seen) - prevconsize;
    csizes.push_back(nowsize);
    prevconsize += nowsize;
  }

  int maxsize = 0;
  for (auto &&sizes : csizes) {
    maxsize = max(sizes, maxsize);
  }

  int ans = maxsize;
  cout << ans << endl;

  return 0;
}