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

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

class UnionFind {
public:
  vector<int> par;  // 各元の親を表す配列
  vector<int> rank; // 素集合のランクを表す配列(0 で初期化)

  UnionFind(int n) : par(n), rank(n) { init(n); }

  void init(int n) {
    par.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i;
    rank.assign(n, 0);
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return root(par[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else { // rank[y] < rank[x]
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
    return true;
  }

  bool isSame(int x, int y) { // 連結判定
    return root(x) == root(y);
  }

  int getRank(int x) { // 素集合のランク
    return rank[root(x)];
  }
};

int main() {

  // 頂点数と辺数
  int n, m;
  cin >> n >> m;
  UnionFind tree(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree.unite(a, b);
  }
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    mp[tree.root(i)]++;
  }
  int ans = 0;
  for (auto u : mp) {
    ans = max(ans, u.second);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  // 頂点数と辺数
  int n, m;
  cin >> n >> m;

  vector<int> num(n, 0);

  // グラフ入力受取 (ここでは無向グラフを想定)
  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);
  }
  int ans = 0;
  vector<bool> used(n, false);
  for (int i = 0; i < n; i++) {

    if (used[i])
      continue;
    used[i] = true;
    int tmpAns = 0;

    // BFS のためのデータ構造
    vector<int> dist(n, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[i] = 0;
    que.push(i); // 0 を橙色頂点にする

    //// BFS 開始 (キューが空になるまで探索を行う)
    // while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int new_v : G[v]) {
      if (dist[new_v] != -1)
        continue; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 new_v について距離情報を更新してキューに追加する
      dist[new_v] = dist[v] + 1;
      que.push(new_v);

      tmpAns++;
      used[new_v] = true;
    }
    //}
    ans = max(ans, tmpAns);
  }

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

#include <bits/stdc++.h>
using namespace std;
const int maxx = 200010;
set<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 mx = 0;
  for (int i = 1; i <= n; ++i) {
    mx = max(ans[i], mx);
  }
  cout << mx << endl;
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  int m = K;
  while (m--) {
    int u, v;
    cin >> u >> v;
    graph[u].insert(v);
    graph[v].insert(u);
  }
  MaximumVisit(N);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
set<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 mx = 0;
  for (int i = 1; i <= n; ++i) {
    mx = max(ans[i], mx);
  }
  cout << mx << endl;
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  int m = K;
  while (m--) {
    int u, v;
    cin >> u >> v;
    graph[u].insert(v);
    graph[v].insert(u);
  }
  MaximumVisit(N);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, ans, fa[N], sum[N];

inline int Find(int x) {
  if (x == fa[x])
    return x;
  return fa[x] = Find(fa[x]);
}

inline void Union(int x, int y) {
  x = Find(x), y = Find(y);
  if (x != y) {
    fa[y] = x;
    sum[x] += sum[y];
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    sum[i] = 1;
  }

  while (m--) {
    int a, b;
    cin >> a >> b;
    Union(a, b);
  }
  for (int i = 1; i <= n; i++)
    if (Find(i) == i)
      ans = max(ans, sum[i]);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, m, ans = 0, fa[N], sum[N];

inline int Find(int x) {
  if (x != fa[x])
    fa[x] = Find(fa[x]);
}

inline void add(int x, int y) {
  x = Find(x), y = Find(y);
  if (x != y) {
    fa[y] = x;
    sum[x] += sum[y];
  }
}

void init() {}

int main() {
  cin >> n >> m;
  for (int i = 0; i <= n; i++)
    fa[i] = i, sum[i] = 1;

  while (m--) {
    int u, v;
    cin >> u >> v;
    add(u, v);
  }
  for (int i = 1; i <= n; i++)
    if (Find(i) == i)
      ans = max(ans, sum[i]);
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
const ll mod = 1e9 + 7;
const int N = 2e5 + 205;
vector<int> v[N];
bool vis[N];
int sz;
int ans;
void dfs(int x) {
  vis[x] = true;
  sz++;
  ans = max(ans, sz);
  for (int y : v[x]) {
    if (!vis[y])
      dfs(y);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sz = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
const ll mod = 1e9 + 7;
const int N = 1e5 + 205;
vector<int> v[N];
bool vis[N];
int sz;
int ans;
void dfs(int x) {
  vis[x] = true;
  sz++;
  ans = max(ans, sz);
  for (int y : v[x]) {
    if (!vis[y])
      dfs(y);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sz = 0;
      dfs(i);
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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++)
#define vv(T) vector<vector<T>>
using namespace std;
using ll = long long;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using vbool = vector<bool>;
using vvbool = vector<vector<bool>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

struct UnionFind {
  vector<int> parent;
  // parent[i] = jのとき、iの親はj
  // 負の値のとき根であり、その絶対値はは木の要素数
  UnionFind(int N) : parent(N, -1) {
    // 初期化
  }
  int root(int x) {
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }
  void unite(int x, int y) {
    // xとyの木を結合
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (size(x) > size(y)) {
      // 小さい方の木を大きい方につなげる
      // サイズは負の値で管理している
      parent[rx] += parent[ry];
      parent[ry] = rx;
    } else {
      parent[ry] += parent[rx];
      parent[rx] = ry;
    }
    return;
  }
  bool isSame(int x, int y) {
    // 同じ木に所属するか?
    return root(x) == root(y);
  }
  int size(int x) {
    int rx = root(x);
    return -parent[rx];
  }
};

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

  UnionFind uf(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }

  int ans = 0;
  rep(i, n) { ans = max(ans, uf.size(i)); }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vv(T) vector<vector<T>>
using namespace std;
using ll = long long;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using vbool = vector<bool>;
using vvbool = vector<vector<bool>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

queue<int> searching;
int ans = 0;

int BFS(vector<int> &hisGroup, int presentGroupNum,
        vector<vector<bool>> &isDirectFriend) {
  int people = 0;
  while (!searching.empty()) {
    int presentPerson = searching.front();
    searching.pop();
    if (hisGroup[presentPerson] != -1) {
      continue;
    }

    hisGroup[presentPerson] = presentGroupNum;
    people++;
    for (int i = 0; i < hisGroup.size(); i++) {
      if (isDirectFriend[presentPerson][i]) {
        searching.push(i);
      }
    }
  }
  ans = max(ans, people);
  return 0;
}

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

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

  // vector<bool> isChecked(n, false);
  vector<int> hisGroup(n, -1);
  int presentGroupNum = 0;
  {
    for (int i = 0; i < n; i++) {
      auto presentPerson = i;
      if (hisGroup[presentPerson] == -1) {
        searching.push(presentPerson);
        BFS(hisGroup, presentGroupNum, isDirectFriend);
        presentGroupNum++;
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  vector<vector<int>> f(N);
  for (int i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    int a = A.at(i) - 1;
    int b = B.at(i) - 1;
    f[a].push_back(b);
    f[b].push_back(a);
  }
  int ans = 0;
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    if (seen.at(i))
      continue;
    seen.at(i) = true;
    int num = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int j = q.front();
      q.pop();
      for (auto v : f[j]) {
        if (seen.at(v))
          continue;
        seen.at(v) = true;
        q.push(v);
        num++;
      }
    }
    ans = max(ans, num);
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  vector<vector<int>> f(N);
  for (int i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    int a = A.at(i) - 1;
    int b = B.at(i) - 1;
    if (find(f[a].begin(), f[a].end(), b) == f[a].end()) {
      f[a].push_back(b);
    }
    if (find(f[b].begin(), f[b].end(), a) == f[b].end()) {
      f[b].push_back(a);
    }
  }
  int ans = 0;
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    if (seen.at(i))
      continue;
    seen.at(i) = true;
    int num = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int j = q.front();
      q.pop();
      for (auto v : f[j]) {
        if (seen.at(v))
          continue;
        seen.at(v) = true;
        q.push(v);
        num++;
      }
    }
    ans = max(ans, num);
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

int main() {
  int64_t N, M;

  std::cin >> N >> M;

  std::map<int64_t, std::set<int64_t>> G;

  for (int64_t m = 0; m < M; ++m) {
    int64_t a, b;
    std::cin >> a >> b;
    G[a].insert(b);
    G[b].insert(a);
  }

  std::set<int64_t> r;
  for (int64_t i = 1; i <= N; ++i) {
    r.insert(i);
  }
  size_t m = 0;
  while (true) {
    if (r.empty())
      break;

    std::set<int64_t> g1;
    std::set<int64_t> g2;
    g2.insert(*r.begin());
    int64_t s = *r.begin();
    r.erase(s);
    while (!g2.empty()) {
      int64_t s = *g2.rbegin();
      g2.erase(s);
      g1.insert(s);
      auto x = G[s];

      for (auto xx : x) {
        if (g1.find(xx) == g1.end()) {
          g2.insert(xx);
        }
      }
    }
    for (auto gg : g1) {
      r.erase(gg);
    }
    m = std::max(m, g1.size());
  }

  std::cout << m << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

int main() {
  int64_t N, M;

  std::cin >> N >> M;

  std::map<int64_t, std::set<int64_t>> G;

  for (int64_t m = 0; m < M; ++m) {
    int64_t a, b;
    std::cin >> a >> b;
    G[a].insert(b);
    G[b].insert(a);
  }

  std::set<int64_t> r;
  for (int64_t i = 1; i <= N; ++i) {
    r.insert(i);
  }
  size_t m = 0;
  while (true) {
    if (r.empty())
      break;

    std::set<int64_t> g1;
    std::set<int64_t> g2;
    g2.insert(*r.begin());
    int64_t s = *r.begin();
    r.erase(s);
    while (!g2.empty()) {
      int64_t s = *g2.rbegin();
      g2.erase(s);
      g1.insert(s);
      auto r = G[s];

      for (auto rr : r) {
        if (g1.find(rr) == g1.end()) {
          g2.insert(rr);
        }
      }
    }
    m = std::max(m, g1.size());
  }

  std::cout << m << std::endl;

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

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

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);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A -= 1;
    B -= 1;
    UF.unite(A, B);
  }

  // 最大の友達集合を求める
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() { // main_program
  int N, M, max;
  max = 0;

  cin >> N >> M;
  vector<int> A(N);

  for (int i = 0; i < N; i++) {
    A.at(i) = i;
  }

  for (int i = 0; i < M; i++) {
    int k, l;
    cin >> k >> l;

    if (A.at(k - 1) != A.at(l - 1)) {
      if (A.at(k - 1) < A.at(l - 1)) {
        for (int h = A.at(k - 1); h < N; h++) {
          if (A.at(h) == A.at(l - 1)) {
            A.at(h) = A.at(k - 1);
          }
        }
      } else {
        for (int h = A.at(l - 1); h < N; h++) {
          if (A.at(h) == A.at(k - 1)) {
            A.at(h) = A.at(l - 1);
          }
        }
      }
    }
    // cout << i << ":"  <<endl;
    // for(int l = 0;l<N;l++){
    //   cout << l << ":" << A.at(l) <<endl;
    // }
    // cout <<endl;
  }

  for (int i = 0; i < N; i++) {
    if (A.at(i) == i) {
      int count = 0;
      for (int l = i; l < N; l++) {
        if (A.at(i) == A.at(l)) {
          count++;
        }
      }
      if (count > max) {
        max = count;
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

vector<vector<int>> G;
vector<bool> seen;
vector<int> group_size;

int cnt;

void dfs(int v) {
  cnt++;
  seen[v] = true;
  for (int nv : G[v]) {
    if (seen[nv])
      continue;
    dfs(nv);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  G.resize(N);
  seen.resize(N, false);
  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);
  }
  for (int i = 0; i < N; i++) {
    if (seen[i])
      continue;
    cnt = 0;
    dfs(i);
    group_size.push_back(cnt);
  }
  sort(group_size.begin(), group_size.end());
  int s = group_size.size();
  int ans = 0;

  /*
  これはTLEするからボツ

  for (int i = 0; i < s - 1; i++) {
          int num = group_size[i];
          for (int j = i; j < s; j++) group_size[j] -= num;
          ans += num;
  }
  if (group_size[s - 1] != 0) ans += group_size[s - 1];
  */

  int num = 0;
  for (int i = 0; i < s; i++) {
    if (group_size[i] > num) {
      ans += (group_size[i] - num);
      num = group_size[i];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

vector<vector<int>> G;
vector<bool> seen;
vector<int> group_size;

int cnt;

void dfs(int v) {
  cnt++;
  seen[v] = true;
  for (int nv : G[v]) {
    if (seen[nv])
      continue;
    dfs(nv);
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  G.resize(N);
  seen.resize(N, false);
  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);
  }
  for (int i = 0; i < N; i++) {
    if (seen[i])
      continue;
    cnt = 0;
    dfs(i);
    group_size.push_back(cnt);
  }
  sort(group_size.begin(), group_size.end());
  int s = group_size.size();
  int ans = 0;
  for (int i = 0; i < s - 1; i++) {
    int num = group_size[i];
    for (int j = i; j < s; j++)
      group_size[j] -= num;
    ans += num;
  }
  if (group_size[s - 1] != 0)
    ans += group_size[s - 1];

  cout << ans << endl;

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

std::vector<int> v[200001];
int sz[200001];

void dfs(int index, int &cnt, std::vector<int> &mark) {
  // cout << "--- index:" << index << ", cnt:" << cnt << endl;
  // int sz = v[index].size();
  for (int i = 0; i < sz[index]; i++) {
    if (mark[v[index][i]] == 0) {
      mark[v[index][i]] = 1;
      cnt++;
      // cout << "    next:" << v[index][i] << endl;
      dfs(v[index][i], cnt, mark);
    }
  }
}

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

  // std::vector<int> a, b;

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

  for (int i = 0; i <= n; i++) {
    std::sort(v[i].begin(), v[i].end());
    v[i].erase(std::unique(v[i].begin(), v[i].end()), v[i].end());
    sz[i] = v[i].size();
    // cout << v[i].size() << endl;
  }

  int maxcnt = 0;
  std::vector<int> mark(n + 1);
  for (int i = 0; i < n; i++) {

    int cnt = 1;
    mark[i + 1] = 1;
    // cout << "***" << endl;
    dfs(i + 1, cnt, mark);
    if (maxcnt < cnt)
      maxcnt = cnt;
  }

  cout << maxcnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

std::vector<int> v[200001];
int sz[200001];

void dfs(int index, int &cnt, std::vector<int> &mark) {
  // cout << "--- index:" << index << ", cnt:" << cnt << endl;
  // int sz = v[index].size();
  for (int i = 0; i < sz[index]; i++) {
    if (mark[v[index][i]] == 0) {
      mark[v[index][i]] = 1;
      cnt++;
      // cout << "    next:" << v[index][i] << endl;
      dfs(v[index][i], cnt, mark);
    }
  }
}

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

  // std::vector<int> a, b;

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

  for (int i = 0; i <= n; i++) {
    std::sort(v[i].begin(), v[i].end());
    v[i].erase(std::unique(v[i].begin(), v[i].end()), v[i].end());
    sz[i] = v[i].size();
    // cout << v[i].size() << endl;
  }

  int maxcnt = 0;
  for (int i = 0; i < n; i++) {
    std::vector<int> mark(n + 1);
    int cnt = 1;
    mark[i + 1] = 1;

    dfs(i + 1, cnt, mark);
    if (maxcnt < cnt)
      maxcnt = cnt;
  }

  cout << maxcnt << endl;

  return 0;
}