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

#include <bits/stdc++.h>

using Graph = std::vector<std::vector<int>>;

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

  std::set<std::pair<int, int>> v_pair;

  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    std::cin >> a >> b;
    --a, --b;

    std::pair<int, int> p1(a, b);
    std::pair<int, int> p2(b, a);

    if (v_pair.count(p1) || v_pair.count(p2))
      continue;
    v_pair.insert(p1);

    G[a].push_back(b);
    G[b].push_back(a);
  }

  std::vector<int> dist(N, -1);
  std::queue<int> que;

  int ans = 0;

  for (int v = 0; v < N; ++v) {
    if (dist[v] != -1)
      continue; // vは探索済み
    dist[v] = 0;
    que.push(v);
    int count = 0;
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      ++count;
      for (auto v_next : G[v]) {
        if (dist[v_next] != -1)
          continue;
        dist[v_next] = dist[v] + 1;
        que.push(v_next);
        // ++count;
      }
    }
    ans = std::max(ans, count);
  }

  if (M == 0)
    ans = 1;

  std::cout << ans << std::endl << std::flush;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using Graph = std::vector<std::vector<int>>;

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

  std::set<std::pair<int, int>> v_pair;

  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    std::cin >> a >> b;
    std::pair<int, int> p1(a, b);
    std::pair<int, int> p2(b, a);

    if (v_pair.count(p1) || v_pair.count(p2))
      continue;
    v_pair.insert(p1);

    G[a].push_back(b);
    G[b].push_back(a);
  }

  std::vector<int> dist(N, -1);
  std::queue<int> que;

  int ans = 0;

  for (int v = 0; v < N; ++v) {
    if (dist[v] != -1)
      continue; // vは探索済み
    dist[v] = 0;
    que.push(v);
    int count = 0;
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (auto v_next : G[v]) {
        if (dist[v_next] != -1)
          continue;
        dist[v_next] = dist[v] + 1;
        que.push(v_next);
        ++count;
      }
    }
    ans = std::max(ans, count);
  }

  std::cout << ans << std::endl << std::flush;

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

#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 9;
int p[MAXN], r[MAXN];
unordered_map<int, int> mp;
// mp.reserve(MAXN);

void init() {
  for (int i = 0; i < MAXN; ++i) {
    p[i] = i;
    r[i] = 1;
  }
  // mp.reserve(MAXN);
  mp.clear();
}

int find(int u) {
  if (p[u] != u)
    p[u] = find(p[u]);
  return p[u];
}

void unionFunc(int u, int v) {
  int pu = find(u);
  int pv = find(v);
  if (pu == pv)
    return;
  if (r[pv] == r[pu]) {
    p[pv] = pu;
    ++r[pu];
  } else if (r[pu] < r[pv]) {
    p[pu] = pv;
  } else
    p[pv] = pu;
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  init();
  int N, M;
  cin >> N >> M;
  // cout << "N: " << N << " M: " << M << '\\n';
  long long ans = 0;
  // unordered_map<int, int> pp;
  // pp.clear();
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    // int pa = find(A);
    // int pb = find(B);
    // A = min(A, B);
    // B = max(A, B);
    // if(pp.find(A) == pp.end()){
    unionFunc(A, B);
    // ++pp[A];
    // }
    // cout << "UNION\\n";
  }
  mp.clear();
  for (int i = 0; i <= N; ++i) {
    ++mp[(int)find(i)];
    // cout << "FIND\\n";
  }

  for (auto it = mp.begin(); it != mp.end(); ++it) {
    // if(it->second == ans)
    // ans = max(ans, (long long)it->second) + 1;
    ans = max(ans, (long long)it->second);
    // cout << "MP\\n";
  }

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

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 9;
int n, m;
int p[MAXN], r[MAXN];

void init() {
  for (int i = 0; i < MAXN; ++i) {
    p[i] = i;
    r[i] = 0;
  }
}

int find(int u) {
  if (p[u] != u)
    p[u] = find(p[u]);
  return p[u];
}

void unionFunc(int u, int v) {
  int pu = find(u);
  int pv = find(v);

  if (r[pv] == r[pu]) {
    p[v] = u;
    ++r[u];
  } else if (r[pu] < r[pv]) {
    p[u] = pv;
  } else
    p[v] = pu;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  init();
  int N, M;
  cin >> N >> M;
  int ans = 0;
  unordered_map<int, int> mp;
  mp.reserve(MAXN);
  mp.clear();
  for (int i = 0; i < M; ++i) {
    int A, B;
    cin >> A >> B;
    // int pa = find(A);
    // int pb = find(B);
    unionFunc(A, B);
  }
  for (int i = 0; i <= N; ++i) {
    ++mp[find(i)];
  }

  for (auto it = mp.begin(); it != mp.end(); ++it) {
    ans = max(ans, (int)it->second);
  }

  cout << ans << '\\n';
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int N = 2e5 + 5;

typedef long long ll;
int father[N];
int height[N];
int people[N];

int find(int x) {
  if (x != father[x])
    father[x] = find(father[x]);

  return father[x];
}

// 合并并返回合并后的祖先序号
void Union(int x, int y) {
  x = find(x);
  y = find(y);

  if (height[x] > height[y]) {
    father[y] = x;

    // 两者祖先相同时，实际没发生合并，只考虑祖先不同的情况
    if (x != y)
      people[x] += people[y];
  } else {
    father[x] = y;

    // 两者祖先相同时，实际没发生合并，只考虑祖先不同的情况
    if (x != y)
      people[y] += people[x];

    if (height[x] == height[y]) // 树高相同时让父节点的树高值加一
      height[y] += 1;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    father[i] = i;
    height[i] = 0;
    people[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    Union(a, b);
  }
  int cnt = 1;
  for (int i = 1; i <= n; i++)
    if (people[i] > cnt)
      cnt = people[i];
  cout << cnt;
}


Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 5;

typedef long long ll;
int father[N];
int height[N];
int people[N];

int find(int x) {
  if (x != father[x])
    father[x] = find(father[x]);

  return father[x];
}

// 合并并返回合并后的祖先序号
void Union(int x, int y) {
  x = find(x);
  y = find(y);

  if (height[x] > height[y]) {
    father[y] = x;

    // 两者祖先相同时，实际没发生合并，只考虑祖先不同的情况
    if (x != y)
      people[x] += people[y];
  } else {
    father[x] = y;

    // 两者祖先相同时，实际没发生合并，只考虑祖先不同的情况
    if (x != y)
      people[y] += people[x];

    if (height[x] == height[y]) // 树高相同时让父节点的树高值加一
      height[y] += 1;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    father[i] = i;
    height[i] = 0;
    people[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    Union(a, b);
  }
  int cnt = 1;
  for (int i = 1; i <= n; i++)
    if (people[i] > cnt)
      cnt = people[i];
  cout << cnt;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

// time-limit: 2000
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2e5 + 10;
bool vis[mxN];
vector<int> adj[mxN];
int cnt;

void dfs(int i) {
  if (vis[i])
    return;
  vis[i] = true;
  cnt++;
  for (auto &u : adj[i]) {
    dfs(u);
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].emplace_back(b);
    adj[b].emplace_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (vis[i])
      continue;
    cnt = 0;
    dfs(i);
    ans = max(ans, cnt);
  }
  cout << ans << "\\n";
}

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


Error: 0

Fixed submission:

// time-limit: 2000
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2e5;
bool vis[mxN];
vector<int> adj[mxN];
int cnt;

void dfs(int i) {
  if (vis[i])
    return;
  vis[i] = true;
  cnt++;
  for (auto &u : adj[i]) {
    dfs(u);
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    --a;
    --b;
    cin >> a >> b;
    adj[a].emplace_back(b);
    adj[b].emplace_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (vis[i])
      continue;
    cnt = 0;
    dfs(i);
    ans = max(ans, cnt);
  }
  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // 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;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

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 (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};

int main(void) {
  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 res = 0;
  rep(i, N) { chmax(res, uf.size(i)); }
  cout << res << 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;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

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) {
    if (x == y)
      return false;
    if (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};

int main(void) {
  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 res = 0;
  rep(i, N) { chmax(res, uf.size(i)); }
  cout << res << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long
#define f first
#define s second
#define inf 1e15

#define N 300005

string s;
ll n, m, x, y, d, k;
ll a[N];

ll parent[N];
ll siz[N];

int find(int g) {

  if (parent[g] != g) {
    parent[g] = find(parent[g]);
  }
  return parent[g];
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      parent[i] = i;
      siz[i] = 1;
    }

    for (int i = 0; i < m; i++) {

      int x, y;
      ;
      cin >> x >> y;
      x--;
      y--;

      x = find(x);
      y = find(y);

      if (x == y)
        continue;

      parent[x] = y;
      siz[y] += siz[x];
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, siz[find(i)]);
    }

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


Error: WA

Fixed submission:

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

#define ll long long
#define f first
#define s second
#define inf 1e15

#define N 300005

string s;
ll n, m, x, y, d, k;
ll a[N];

ll parent[N];
ll siz[N];

int find(int g) {

  if (parent[g] != g) {
    parent[g] = find(parent[g]);
  }
  return g;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      parent[i] = i;
      siz[i] = 1;
    }

    for (int i = 0; i < m; i++) {

      int x, y;
      ;
      cin >> x >> y;
      x--;
      y--;

      x = find(x);
      y = find(y);

      if (x == y)
        continue;

      parent[x] = y;
      siz[y] += siz[x];
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, siz[find(i)]);
    }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\\n'
#define dc " "
#define Truong_Thanh_Minh                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef pair<ll, ll> ii;
const ll maxn = 2e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, m, x, y, ok[maxn], ngroup, cnt[maxn], group[maxn], res;
vector<ll> a[maxn];
void dfs(int i) {
  ok[i] = 1;
  group[i] = ngroup;
  ++cnt[ngroup];
  for (int j : a[i])
    if (!ok[j])
      dfs(j);
}
int main() {
  Truong_Thanh_Minh
          // freopen(name".inp","r",stdin);
          // freopen(name".out","w",stdout);
          cin >>
      n >> m;
  rep(i, 1, m) {
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }
  rep(i, 1, n) {
    if (!ok[i]) {
      ++ngroup;
      dfs(i);
    }
  }
  rep(i, 1, ngroup) res = max(res, cnt[i]);
  cout << res;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\\n'
#define dc " "
#define Truong_Thanh_Minh                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef pair<ll, ll> ii;
const ll maxn = 1e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, m, x, y, ok[maxn], ngroup, cnt[maxn], group[maxn], res;
vector<ll> a[maxn];
void dfs(int i) {
  ok[i] = 1;
  group[i] = ngroup;
  ++cnt[ngroup];
  for (int j : a[i])
    if (!ok[j])
      dfs(j);
}
int main() {
  Truong_Thanh_Minh
          // freopen(name".inp","r",stdin);
          // freopen(name".out","w",stdout);
          cin >>
      n >> m;
  rep(i, 1, m) {
    cin >> x >> y;
    a[x].pb(y);
    a[y].pb(x);
  }
  rep(i, 1, n) {
    if (!ok[i]) {
      ++ngroup;
      dfs(i);
    }
  }
  rep(i, 1, ngroup) res = max(res, cnt[i]);
  cout << res;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long
// #define endl '\\n'

void max_self(int &a, int b) { a = max(a, b); }
// void min_self(int &a, int b){a = min(a, b);}

const int nax = 2e5 + 5;
vector<int> G[nax], vis(nax);
int cnt;

void dfs(int node) {
  vis[node] = 1;
  cnt++;
  for (int i : G[node]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      max_self(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define ll long long
// #define endl '\\n'

void max_self(int &a, int b) { a = max(a, b); }
// void min_self(int &a, int b){a = min(a, b);}

const int nax = 1e5 + 5;
vector<int> G[nax], vis(nax);
int cnt;

void dfs(int node) {
  vis[node] = 1;
  cnt++;
  for (int i : G[node]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      max_self(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int sz[M], fa[M], n, m;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    sz[i] = 1, fa[i] = i;
  for (int i = 0, x, y; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = find(x), y = find(y);
    if (x != y)
      fa[y] = x, sz[x] += sz[y];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, sz[i]);
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int sz[M], fa[M], n, m;
int find(int x) { return fa[x] == x ? x : find(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    sz[i] = 1, fa[i] = i;
  for (int i = 0, x, y; i < m; i++) {
    scanf("%d%d", &x, &y);
    x = find(x), y = find(y);
    if (x != y)
      fa[y] = x, sz[x] += sz[y];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, sz[i]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
bool visited[1000000] = {0};
ll ans;
ll c;
void dfs(vector<vector<ll>> &ans, ll n, ll pos) {
  if (visited[pos])
    return;
  visited[pos] = 1;
  c++;
  for (auto i = ans[pos].begin(); i != ans[pos].end(); i++) {
    if (ans[pos].size() == 0)
      return;
    if (visited[*i])
      continue;
    dfs(ans, n, *i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return 0;
  }
  vector<vector<ll>> adj(n + 1);
  ll a, b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    dfs(adj, n, i);
    ans = max(ans, c);
    c = 0;
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
bool visited[1000000] = {0};
ll ans;
ll c;
void dfs(vector<vector<ll>> ans, ll n, ll pos) {
  if (visited[pos])
    return;
  visited[pos] = 1;
  c++;
  for (auto i = ans[pos].begin(); i != ans[pos].end(); i++) {
    if (ans[pos].size() == 0)
      return;
    if (visited[*i])
      continue;
    dfs(ans, n, *i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << 1;
    return 0;
  }
  vector<vector<ll>> adj(n + 1);
  ll a, b;
  while (m--) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    dfs(adj, n, i);
    ans = max(ans, c);
    c = 0;
  }
  cout << ans;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_1 (i, j, n) for (int i = j; i < n; i++)
typedef long long ll;
typedef long l;
const ll l_INF = 1010101010101010;
const int INF = 1010101010;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

struct UnionFind {
  // 自身が親なら、その集合に属する頂点数に-1をかけたもの
  // そうでなければ親のid
  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);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    UF.unite(a, b);
  }
  // 最大の友達集合を求める
  int ans = 0;
  rep(i, n) ans = max(ans, UF.size(i));
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_1 (i, j, n) for (int i = j; i < n; i++)
typedef long long ll;
typedef long l;
const ll l_INF = 1010101010101010;
const int INF = 1010101010;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
  int n, m;
  cin >> n >> m;
  // vector<vector<int>> g(n);
  vector<set<int>> g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    // g[a].push_back(b);
    // g[b].push_back(a);
    if (!(g[a].count(b)))
      g[a].insert(b);
    if (!(g[b].count(a)))
      g[b].insert(a);
  }
  int ans = 1;
  /*vi dist(n);
  rep(i,n) dist[i]=INF;
  rep(i,n){
    if(dist[i]!=INF) continue;
    dist[i]=0;
    int cnt=1;
    for(auto x:g[i]){
      if(dist[x]!=INF) continue;
      dist[x]=0;
      cnt++;
    }
    ans=max(cnt,ans);
  }*/
  vi y(n);
  rep(i, n) y[i] = g[i].size() + 1;
  rep(i, n) rep(i, n) ans = max(ans, y[i]);
  cout << ans << endl;
}