Problem p03857 (C++)

Buggy submission (status: Runtime Error):
// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

const int MAXN = 100010;
struct UnionFind {
  int node[MAXN];
  UnionFind() { memset(node, -1, sizeof(node)); }
  int find(int x) { return node[x] < 0 ? x : node[x] = find(node[x]); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    node[x] += node[y];
    node[y] = x;
  }
};

UnionFind road, rail;
signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int u, v;
    cin >> u >> v;
    road.unite(u, v);
  }

  for (int i = 0; i < L; i++) {
    int u, v;
    cin >> u >> v;
    rail.unite(u, v);
  }

  map<pair<int, int>, int> hash;
  for (int i = 1; i <= N; i++) {
    int A = road.find(i), B = rail.find(i);
    hash[make_pair(A, B)]++;
  }

  for (int i = 1; i <= N; i++) {
    int A = road.find(i), B = rail.find(i);
    int ans = hash[make_pair(A, B)];

    printf("%lld%c", ans, " \n"[i == N]);
  }
  return 0;
}

Fixed submission:
// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

const int MAXN = 200010;
struct UnionFind {
  int node[MAXN];
  UnionFind() { memset(node, -1, sizeof(node)); }
  int find(int x) { return node[x] < 0 ? x : node[x] = find(node[x]); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    node[x] += node[y];
    node[y] = x;
  }
};

UnionFind road, rail;
signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int u, v;
    cin >> u >> v;
    road.unite(u, v);
  }

  for (int i = 0; i < L; i++) {
    int u, v;
    cin >> u >> v;
    rail.unite(u, v);
  }

  map<pair<int, int>, int> hash;
  for (int i = 1; i <= N; i++) {
    int A = road.find(i), B = rail.find(i);
    hash[make_pair(A, B)]++;
  }

  for (int i = 1; i <= N; i++) {
    int A = road.find(i), B = rail.find(i);
    int ans = hash[make_pair(A, B)];

    printf("%lld%c", ans, " \n"[i == N]);
  }
  return 0;
}