Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout << fixed;                                                               \
  cout << setprecision(12);
#define randomINT                                                              \
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\n"
#define DISP(as)                                                               \
  for (auto it : as)                                                           \
    cout << it << " ";                                                         \
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\n";
#define deb1(x) cerr << #x << " = " << x << '\n';
#define deb2(x, y)                                                             \
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \
       << '\n';
#define deb3(x, y, z)                                                          \
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \
       << y << ',' << z << ']' << '\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;

const ll N = 3 * MAX5;
// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, m, y, k, z, l;
string s, t, d, c;

ll par[N], sz[N], ans[N], vis[N];
vll v, adj[N];
// <------------- Implement Functions Here ----------> //

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

ll find(ll x) { return x == par[x] ? x : par[x] = find(par[x]); }

void merge(ll x, ll y) {
  ll u = find(x);
  ll v = find(y);

  if (u == v)
    return;

  if (sz[u] < sz[v])
    swap(u, v);
  sz[u] += sz[v];
  par[v] = u;
}

void dfs(ll x) {
  vis[x] = 1;
  v.pb(x);
  for (auto it : adj[x]) {
    if (!vis[it])
      dfs(it);
  }
}

// <------------- Start of main() -------------------> //

void MAIN() {
  cin >> n;
  make(n);
  cin >> k >> l;

  for (ll i = 0; i < k; i++) {
    cin >> x >> y;
    merge(x, y);
  }

  for (ll i = 0; i < l; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  for (ll i = 1; i <= n; i++) {
    if (!vis[i]) {
      v.clear();
      dfs(i);
      map<ll, ll> cnt;
      for (auto it : v)
        cnt[find(it)]++;
      for (auto it : v)
        ans[it] = cnt[find(it)];
    }
    cout << ans[i] << " ";
  }

  newl;
}

int main() {
  fastio;
  randomINT;
  // cin >> T;
  while (T--) {
    MAIN();
  }
  return 0;
}


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

#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout << fixed;                                                               \
  cout << setprecision(12);
#define randomINT                                                              \
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\n"
#define DISP(as)                                                               \
  for (auto it : as)                                                           \
    cout << it << " ";                                                         \
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\n";
#define deb1(x) cerr << #x << " = " << x << '\n';
#define deb2(x, y)                                                             \
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \
       << '\n';
#define deb3(x, y, z)                                                          \
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \
       << y << ',' << z << ']' << '\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;

const ll N = 3 * MAX5;
// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, m, y, k, z, l;
string s, t, d, c;

ll par[N], sz[N], ans[N], vis[N];
vll v, adj[N];
// <------------- Implement Functions Here ----------> //

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

ll find(ll x) { return x == par[x] ? x : par[x] = find(par[x]); }

void merge(ll x, ll y) {
  ll u = find(x);
  ll v = find(y);

  if (u == v)
    return;

  if (sz[u] < sz[v])
    swap(u, v);
  sz[u] += sz[v];
  par[v] = u;
}

void dfs(ll x) {
  vis[x] = 1;
  v.pb(x);
  for (auto it : adj[x]) {
    if (!vis[it])
      dfs(it);
  }
}

// <------------- Start of main() -------------------> //

void MAIN() {
  cin >> n;
  make(n);
  cin >> k >> l;

  for (ll i = 0; i < k; i++) {
    cin >> x >> y;
    merge(x, y);
  }

  for (ll i = 0; i < l; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  for (ll i = 1; i <= n; i++) {
    if (!vis[i]) {
      v.clear();
      dfs(i);
      map<ll, ll> cnt;
      for (auto it : v)
        cnt[find(it)]++;
      for (auto it : v)
        ans[it] = cnt[find(it)];
    }
    cout << ans[i] << " ";
  }

  newl;
}

int main() {
  fastio;
  randomINT;
  // cin >> T;
  while (T--) {
    MAIN();
  }
  return 0;
}