Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  vii A(N), B(N);
  vi F(N, -1), G(N, -1);
  rep(i, 0, K) {
    int X, Y;
    cin >> X >> Y;
    A[X - 1].pb(Y - 1);
    A[Y - 1].pb(X - 1);
  }
  rep(i, 0, N) {
    if (F[i] == -1) {
      queue<int> Q;
      Q.push(i);
      F[i] = i;
      while (Q.size() > 0) {
        int X = Q.front();
        Q.pop();
        rep(j, 0, A[X].size()) {
          int Y = A[X][j];
          if (F[Y] == -1) {
            F[Y] = i;
            Q.push(Y);
          }
        }
      }
    }
  }
  rep(i, 0, L) {
    int X, Y;
    cin >> X >> Y;
    B[X - 1].pb(Y - 1);
    B[Y - 1].pb(X - 1);
  }
  rep(i, 0, N) {
    if (G[i] == -1) {
      queue<int> Q;
      Q.push(i);
      G[i] = i;
      while (Q.size() > 0) {
        int X = Q.front();
        Q.pop();
        rep(j, 0, B[X].size()) {
          int Y = B[X][j];
          if (G[Y] == -1) {
            G[Y] = i;
            Q.push(Y);
          }
        }
      }
    }
  }
  vii E(N, vi(N));
  rep(i, 0, N) { E[F[i]][G[i]]++; }
  rep(i, 0, N) { cout << E[F[i]][G[i]] << " "; }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  vii A(N), B(N);
  vi F(N, -1), G(N, -1);
  rep(i, 0, K) {
    int X, Y;
    cin >> X >> Y;
    A[X - 1].pb(Y - 1);
    A[Y - 1].pb(X - 1);
  }
  rep(i, 0, N) {
    if (F[i] == -1) {
      queue<int> Q;
      Q.push(i);
      F[i] = i;
      while (Q.size() > 0) {
        int X = Q.front();
        Q.pop();
        rep(j, 0, A[X].size()) {
          int Y = A[X][j];
          if (F[Y] == -1) {
            F[Y] = i;
            Q.push(Y);
          }
        }
      }
    }
  }
  rep(i, 0, L) {
    int X, Y;
    cin >> X >> Y;
    B[X - 1].pb(Y - 1);
    B[Y - 1].pb(X - 1);
  }
  rep(i, 0, N) {
    if (G[i] == -1) {
      queue<int> Q;
      Q.push(i);
      G[i] = i;
      while (Q.size() > 0) {
        int X = Q.front();
        Q.pop();
        rep(j, 0, B[X].size()) {
          int Y = B[X][j];
          if (G[Y] == -1) {
            G[Y] = i;
            Q.push(Y);
          }
        }
      }
    }
  }
  map<int, map<int, int>> E;
  rep(i, 0, N) { E[F[i]][G[i]]++; }
  rep(i, 0, N) { cout << E[F[i]][G[i]] << " "; }
  cout << endl;
}