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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> lP;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> frie(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    frie[a].push_back(b);
    frie[b].push_back(a);
  }

  queue<ll> que;
  ll ans = 1;

  vector<bool> dist(n, false);

  rep(i, n) {
    que.push(i);
    if (dist[i])
      continue;
    dist[i] = true;
    ll count = 1;
    while (!que.empty()) {
      ll v = que.front();
      que.pop();
      for (auto next : frie[v]) {
        if (dist[next])
          continue;
        dist[next] = true;
        count++;
        ans = max(ans, count);
        que.push(next);
      }
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> lP;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> frie(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    frie[a].push_back(b);
    frie[b].push_back(a);
  }

  queue<ll> que;
  ll ans = 1;

  rep(i, n) {
    que.push(i);
    vector<bool> dist(n, false);
    dist[i] = true;
    ll count = 1;
    while (!que.empty()) {
      ll v = que.front();
      que.pop();
      for (auto next : frie[v]) {
        if (dist[next])
          continue;
        dist[next] = true;
        count++;
        ans = max(ans, count);
        que.push(next);
      }
    }
  }

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

#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;
typedef long long ll;
#define forn(i, n) for (ll i = 0; i < ll(n); i++)

class DisjointSet {
  vector<ll> Parent, Rank;

public:
  DisjointSet(ll n) { forn(i, n + 1) Parent.push_back(i), Rank.push_back(0); }
  ll Find(ll x) {
    if (Parent[x] == x)
      return x;
    else
      Parent[x] = Find(Parent[x]);
    return Parent[x];
  }
  void Union(ll x, ll y) {
    ll u = Find(x);
    ll v = Find(y);
    if (u == v)
      return;
    if (Rank[u] > Rank[v])
      Parent[v] = u;
    else {
      Parent[u] = v;
      if (Rank[u] == Rank[v])
        Rank[u]++;
    }
    return;
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  DisjointSet dis(n);
  ll j, k;
  ll ans = 1;
  forn(i, m) {
    cin >> j >> k;
    dis.Union(j, k);
  }
  map<ll, ll> mp;
  forn(i, n) {
    ll u = dis.Find(i + 1);
    mp[u]++;
    ans = max(mp[u], ans);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;
typedef long long ll;
#define forn(i, n) for (ll i = 0; i < ll(n); i++)

class DisjointSet {
  vector<ll> Parent, Rank;

public:
  DisjointSet(ll n) { forn(i, n + 1) Parent.push_back(i), Rank.push_back(0); }
  ll Find(ll x) {
    while (Parent[x] != x)
      x = Parent[x];
    return x;
  }
  void Union(ll x, ll y) {
    ll u = Find(x);
    ll v = Find(y);
    if (u == v)
      return;
    if (Rank[u] > Rank[v])
      Parent[v] = u;
    else {
      Parent[u] = v;
      if (Rank[u] == Rank[v])
        Rank[u]++;
    }
    return;
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  DisjointSet dis(n);
  ll j, k;
  ll ans = 1;
  forn(i, m) {
    cin >> j >> k;
    dis.Union(j, k);
  }
  map<ll, ll> mp;
  forn(i, n) {
    ll u = dis.Find(i + 1);
    mp[u]++;
    ans = max(mp[u], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using ll = long long;
using namespace std;
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define ar array

const int N = 2e5 + 9;
vector<int> g[N];
bool used[N];
int val[N];
int n, m;

int dfs(int u) {
  used[u] = 1;
  int y = 1;
  for (auto x : g[u]) {
    if (used[x])
      continue;
    y += dfs(x);
  }
  return y;
}

void solve() {
  memset(used, 0, sizeof(used));
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  int ans = -1e9;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int y = dfs(i);
      ans = max(ans, y);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("input.txt",r,"stdin");
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using ll = long long;
using namespace std;
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define ar array

const int N = 1e5 + 9;
vector<int> g[N];
bool used[N];
int val[N];
int n, m;

int dfs(int u) {
  used[u] = 1;
  int y = 1;
  for (auto x : g[u]) {
    if (used[x])
      continue;
    y += dfs(x);
  }
  return y;
}

void solve() {
  memset(used, 0, sizeof(used));
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  int ans = -1e9;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int y = dfs(i);
      ans = max(ans, y);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("input.txt",r,"stdin");
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define foi(n) for (ll i = 0; i < n; i++)
#define foj(n) for (ll j = 0; j < n; j++)
#define pb push_back
using namespace std;

ll a[200005];
vector<ll> v[200005];
bool visited[200005] = {false};

void bfs(ll i, ll cnt) {
  visited[i] = true;
  a[i] = cnt;
  foj(v[i].size()) {
    if (!visited[v[i][j]])
      bfs(v[i][j], cnt);
  }
}

int main() {

  ll n, m;
  cin >> n >> m;

  ll cnt = 1;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      bfs(i, cnt);
      cnt++;
    }
  }
  sort(a + 1, a + n + 1);
  map<ll, ll> ma;
  foi(n) { ma[a[i + 1]]++; }

  ll ans = 0;
  for (auto it : ma) {
    ans = max(ans, it.second);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define foi(n) for (ll i = 0; i < n; i++)
#define foj(n) for (ll j = 0; j < n; j++)
#define pb push_back
using namespace std;

ll a[100005];
vector<ll> v[100005];
bool visited[100005] = {false};

void bfs(ll i, ll cnt) {
  visited[i] = true;
  a[i] = cnt;
  foj(v[i].size()) {
    if (!visited[v[i][j]])
      bfs(v[i][j], cnt);
  }
}

int main() {

  ll n, m;
  cin >> n >> m;

  ll cnt = 1;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i]) {
      bfs(i, cnt);
      cnt++;
    }
  }
  sort(a + 1, a + n + 1);
  map<ll, ll> ma;
  foi(n) { ma[a[i + 1]]++; }

  ll ans = 0;
  for (auto it : ma) {
    ans = max(ans, it.second);
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;
int pre[maxn];
int vis[maxn], Rank[maxn];
void init(int n) {
  for (int i = 1; i <= n; ++i)
    pre[i] = i, Rank[i] = 1;
}
int Find(int x) {
  int t = x;
  while (pre[x] != x) {
    x = pre[x];
  }
  pre[t] = x;
  return x;
}
void un(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x != y) {
    if (Rank[x] <= Rank[y]) {
      pre[x] = y;
      Rank[y] += Rank[x];
    } else {
      pre[y] = x;
      Rank[x] += Rank[y];
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  init(n);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    un(x, y);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    Find(i);
    // cout << i  << " " << pre[i] << "\\n";
    int d = pre[i];
    vis[d]++;
    ans = max(ans, vis[d]);
  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;
int pre[maxn];
LL vis[maxn];
void init(int n) {
  for (int i = 1; i <= n; ++i)
    pre[i] = i;
}
int Find(int x) {
  int t = x;
  while (pre[x] != x) {
    x = pre[x];
  }
  pre[t] = x;
  return x;
}
void un(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x != y) {
    pre[x] = y;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  init(n);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    un(x, y);
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    Find(i);
    // cout << i  << " " << pre[i] << "\\n";
    int d = pre[i];
    vis[d]++;
    ans = max(ans, vis[d]);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define fastIo                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MOD 1000000007

using namespace std;
int par[200005];
int find(int x) {
  if (par[x] < 0)
    return x;
  return par[x] = find(par[x]);
}
void UNION(int a, int b) {
  int parA = find(a);
  int parB = find(b);
  if (parA != parB) {
    par[parB] += par[parA];
    par[parA] = parB;
  }
}
int main() {
  fastIo;

  for (int i = 0; i < 200005; i++)
    par[i] = -1;

  int n;
  cin >> n;

  int m;
  cin >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    UNION(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    if (par[i] < 0)
      ans = max(ans, abs(par[i]));
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define fastIo                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MOD 1000000007

using namespace std;
const int N = 200001;
int par[N];
int find(int x) {
  vector<int> v;
  while (par[x] > 0) {
    // cout<<"s";
    v.pb(x);
    x = par[x];
  }

  for (int i = 0; i < v.size(); i++)
    par[v[i]] = x;
  return x;
}
int UNION(int a, int b) {
  int parA = find(a);
  int parB = find(b);
  if (parA != parB) {
    par[parB] += par[parA];
    par[parA] = parB;
  }
}
int main() {
  fastIo;

  for (int i = 0; i < N; i++)
    par[i] = -1;

  int n;
  cin >> n;

  int m;
  cin >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    UNION(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    if (par[i] < 0)
      ans = max(ans, abs(par[i]));
  }
  cout << ans << endl;

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

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int maxx = 200005;
vector<int> graph[maxx];
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;
  ++cnt;
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

void MaximumVisit(int n) {
  int visited[maxx];
  memset(visited, 0, sizeof visited);
  int ans[maxx];
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }
  int an = 0;
  for (int i = 1; i <= n; ++i) {
    an = max(an, ans[i]);
  }
  cout << an;
  return;
}
int main() {
  int n, k, a, b;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  MaximumVisit(n);
  return 0;
}


Error: WA

Fixed submission:

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
vector<int> graph[maxx];
void dfs(int curr, int &cnt, int *visited, vector<int> &duringdfs) {
  visited[curr] = 1;
  ++cnt;
  duringdfs.push_back(curr);
  for (auto &child : graph[curr]) {
    if (visited[child] == 0) {
      dfs(child, cnt, visited, duringdfs);
    }
  }
}

void MaximumVisit(int n) {
  int visited[maxx];
  memset(visited, 0, sizeof visited);
  int ans[maxx];
  memset(ans, 0, sizeof ans);
  vector<int> duringdfs;
  for (int i = 1; i <= n; ++i) {
    duringdfs.clear();
    int cnt = 0;
    if (visited[i] == 0) {
      cnt = 0;
      dfs(i, cnt, visited, duringdfs);
    }
    for (auto &x : duringdfs) {
      ans[x] = cnt;
    }
  }
  int an = 0;
  for (int i = 1; i <= n; ++i) {
    an = max(an, ans[i]);
  }
  cout << an;
  return;
}
int main() {
  int n, k, a, b;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  MaximumVisit(n);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define ll long long
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define vi vector<int>
#define ff first
#define ss second
#define fr(a, b) for (int i = a; i <= b; i++)
#define inf 1e18
#define mod 1000000007
vector<int> v[200005];
bool vis[200005];
void dfs(int src, int &c) {
  c++;
  vis[src] = true;
  for (auto nvr : v[src]) {
    if (vis[nvr] == false)
      dfs(nvr, c);
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = INT_MIN;
  int c = 0;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define ll long long
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define vi vector<int>
#define ff first
#define ss second
#define fr(a, b) for (int i = a; i <= b; i++)
#define inf 1e18
#define mod 1000000007
vector<int> v[100005];
bool vis[100005];
void dfs(int src, int &c) {
  c++;
  vis[src] = true;
  for (auto nvr : v[src]) {
    if (vis[nvr] == false)
      dfs(nvr, c);
  }
}
int main() {
  ll n, m;
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = INT_MIN;
  int c = 0;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      c = 0;
      dfs(i, c);
      ans = max(ans, c);
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 2000015
const ll MOD = 1000000000 + 7;

int parent[MAX], sz[MAX];

int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

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

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
      make_set(i);

    for (int i = 1; i <= k; i++) {
      int a, b;
      cin >> a >> b;
      union_sets(a, b);
    }
    int mx = -1;
    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;
typedef long long ll;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 2000015
const ll MOD = 1000000000 + 7;

int parent[MAX], sz[MAX];

int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

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

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
      make_set(i);

    for (int i = 0; i <= k; i++) {
      int a, b;
      cin >> a >> b;
      union_sets(a, b);
    }
    int mx = -1;
    for (int i = 1; i <= n; i++)
      mx = max(mx, sz[i]);

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

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

const int N = 2e5 + 100;
int f[N], s[N];

int Find(int x) {
  stack<int> stk;
  while (f[x] != -1)
    stk.push(x), x = f[x];
  while (!stk.empty())
    f[stk.top()] = x, stk.pop();
  return x;
}

int main() {
  fastIO;

  int n, m;
  cin >> n >> m;
  int ans = 1;

  for (int i = 1; i <= n; i++)
    f[i] = -1, s[i] = 1;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a = Find(a);
    b = Find(b);
    if (a == b)
      continue;
    f[a] = b;
    s[b] += s[a];
    ans = max(ans, s[b]);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int Find(int x, vi f) {
  stack<int> stk;
  while (f[x] != -1)
    stk.push(x), x = f[x];
  while (!stk.empty())
    f[stk.top()] = x, stk.pop();
  return x;
}

int main() {
  fastIO;

  int n, m;
  cin >> n >> m;
  int ans = 1;
  vi f(n + 1, -1), s(n + 1, 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a = Find(a, f);
    b = Find(b, f);
    if (a == b)
      continue;
    f[a] = b;
    s[b] += s[a];
    ans = max(ans, s[b]);
  }

  cout << ans << endl;

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

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

bool vis[200005];
vector<int> adj[200005];

int dfs(int i) {

  int ans = 0;

  if (!vis[i]) {
    vis[i] = 1;
    ans++;
    for (auto j : adj[i]) {

      ans += dfs(j);
    }
  }
  return ans;
}

int main() {
  ll n, m, a, b, h = 0;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {

      ll c = dfs(i);

      h = max(c, h);
    }
  }

  cout << h;
}


Error: TLE

Fixed submission:

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

bool vis[10000000] = {0};

int dfs(int i, vector<vector<int>> adj) {

  int ans = 0;
  for (int j = 0; j < adj[i].size(); j++) {
    if (!vis[adj[i][j]]) {
      vis[adj[i][j]] = 1;
      ans++;
      ans += dfs(adj[i][j], adj);
    }
  }
  return ans;
}

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

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      vis[i] = 1;
      ll c = dfs(i, adj);

      h = max(c, h);
    }
  }

  cout << h + 1;
}