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

#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
vector<int> adj[200005];
int cnt;
vector<int> vis(200005, 0);
void dfs(int node) {
  if (!vis[node]) {
    ++cnt;
    vis[node] = 1;
    for (int son : adj[node])
      dfs(son);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
vector<int> adj[200005];
int cnt;
vector<int> vis(200005, 0);
int dfs(int node) {
  if (!vis[node]) {
    ++cnt;
    vis[node] = 1;
    for (int son : adj[node])
      dfs(son);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis[i]) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
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)]; }
  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int N, M, i, p, q, a;
  cin >> N >> M;
  UnionFind tree(N + 10);
  for (i = 0; i < M; i++) {
    cin >> p >> q;
    tree.unite(p, q);
  }
  a = 0;
  vector<bool> sirabe(N + 10, false);
  for (p = 1; p <= N; p++) {
    a = max(a, tree.size(p));
  }
  cout << a << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }
  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int N, M, i, p, q, a;
  cin >> N >> M;
  UnionFind tree(N + 10);
  for (i = 0; i < M; i++) {
    cin >> p >> q;
    tree.unite(p, q);
  }
  a = 0;
  vector<bool> sirabe(N + 10, false);
  for (p = 1; p <= N; p++) {
    if (!(sirabe[p])) {
      q = 0;
      for (i = 1; i <= N; i++) {
        if (tree.same(i, p)) {
          sirabe[i] = true;
          q++;
        }
      }
      if (q > a) {
        a = q;
      }
    }
  }
  cout << a << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define M 1000000007
vector<ll> adj[200006];
ll vis[200006];

void dfs(ll node, vector<ll> &v) {
  vis[node] = 1;
  v.push_back(node);
  for (ll child : adj[node]) {
    if (vis[child] == 0)
      dfs(child, v);
  }
}

int main() {

#ifndef ONLINE_JUDGE

  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

#endif

  fastio;

  ll n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    vis[i] = 0;
    adj[i].clear();
  }
  while (m--) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  ll res = INT_MIN;
  for (int i = 1; i <= n; ++i) {
    if (vis[i] == 0) {
      vector<ll> v;
      dfs(i, v);
      res = max(res, (ll)v.size());
      v.clear();
    }
  }

  cout << res;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define M 1000000007
vector<ll> adj[20000];
ll vis[20000];

void dfs(ll node, vector<ll> &v) {
  vis[node] = 1;
  v.push_back(node);
  for (ll child : adj[node]) {
    if (vis[child] == 0)
      dfs(child, v);
  }
}

int main() {

#ifndef ONLINE_JUDGE

  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

#endif

  fastio;

  ll n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    vis[i] = 0;
    adj[i].clear();
  }
  while (m--) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  ll res = INT_MIN;
  for (int i = 1; i <= n; ++i) {
    if (vis[i] == 0) {
      vector<ll> v;
      dfs(i, v);
      res = max(res, (ll)v.size());
      v.clear();
    }
  }

  cout << res;

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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

#define xx first
#define yy second
#define $1 first
#define $2 second

typedef vector<ll> Vll;
ll find(Vll &C, ll x) { return (C[x] == x) ? x : C[x] = find(C, C[x]); }
void merge(Vll &C, ll x, ll y) { C[find(C, x)] = find(C, y); }
// union
void solveCase() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, m;
  cin >> n >> m;
  vector<ll> C;
  C.resize(n + 1);
  for (int i = 1; i <= n; i++)
    C[i] = i;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    merge(C, u, v);
  }
  unordered_map<ll, ll> mp;
  for (int i = 1; i <= n; i++) {
    mp[find(C, i)]++;
  }
  vector<ll> dq;
  for (auto iter = mp.begin(); iter != mp.end(); iter++) {
    dq.push_back((*iter).second);
  }
  sort(dq.begin(), dq.end());
  ll ans = 0;
  ll now_fr = 0;

  for (int i = 0; i < dq.size(); i++) {
    ans += dq[i] - now_fr;
    now_fr = dq[i];
  }
  cout << ans;
}

int main() {

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
#endif
  solveCase();
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

#define xx first
#define yy second
#define $1 first
#define $2 second

typedef vector<ll> Vll;
ll find(Vll &C, ll x) { return (C[x] == x) ? x : C[x] = find(C, C[x]); }
void merge(Vll &C, ll x, ll y) { C[find(C, x)] = find(C, y); }
// union
void solveCase() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, m;
  cin >> n >> m;
  vector<ll> C;
  C.resize(n + 1);
  for (int i = 1; i <= n; i++)
    C[i] = i;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    merge(C, u, v);
  }
  unordered_map<ll, ll> mp;
  for (int i = 1; i <= n; i++) {
    mp[find(C, i)]++;
  }
  deque<ll> dq;
  for (auto iter = mp.begin(); iter != mp.end(); iter++) {
    dq.push_back((*iter).second);
  }
  sort(dq.begin(), dq.end());
  ll ans = 0;
  while (!dq.empty()) {
    ll fr = dq.front();
    for (int i = 0; i < dq.size(); i++) {
      dq[i] -= fr;
    }
    dq.pop_front();
    ans += fr;
  }
  cout << ans;
}

int main() {

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
#endif
  solveCase();
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl

vector<ll> friends[200010];
bool vis[200010];
ll dfs(ll st) {
  vis[st] = 1;
  ll num = 0;
  num++;
  FOR(i, 0, friends[st].size()) {
    if (!vis[friends[st][i]]) {
      num += dfs(friends[st][i]);
    }
  }
  return num;
}
int main() {
  ll n, q;
  cin >> n >> q;
  FOR(i, 0, q) {
    ll a, b;
    cin >> a >> b;
    friends[a].pb(b);
    friends[b].pb(a);
  }
  ll ma = -1;
  FORE(i, 1, n) {
    //		memset(vis,0,sizeof(vis));
    ma = max(ma, dfs(i));
  }
  cout << ma << Endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl

vector<ll> friends[200010];
bool vis[200010];
ll num;
ll dfs(ll st) {
  vis[st] = 1;
  num++;
  FOR(i, 0, friends[st].size()) {
    if (!vis[friends[st][i]]) {
      dfs(friends[st][i]);
    }
  }
  return num;
}
int main() {
  ll n, q;
  cin >> n >> q;
  FOR(i, 0, q) {
    ll a, b;
    cin >> a >> b;
    friends[a].pb(b);
    friends[b].pb(a);
  }
  ll ma = -1;
  FORE(i, 1, n) {
    num = 0;
    memset(vis, 0, sizeof(vis));
    ma = max(ma, dfs(i));
  }
  cout << ma << Endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
int n, m;
int par[N], rk[N];
unordered_map<int, int> h;
void init(int n) {
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    rk[i] = 0;
  }
}

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

bool same(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

int main() {
  cin >> n >> m;
  init(n);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (!same(a, b)) {
      unite(a, b);
    }
  }
  for (int i = 1; i <= n; i++)
    find(i);
  for (int i = 1; i <= n; i++) {
    h[par[i]]++;
    ans = max(ans, h[par[i]]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
int n, m;
int par[N], rk[N];
unordered_map<int, int> h;
void init(int n) {
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    rk[i] = 0;
  }
}

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

bool same(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

int main() {
  cin >> n >> m;
  init(n + 10);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (!same(a, b)) {
      unite(a, b);
    }
  }
  // unordered_map<int,int>::iterator iter;
  // for(iter = h.begin(); iter != h.end();iter++){
  //     ans = max(ans,iter->second);
  // }
  for (int i = 1; i <= n; i++) {
    find(i);
    h[par[i]]++;
    ans = max(ans, h[par[i]]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long ll;
int F[200003];
int Find(int a) {
  if (F[a] < 0)
    return a;
  return F[a] = Find(F[a]);
}
int main() {
  /*  string s,s1;
    cin>>s>>s1;
    int Max=0;
    int len=s.size();
    int len1=s1.size();
    for(int i=0;i<len;i++)
        for(int j=len-i-1;j>=1;j--)
    {  int z=s1.find(s.substr(i,j));
        if(len1>=j&&z!=string::npos)
        {
            if(i>=z&&len-(i+j-1)>=len1-(z+j-1))
            Max=max(Max,j);
        }
    }
    cout<<len1-Max;*/
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    F[i] = -1;
  while (m--) {
    int a, b;
    scanf("%d %d", &a, &b);
    int x, y;
    x = Find(a);
    y = Find(b);
    if (x == y)
      continue;
    F[x] += F[y];
    F[y] = x;
  }
  int Max = 0;
  for (int i = 1; i <= n; i++)
    Max = max(Max, -F[i]);
  ;
  cout << Max;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long ll;
int F[200003];
int Find(int a) {
  if (F[a] < 0)
    return a;
  return Find(F[a]);
}
int main() {
  /*  string s,s1;
    cin>>s>>s1;
    int Max=0;
    int len=s.size();
    int len1=s1.size();
    for(int i=0;i<len;i++)
        for(int j=len-i-1;j>=1;j--)
    {  int z=s1.find(s.substr(i,j));
        if(len1>=j&&z!=string::npos)
        {
            if(i>=z&&len-(i+j-1)>=len1-(z+j-1))
            Max=max(Max,j);
        }
    }
    cout<<len1-Max;*/
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    F[i] = -1;
  while (m--) {
    int a, b;
    cin >> a >> b;
    int x, y;
    x = Find(a);
    y = Find(b);
    if (x == y)
      continue;
    F[x] += F[y];
    F[y] = x;
  }
  int Max = 0;
  for (int i = 1; i <= n; i++)
    Max = max(Max, -F[i]);
  ;
  cout << Max;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
ll f[N + 1];
ll b[N + 1];
ll finds(ll x) {
  if (f[x] != x)
    f[x] = finds(f[x]);
  return f[x];
}
void uni(ll x, ll y) {

  ll x1 = finds(x);
  ll y1 = finds(y);
  ll a = min(x1, y1);
  ll b = max(x1, y1);
  f[b] = f[a];
}
int main() {
  for (int i = 1; i < N; i++) {
    f[i] = i;
  }
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    uni(x, y);
  }
  ll maxs = -99999;
  for (int i = 1; i <= n; i++) {
    b[finds(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    maxs = max(maxs, b[i]);
  }
  cout << maxs << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
ll f[N + 1];
ll b[N + 1];
ll finds(int x) {
  if (x != f[x]) {

    return finds(f[x]);
  }
  return x;
}
void uni(int x, int y) {

  int x1 = finds(x);
  int y1 = finds(y);
  int a = min(x1, y1);
  int b = max(x1, y1);
  f[b] = f[a];
}
int main() {
  for (int i = 1; i <= N; i++) {
    f[i] = i;
  }
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    uni(x, y);
  }
  ll maxs = -99999;
  for (int i = 1; i <= n; i++) {
    b[finds(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    maxs = max(maxs, b[i]);
  }
  cout << maxs << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
typedef std::pair<int, int> Edge;

int find(int *par, int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par, par[x]);
}

void unite(int *par, int x, int y) { par[find(par, x)] = find(par, y); }

bool isSame(int *par, int x, int y) { return find(par, x) == find(par, y); }

int main() {
  int N, M;
  std::cin >> N >> M;
  Edge *edge_list = new Edge[M];
  for (int i = 0; i < M; i++) {
    int a, b;
    std::cin >> a >> b;
    edge_list[i].first = a;
    edge_list[i].second = b;
  }
  int *par = new int[N + 1];
  for (int i = 1; i <= N; i++)
    par[i] = i;
  for (int i = 0; i < M; i++) {
    int u, v;
    u = edge_list[i].first;
    v = edge_list[i].second;
    unite(par, u, v);
  }
  int *color_cnt = new int[N + 1];
  for (int i = 1; i <= N; i++) {
    color_cnt[i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    color_cnt[find(par, i)]++;
  }
  int max_size = 0;
  for (int i = 1; i <= N; i++) {
    max_size = std::max(max_size, color_cnt[i]);
  }
  std::cout << max_size << std::endl;
  delete[] edge_list;
  delete[] par;
  delete[] color_cnt;
}


Error: TLE

Fixed submission:

#include <iostream>
typedef std::pair<int, int> Edge;

int find(int *par, int x) {
  if (x == par[x])
    return x;
  else
    return find(par, par[x]);
}

void unite(int *par, int x, int y) { par[find(par, x)] = find(par, y); }

bool isSame(int *par, int x, int y) { return find(par, x) == find(par, y); }

int main() {
  int N, M;
  std::cin >> N >> M;
  Edge *edge_list = new Edge[M];
  for (int i = 0; i < M; i++) {
    int a, b;
    std::cin >> a >> b;
    edge_list[i].first = a;
    edge_list[i].second = b;
  }
  int *par = new int[N + 1];
  for (int i = 1; i <= N; i++)
    par[i] = i;
  for (int i = 0; i < M; i++) {
    int u, v;
    u = edge_list[i].first;
    v = edge_list[i].second;
    unite(par, u, v);
  }
  int *color_cnt = new int[N + 1];
  for (int i = 1; i <= N; i++) {
    color_cnt[i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    color_cnt[find(par, i)]++;
  }
  int max_size = 0;
  for (int i = 1; i <= N; i++) {
    max_size = std::max(max_size, color_cnt[i]);
  }
  std::cout << max_size << std::endl;
  delete[] edge_list;
  delete[] par;
  delete[] color_cnt;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int n, m;

const int Max = 2e5 + 10;
int fa[Max], num[Max];
bool vis[Max];
vector<int> root, son[Max];

int findfather(int x) {
  int a = x;
  while (x != fa[x]) {
    x = fa[x];
  }
  while (a != x) {
    int z = fa[a];
    fa[a] = x;
    a = z;
  }
  return x;
}

void Un(int a, int b) {
  int fa1 = findfather(a);
  int fa2 = findfather(b);
  if (fa1 != fa2) {
    fa[fa1] = fa2;
  }
}
void findroot(int k) {
  if (fa[k] == k) {
    num[k]++;
    return;
  }
  vis[k] = true;
  findroot(fa[k]);
}

int dfs(int k) {
  if (fa[k] == k)
    return k;
  if (fa[k] != k)
    fa[k] = dfs(fa[k]);
  return fa[k];
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Un(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  for (int i = 1; i <= n; i++) {
    findroot(i);
  }
  for (int i = 1; i <= n; i++)
    ans = max(ans, num[i]);
  printf("%d", ans);
  // for (int i = 1; i <= n; i++) printf("\\n%d %d", i, fa[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int n, m;

const int Max = 2e5 + 10;
int fa[Max], num[Max];
bool vis[Max];
vector<int> root, son[Max];

int findfather(int x) {
  int a = x;
  while (x != fa[x]) {
    x = fa[x];
  }
  while (a != x) {
    int z = fa[a];
    fa[a] = x;
    a = z;
  }
  return x;
}

void Un(int a, int b) {
  int fa1 = findfather(a);
  int fa2 = findfather(b);
  if (fa1 != fa2) {
    fa[fa1] = fa2;
  }
}
int dfs(int k) {
  if (son[k].empty())
    return 1;
  int res = 1;
  for (auto i : son[k]) {
    if (i != k)
      res += dfs(i);
  }
  return res;
}

void findroot(int k) {
  if (fa[k] == k) {
    num[k]++;
    return;
  }
  findroot(fa[k]);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Un(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    son[fa[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    findroot(i);
  }
  for (int i = 1; i <= n; i++)
    ans = max(ans, num[i]);
  printf("%d", ans);
  return 0;
}