Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
// #define int ll
// #define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 1e5 + 5;
vector<int> g[N], g2[N];
int u2[N], c = 0, u[N];
void dfs2(int v) {
  u2[v] = c;
  for (int to : g2[v])
    if (!u2[to])
      dfs2(to);
}
vector<int> comp;
void dfs(int v) {
  u[v] = 1;
  comp.pb(v);
  for (int to : g[v])
    if (!u[to])
      dfs(to);
}
int ans[N], cnt[N];
int32_t main() {
  int n, m1, m2;
  cin >> n >> m1 >> m2;
  forn(i, m1) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  forn(i, m2) {
    int a, b;
    cin >> a >> b;
    g2[a].pb(b);
    g2[b].pb(a);
  }
  for (int i = 1; i <= n; ++i) {
    if (!u2[i]) {
      c++;
      dfs2(i);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!u[i]) {
      comp.clear();
      dfs(i);
      for (int v : comp)
        cnt[u2[v]]++;
      for (int v : comp)
        ans[v] = cnt[u2[v]];
      for (int v : comp)
        cnt[u2[v]]--;
    }
  }
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
// #define int ll
// #define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 2e5 + 5;
vector<int> g[N], g2[N];
int u2[N], c = 0, u[N];
void dfs2(int v) {
  u2[v] = c;
  for (int to : g2[v])
    if (!u2[to])
      dfs2(to);
}
vector<int> comp;
void dfs(int v) {
  u[v] = 1;
  comp.pb(v);
  for (int to : g[v])
    if (!u[to])
      dfs(to);
}
int ans[N], cnt[N];
int32_t main() {
  int n, m1, m2;
  cin >> n >> m1 >> m2;
  forn(i, m1) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  forn(i, m2) {
    int a, b;
    cin >> a >> b;
    g2[a].pb(b);
    g2[b].pb(a);
  }
  for (int i = 1; i <= n; ++i) {
    if (!u2[i]) {
      c++;
      dfs2(i);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!u[i]) {
      comp.clear();
      dfs(i);
      for (int v : comp)
        cnt[u2[v]]++;
      for (int v : comp)
        ans[v] = cnt[u2[v]];
      for (int v : comp)
        cnt[u2[v]]--;
    }
  }
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}