Problem p02573 (C++)

Buggy submission (status: Runtime Error):

// Problem : D - Friends
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
int DSU[100005];
int Rank[100005];
int find(int a) {
  if (DSU[a] == a)
    return a;
  return DSU[a] = find(DSU[a]);
}
void Union(int a, int b) {
  int x = find(a), y = find(b);
  if (Rank[x] == Rank[x]) {
    Rank[x]++;
    DSU[y] = x;
  } else if (Rank[x] > Rank[y]) {
    DSU[y] = x;
  } else {
    DSU[x] = y;
  }
  return;
}
void solve() {
  int n, m, i;
  cin >> n >> m;
  for (i = 0; i <= n; i++) {
    DSU[i] = i;
    Rank[i] = 1;
  }
  for (i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    if (DSU[u] == DSU[v])
      continue;
    Union(u, v);
  }
  map<int, int> M;
  m = 0;
  for (i = 1; i <= n; i++) {
    M[find(i)]++;
    m = max(m, M[DSU[i]]);
  }
  cout << m << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Fixed submission:

// Problem : D - Friends
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
int DSU[200005];
int Rank[200005];
int find(int a) {
  if (DSU[a] == a)
    return a;
  return DSU[a] = find(DSU[a]);
}
void Union(int a, int b) {
  int x = find(a), y = find(b);
  if (Rank[x] == Rank[x]) {
    Rank[x]++;
    DSU[y] = x;
  } else if (Rank[x] > Rank[y]) {
    DSU[y] = x;
  } else {
    DSU[x] = y;
  }
  return;
}
void solve() {
  int n, m, i;
  cin >> n >> m;
  for (i = 0; i <= n; i++) {
    DSU[i] = i;
    Rank[i] = 1;
  }
  for (i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    if (DSU[u] == DSU[v])
      continue;
    Union(u, v);
  }
  map<int, int> M;
  m = 0;
  for (i = 1; i <= n; i++) {
    M[find(i)]++;
    m = max(m, M[DSU[i]]);
  }
  cout << m << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}