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

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

struct UnionFind {
  vector<int> d;
  // コンストラクタ 最初はすべて-1スタート
  UnionFind(int n = 0) : d(n, -1) {}

  int find(int x) {
    // 頂点が根なら頂点番号を返す
    // 違う場合、親についてfindクエリ実行(再帰)
    if (d.at(x) < 0)
      return x;
    return find(d.at(x));
  }

  bool unite(int x, int y) {
    // 両方の根を見る
    x = find(x);
    y = find(y);
    // 連結同士が一致している場合は、何もしない
    if (x == y)
      return false;
    // 小さいほうを大きいほうにつなげるため、yに小さいほうをあてる
    if (d.at(x) > d.at(y))
      swap(x, y);
    // yをxにつなげる
    d.at(x) += d.at(y);
    d.at(y) = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d.at(find(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 kekka = 0;
  for (int i = 0; i < n; i++)
    kekka = max(kekka, uf.size(i));

  cout << kekka << endl;
  //
}

Error: 0

Fixed submission:

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

int main() {
  //
  long long n, m;
  long long Max = 0;

  cin >> n >> m;
  vector<vector<long long>> d(n, vector<long long>(n, 0));
  vector<long long> vec(n, 0);
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    // 友達同士にコスト1を立てる

    if (d.at(a).at(b) == 0)
      vec.at(a)++;
    if (d.at(b).at(a) == 0)
      vec.at(b)++;
    d.at(a).at(b) = 1;
    d.at(b).at(a) = 1;
  }

  sort(vec.begin(), vec.end(), greater<long long>());
  cout << vec.at(0) + 1 << endl;
  //
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define lli long long int

using namespace std;

int dfs(int src, std::vector<bool> &visited,
        std::unordered_map<int, std::vector<int>> &adj_list) {
  visited[src] = true;
  int cnt = 1;
  for (auto neighbor : adj_list[src]) {
    if (!visited[neighbor]) {
      cnt += dfs(neighbor, visited, adj_list);
    }
  }
  return cnt;
}

int main() {
  int n, m;
  std::cin >> n >> m;
  std::unordered_map<int, std::vector<int>> adj_list{};
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }
  int max_size = 0;
  std::vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; ++i) {
    if (!visited[i]) {
      int size = dfs(i, visited, adj_list);
      max_size = max(size, max_size);
    }
  }
  std::cout << max_size << std::endl;
}

Error: 0

Fixed submission:

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

int dfs(unordered_map<int, vector<int>> adj_list, int src,
        vector<bool> &visited, int &count) {
  visited[src] = true;
  ++count;
  for (auto neighbor : adj_list[src]) {
    if (!visited[neighbor]) {
      dfs(adj_list, neighbor, visited, count);
    }
  }
}

int CountNodes(unordered_map<int, vector<int>> adj_list, int N) {
  vector<bool> visited(N, false);
  int max_nodes = 0;
  for (int i = 0; i < N; ++i) {
    if (!visited[i]) {
      int nodes = 0;
      dfs(adj_list, i, visited, nodes);
      max_nodes = max(max_nodes, nodes);
    }
  }
  return max_nodes;
}

int main() {
  int N, M;
  cin >> N >> M;
  unordered_map<int, vector<int>> adj_list;
  for (int i = 0; i < M; ++i) {
    int u, v;
    std::cin >> u >> v;
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
  }
  std::cout << CountNodes(adj_list, N) << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define ll long long int
#define hell 1000000007LL
using namespace std;
vector<int> adj[200001];
bool v[200001];
ll power(ll x, ll y, ll z) {
  if (y == 0)
    return 1;
  else {
    ll p = power(x, y / 2, z);
    if (y % 2 == 0)
      return (p * p) % z;
    else
      return (x * ((p * p) % z)) % z;
  }
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll po(ll x) {
  ll c = 0;
  while (x > 0) {
    x = x & (x - 1);
    c++;
  }
  return c;
}
int dfs(int x) {
  int c = 1;
  v[x] = 1;
  for (auto it = adj[x].begin(); it != adj[x].end(); it++) {
    if (!v[*it]) {
      c = c + dfs(*it);
    }
  }
  return c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll n, m;
  int k = 0;
  cin >> n >> m;
  ll a[m], b[m];
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    if (mp[{a[i], b[i]}] == 0 && mp[{b[i], a[i]}] == 0) {
      ll x = min(a[i], b[i]), y = max(a[i], b[i]);
      adj[x].push_back(y);
      adj[y].push_back(x);
      mp[{x, y}] = 1;
      mp[{y, x}] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] == false)
      k = max(k, dfs(i));
  }
  cout << k;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define ll long long int
#define hell 1000000007LL
using namespace std;
vector<int> adj[100001];
bool v[100001];
ll power(ll x, ll y, ll z) {
  if (y == 0)
    return 1;
  else {
    ll p = power(x, y / 2, z);
    if (y % 2 == 0)
      return (p * p) % z;
    else
      return (x * ((p * p) % z)) % z;
  }
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll po(ll x) {
  ll c = 0;
  while (x > 0) {
    x = x & (x - 1);
    c++;
  }
  return c;
}
int dfs(int x) {
  int c = 1;
  v[x] = 1;
  for (auto it = adj[x].begin(); it != adj[x].end(); it++) {
    if (!v[*it]) {
      c = c + dfs(*it);
    }
  }
  return c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  ll n, m;
  int k = 0;
  cin >> n >> m;
  ll a[m], b[m];
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    if (mp[{a[i], b[i]}] == 0 && mp[{b[i], a[i]}] == 0) {
      ll x = min(a[i], b[i]), y = max(a[i], b[i]);
      adj[x].push_back(y);
      adj[y].push_back(x);
      mp[{x, y}] = 1;
      mp[{y, x}] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] == false)
      k = max(k, dfs(i));
  }
  cout << k;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
const int N = 2e5 + 100;
int n, a, b, max0, max1, i, num[N], fa[N], m;
int find(int a) {
  if (a == fa[a]) {
    return a;
  } else {
    return fa[a] = find(fa[a]);
  }
}
void f1(int x, int y) {
  int nx, ny;
  nx = find(x);
  ny = find(y);
  if (nx != ny) {
    fa[nx] = ny;
    num[ny] += num[nx]; // 合并两个集合的数量
  }
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= N; i++) {
    fa[i] = i;
    num[i] = 1; // 刚开始集合只有本身
  }
  max0 = 0;
  if (m == 0) {
    cout << 1 << endl;
    return 0;
  }

  for (i = 0; i < m; i++) {
    cin >> a >> b;
    max0 = max(max0, max(a, b)); // 找出关系中编号最大的人
    f1(a, b);
  }
  max1 = 0;
  for (i = 1; i <= max0; i++) {
    if (max1 < num[i]) // 比较每个集合的大小
    {
      max1 = num[i];
    }
  }
  cout << max1 << endl;
}


Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
#define N 100

int Tree[N];
int findRoot(int x) { // 查找x的根节点
  if (Tree[x] == -1)
    return x;
  else {
    int temp = findRoot(Tree[x]);
    Tree[x] = temp;
    return temp;
  }
}

int main() {
  int sum[N]; // 结点i为根的树的节点数
  int n;
  scanf("%d", &n);
  // 初始化
  int m;
  scanf("%d", &m);
  for (int i = 1; i < N; i++) {
    Tree[i] = -1;
    sum[i] = 1;
  }

  while (m--) {
    int a, b;
    scanf("%d %d", &a, &b);
    a = findRoot(a); // 找到a所在树的根节点
    b = findRoot(b); // 找到b所在树的根节点
    if (a != b) {    // ab不在一棵树上
      Tree[a] = b;   // a树连接到b树上
      sum[b] += sum[a]; // 以b为根节点的树上节点数添加a上节点的数目
    }
  }
  int ans = 1;
  for (int i = 1; i < N; i++) {
    if (Tree[i] == -1 && sum[i] > ans) // 找到最大值
      ans = sum[i];
  }
  printf("%d", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long par[200007], mx = 1, sz[200007];

void mk(long long n) {
  long long i;
  for (i = 0; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

long long fin(long long a) {
  if (par[a] == a) {
    return a;
  }
  return par[a] = fin(par[a]);
}

void uni(long long a, long long b) {
  a = fin(a);
  b = fin(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    mx = max(mx, sz[a]);
  }
  mx = max(mx, sz[a]);
  mx = max(mx, sz[b]);
}

int main() {
  long long n, m, i, x, y;
  cin >> n >> m;
  mk(n + 1);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    uni(x, y);
  }
  cout << mx;
  return 0;
}


Error: WA

Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long par[100007], mx = 1, sz[100007];

void mk(long long n) {
  long long i;
  for (i = 0; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

long long fin(long long a) {
  if (par[a] == a) {
    return a;
  }
  return par[a] = fin(par[a]);
}

void uni(long long a, long long b) {
  a = fin(a);
  b = fin(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    mx = max(mx, sz[a]);
  }
  mx = max(mx, sz[a]);
  mx = max(mx, sz[b]);
}

int main() {
  long long n, m, i, x, y;
  cin >> n >> m;
  mk(n + 1);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    uni(x, y);
  }
  cout << mx;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int mx = 0;
int n, m;
int a[200010], father[200010], num[200010];
int findfather(int x) {
  int z = x;
  while (x != father[x]) {
    x = father[x];
  }
  while (z != father[z]) {
    int a = z;
    z = father[z];
    father[a] = x;
  }
  return x;
}
void Union(int a, int b) {
  int faA = findfather(a);
  int faB = findfather(b);
  if (faA != faB)
    father[faA] = faB;
}
int main() {
  int u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    Union(u, v);
  }
  for (int i = 1; i <= n; i++)
    num[findfather(i)]++;
  for (int i = 1; i <= n; i++)
    mx = max(num[i], mx);
  cout << mx << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int mx = 0;
int n, m;
int a[200010], father[200010], num[200010];
int findfather(int x) {
  int z = x;
  while (father[x] != x) {
    x = father[x];
  }
  while (father[z] != z) {
    int a = z;
    father[a] = x;
    z = father[z];
  }
  return x;
}
void Union(int a, int b) {
  int faA = findfather(a);
  int faB = findfather(b);
  if (faA != faB)
    father[faA] = faB;
}
int main() {
  int u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    Union(u, v);
  }
  for (int i = 1; i <= n; i++)
    num[findfather(i)]++;
  for (int i = 1; i <= n; i++)
    mx = max(num[i], mx);
  cout << mx << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct UnionFind {
  vector<int> r;

  UnionFind(int x) { r = vector<int>(x, -1); }

  int root(int x) {
    if (r.at(x) < 0)
      return x;
    else
      return root(r.at(x));
  }

  int size(int x) { return -1 * r.at(root(x)); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r.at(x) > r.at(y))
      swap(x, y);
    r.at(x) += r.at(y);
    r.at(y) = x;
    return true;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  UnionFind UF(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    UF.unite(a - 1, b - 1);
  }
  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;

struct UnionFind {
  vector<int> r;

  UnionFind(int x) { r = vector<int>(x, -1); }

  int root(int x) {
    if (r.at(x) < 0)
      return x;
    else
      return root(r.at(x));
  }

  int size(int x) { return -1 * r.at(root(x)); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r.at(x) < r.at(y))
      swap(x, y);
    r.at(x) += r.at(y);
    r.at(y) = x;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  UnionFind UF(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    UF.unite(a - 1, b - 1);
  }
  for (int i = 0; i < n; i++) {
    ans = max(ans, UF.size(i));
  }
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll NMAX = 200005;
vector<ll> par(NMAX);
vector<ll> siz(NMAX);
ll n;
void init() {
  for (ll i = 0; i < n; i++) {
    par.at(i) = i;
    siz.at(i) = 1;
  }
}

ll root(ll x) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = root(par.at(x));
  }
}

bool same(ll x, ll y) { return root(x) == root(y); }

bool unite(ll x, ll y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return false;
  }
  if (siz[x] < siz[y])
    swap(x, y);
  siz[x] += siz[y];
  par.at(y) = x;
  return true;
}

ll size(ll x) { return siz.at(root(x)); }

int main() {
  cin >> n;
  ll m;
  cin >> m;
  init();
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    unite(a, b);
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans = max(ans, size(i));
  }

  cout << ans << endl;

  return (0);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NMAX = 100005;
vector<int> par(NMAX);
vector<int> siz(NMAX);
int n;
void init() {
  for (int i = 0; i < n; i++) {
    par.at(i) = i;
    siz.at(i) = 1;
  }
}

int root(int x) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = root(par.at(x));
  }
}

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

bool unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return false;
  }
  if (siz[x] < siz[y])
    swap(x, y);
  siz[x] += siz[y];
  par.at(y) = x;
  return true;
}

int size(int x) { return siz.at(root(x)); }

int main() {
  cin >> n;
  int m;
  cin >> m;
  init();
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    unite(a, b);
  }

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

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define pi acos(-1.0)
#define pb push_back
#define vi vector<int>
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define CP(n) cout << "Case " << n << ": "
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
#define MAX 1000007
#define all(x) (x.begin(), x.end())
#define debug(x, c) cout << x << ": " << c << " ";
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
using namespace std;
int par[200005];
mapii mp;
int find_parent(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find_parent(par[a]);
}
void dsu(int a, int b) {
  int x = find_parent(a);
  int y = find_parent(b);
  if (y > x)
    par[y] = x;
  else
    par[x] = y;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    par[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dsu(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    mp[find_parent(i)]++;

  for (auto x : mp)
    ans = max(ans, x.second);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pi acos(-1.0)
#define pb push_back
#define vi vector<int>
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define CP(n) cout << "Case " << n << ": "
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
#define MAX 1000007
#define all(x) (x.begin(), x.end())
#define debug(x, c) cout << x << ": " << c << " ";
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
using namespace std;
int par[200005];
mapii mp;
int find_parent(int a) {
  if (par[a] == a)
    return a;
  return par[a] = find_parent(par[a]);
}
void dsu(int a, int b) {
  int x = find_parent(a);
  int y = find_parent(b);
  par[x] = y;
  par[y] = x;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    par[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dsu(a, b);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    mp[find_parent(i)]++;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

int main() {
  int n, m;
  cin >> n >> m;
  vvi a(n + 1);
  vi b(n + 1, -1);
  vi c(n + 1);
  int next = 0;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    if (b[x] == -1) {
      if (b[y] == -1) {
        a[next].push_back(x);
        a[next].push_back(y);
        b[x] = next;
        b[y] = next;
        c[next] += 2;
        next++;
        continue;
      }
      a[b[y]].push_back(x);
      b[x] = b[y];
      c[b[y]]++;
      continue;
    }
    if (b[y] == -1) {
      a[b[x]].push_back(y);
      b[y] = b[x];
      c[b[x]]++;
      continue;
    }
    if (b[x] != b[y]) {
      int p = b[x], q = b[y];
      if (c[b[x]] > c[b[y]]) {
        p = b[y];
        q = b[x];
      }
      for (int j : a[p]) {
        a[q].push_back(j);
        b[j] = q;
        c[q] += c[p];
      }
    }
  }
  int ans = 1;
  rep(i, n + 1) {
    int x = a[i].size();
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

int main() {
  int n, m;
  cin >> n >> m;
  vvi a(n + 1);
  vi b(n + 1, -1);
  int next = 0;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    if (b[x] == -1) {
      if (b[y] == -1) {
        a[next].push_back(x);
        a[next].push_back(y);
        b[x] = next;
        b[y] = next;
        next++;
        continue;
      }
      a[b[y]].push_back(x);
      b[x] = b[y];
      continue;
    }
    if (b[y] == -1) {
      a[b[x]].push_back(y);
      b[y] = b[x];
      continue;
    }
    if (b[x] != b[y]) {
      for (int j : a[b[y]]) {
        a[b[x]].push_back(j);
        b[j] = b[x];
      }
    }
  }
  int ans = 1;
  rep(i, n + 1) {
    int x = a[i].size();
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}
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> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector<vector<ll>> Mat;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int N, M;
int Group[int(2e5 + 5)];
int cnt[int(2e5 + 5)];
bool visited[int(2e5 + 5)];

int DFS(int now) {
  if (visited[now])
    return Group[now];

  visited[now] = true;
  int G = DFS(Group[now]);
  return Group[now] = G;
}

int main() {
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    Group[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    int GA = Group[A], GB = Group[B];
    while (GA != Group[GA])
      GA = Group[GA];
    while (GB != Group[GB])
      GB = Group[GB];
    Group[max(GA, GB)] = min(GA, GB);
    Group[A] = min(GA, GB);
    Group[B] = min(GA, GB);
  }

  for (int i = 1; i <= N; i++) {
    DFS(i);
    cnt[Group[i]]++;
  }

  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector<vector<ll>> Mat;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int N, M;
int Group[int(2e5 + 5)];
int cnt[int(2e5 + 5)];

int main() {
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    Group[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    int GA = Group[A], GB = Group[B];
    while (GA != Group[GA])
      GA = Group[GA];
    while (GB != Group[GB])
      GB = Group[GB];
    Group[max(GA, GB)] = min(GA, GB);
    Group[A] = min(GA, GB);
    Group[B] = min(GA, GB);
  }

  for (int i = 1; i <= N; i++) {
    int G = Group[i];
    while (G != Group[G])
      G = Group[G];
    cnt[G]++;
  }

  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int mxn = 2e5 + 5;
const int inf = 1e9;

int sz[mxn];

int par[mxn];

int findrep(int u) { return par[u] == u ? u : (par[u] = findrep(par[u])); }

void unite(int u, int v) {
  u = findrep(u);
  v = findrep(v);

  if (u == v)
    return;
  if (sz[u] > sz[v])
    swap(u, v);

  sz[v] += sz[u];
  par[u] = v;
}

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

  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    unite(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (findrep(i) == i) {
      ans = max(ans, sz[i]);
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int mxn = 1e5 + 5;
const int inf = 1e9;

int sz[mxn];

int par[mxn];

int findrep(int u) { return par[u] == u ? u : (par[u] = findrep(par[u])); }

void unite(int u, int v) {
  u = findrep(u);
  v = findrep(v);

  if (u == v)
    return;
  if (sz[u] > sz[v])
    swap(u, v);

  sz[v] += sz[u];
  par[u] = v;
}

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

  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    unite(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (findrep(i) == i) {
      ans = max(ans, sz[i]);
    }
  }

  cout << ans << endl;
}