Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#include <bitset>
#include <cassert>
#include <iomanip>
#include <queue>
#include <random>
#include <stack>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repf(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repv(x, arr) for (auto &x : arr)
#define all(v) (v).begin(), (v).end()
#define vec(name, num) vector<ll> name((num), 0);
#define mp(a, b) make_pair(a, b)
#define op(i) cout << (i) << endl;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const ll mod = 1e9 + 7;
ll find(vector<ll> &root, ll x) {
  if (root[x] >= 0) {
    root[x] = find(root, root[x]);
    return root[x];
  }
  return x;
}
void unit(vector<ll> &root, ll a, ll b) {
  ll ra, rb;
  ra = find(root, a);
  rb = find(root, b);
  if (ra == rb) {
    return;
  } else if (root[ra] <= root[rb]) {
    root[ra] += root[ra];
    root[rb] = ra;
  } else {
    root[rb] += root[ra];
    root[ra] = rb;
  }
  return;
}
void UnionFind(vector<ll> &root, ll m) {
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    unit(root, a, b);
  }
  return;
}

int main() {
  ll N, K, L;
  cin >> N >> K >> L;
  vector<ll> road(N);
  vector<ll> metro(N);
  rep(i, N) {
    road[i] = -1;
    metro[i] = -1;
  }
  UnionFind(road, K);
  UnionFind(metro, L);
  vector<pll> idx(N);
  map<pll, int> cnt;
  rep(i, N) {
    idx[i] = mp(find(road, i), find(metro, i));
    cnt[idx[i]]++;
  }
  cout << cnt[idx[0]];
  repf(i, 1, N) { cout << " " << cnt[idx[i]]; }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#include <bitset>
#include <cassert>
#include <iomanip>
#include <queue>
#include <random>
#include <stack>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repf(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repv(x, arr) for (auto &x : arr)
#define all(v) (v).begin(), (v).end()
#define vec(name, num) vector<ll> name((num), 0);
#define mp(a, b) make_pair(a, b)
#define op(i) cout << (i) << endl;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const ll mod = 1e9 + 7;
ll find(vector<ll> &root, ll x) {
  if (root[x] >= 0) {
    root[x] = find(root, root[x]);
    return root[x];
  }
  return x;
}
void unit(vector<ll> &root, ll a, ll b) {
  ll ra, rb;
  ra = find(root, a);
  rb = find(root, b);
  if (ra == rb) {
    return;
  } else if (root[ra] <= root[rb]) {
    root[ra] += root[rb];
    root[rb] = ra;
  } else {
    root[rb] += root[ra];
    root[ra] = rb;
  }
  return;
}
void UnionFind(vector<ll> &root, ll m) {
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    unit(root, a, b);
  }
  return;
}

int main() {
  ll N, K, L;
  cin >> N >> K >> L;
  vector<ll> road(N);
  vector<ll> metro(N);
  rep(i, N) {
    road[i] = -1;
    metro[i] = -1;
  }
  UnionFind(road, K);
  UnionFind(metro, L);
  vector<pll> idx(N);
  map<pll, int> cnt;
  rep(i, N) {
    idx[i] = mp(find(road, i), find(metro, i));
    cnt[idx[i]]++;
  }
  cout << cnt[idx[0]];
  repf(i, 1, N) { cout << " " << cnt[idx[i]]; }
  cout << endl;
}