Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
// functions
struct DSU {
  int N;
  vi dt;
  DSU(int n) : N(n), dt(n, -1) {}

  int root(int v) {
    if (dt[v] < 0)
      return v;
    return dt[v] = root(dt[v]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    dt[x] += dt[y];
    dt[y] = x;
    return true;
  }

  int size(int v) { return -dt[root(v)]; }
};
#endif

int main_D() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K, L;
  cin >> N >> K >> L;
  vi P(N), Q(N);
  REP(i, K) {
    cin >> P[i] >> Q[i];
    P[i]--;
    Q[i]--;
  }
  vi R(L), S(L);
  REP(i, L) {
    cin >> R[i] >> S[i];
    R[i]--;
    S[i]--;
  }

  DSU ud(N);
  REP(i, K) { ud.unite(P[i], Q[i]); }

  DSU ul(N);
  REP(i, L) { ul.unite(R[i], S[i]); }

  map<pii, int> mp;
  REP(i, N) { mp[pii(ud.root(i), ul.root(i))]++; }

  vi ans(N);
  REP(i, N) { ans[i] = mp[pii(ud.root(i), ul.root(i))]; }

  cout << ans[0];
  FOR(i, 1, N) cout << " " << ans[i];
  cout << endl;

  return 0;
}

Fixed submission:
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
// functions
struct DSU {
  int N;
  vi dt;
  DSU(int n) : N(n), dt(n, -1) {}

  int root(int v) {
    if (dt[v] < 0)
      return v;
    return dt[v] = root(dt[v]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    dt[x] += dt[y];
    dt[y] = x;
    return true;
  }

  int size(int v) { return -dt[root(v)]; }
};
#endif

int main_D() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K, L;
  cin >> N >> K >> L;
  vi P(K), Q(K);
  REP(i, K) {
    cin >> P[i] >> Q[i];
    P[i]--;
    Q[i]--;
  }
  vi R(L), S(L);
  REP(i, L) {
    cin >> R[i] >> S[i];
    R[i]--;
    S[i]--;
  }

  DSU ud(N);
  REP(i, K) { ud.unite(P[i], Q[i]); }

  DSU ul(N);
  REP(i, L) { ul.unite(R[i], S[i]); }

  map<pii, int> mp;
  REP(i, N) { mp[pii(ud.root(i), ul.root(i))]++; }

  vi ans(N);
  REP(i, N) { ans[i] = mp[pii(ud.root(i), ul.root(i))]; }

  cout << ans[0];
  FOR(i, 1, N) cout << " " << ans[i];
  cout << endl;

  return 0;
}