Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

#define SIZE (10000)
int uni_road[SIZE];
int uni_rail[SIZE];

int root(int a, bool type) {
  if (type) {
    if (uni_road[a] < 0)
      return a;
    return uni_road[a] = root(uni_road[a], type);
  } else {
    if (uni_rail[a] < 0)
      return a;
    return uni_rail[a] = root(uni_rail[a], type);
  }
}

bool connect(int a, int b, bool type) {
  if (type) {
    a = root(a, type);
    b = root(b, type);
    if (a == b)
      return false;
    if (uni_road[a] > uni_road[b]) {
      a ^= b;
      b ^= a;
      a ^= b;
    }
    uni_road[a] = uni_road[a] + uni_road[b];
    uni_road[b] = a;
  } else {
    a = root(a, type);
    b = root(b, type);
    if (a == b)
      return false;
    if (uni_rail[a] > uni_rail[b]) {
      a ^= b;
      b ^= a;
      a ^= b;
    }
    uni_rail[a] = uni_rail[a] + uni_rail[b];
    uni_rail[b] = a;
  }
  return true;
}

bool isConnected(int a, int b, bool type) {
  return root(a, type) == root(b, type);
}

int size(int a, bool type) {
  if (type) {
    return -uni_road[root(a, type)];
  } else {
    return -uni_rail[root(a, type)];
  }
}

int main() {
  memset(uni_road, -1, sizeof(uni_road));
  memset(uni_rail, -1, sizeof(uni_rail));
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    connect(p, q, true);
  }
  for (int i = 0; i < L; i++) {
    int r, s;
    cin >> r >> s;
    connect(r, s, false);
  }

  map<pair<int, int>, int> count;
  for (int i = 1; i <= N; i++) {
    count[make_pair(root(i, true), root(i, false))]++;
  }
  for (int i = 1; i <= N; i++) {
    cout << count[make_pair(root(i, true), root(i, false))];
    if (i != N)
      cout << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <map>
using namespace std;

#define SIZE (200000)
int uni_road[SIZE];
int uni_rail[SIZE];

int root(int a, bool type) {
  if (type) {
    if (uni_road[a] < 0)
      return a;
    return uni_road[a] = root(uni_road[a], type);
  } else {
    if (uni_rail[a] < 0)
      return a;
    return uni_rail[a] = root(uni_rail[a], type);
  }
}

bool connect(int a, int b, bool type) {
  if (type) {
    a = root(a, type);
    b = root(b, type);
    if (a == b)
      return false;
    if (uni_road[a] > uni_road[b]) {
      a ^= b;
      b ^= a;
      a ^= b;
    }
    uni_road[a] = uni_road[a] + uni_road[b];
    uni_road[b] = a;
  } else {
    a = root(a, type);
    b = root(b, type);
    if (a == b)
      return false;
    if (uni_rail[a] > uni_rail[b]) {
      a ^= b;
      b ^= a;
      a ^= b;
    }
    uni_rail[a] = uni_rail[a] + uni_rail[b];
    uni_rail[b] = a;
  }
  return true;
}

bool isConnected(int a, int b, bool type) {
  return root(a, type) == root(b, type);
}

int size(int a, bool type) {
  if (type) {
    return -uni_road[root(a, type)];
  } else {
    return -uni_rail[root(a, type)];
  }
}

int main() {
  memset(uni_road, -1, sizeof(uni_road));
  memset(uni_rail, -1, sizeof(uni_rail));
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    connect(p, q, true);
  }
  for (int i = 0; i < L; i++) {
    int r, s;
    cin >> r >> s;
    connect(r, s, false);
  }

  map<pair<int, int>, int> count;
  for (int i = 1; i <= N; i++) {
    count[make_pair(root(i, true), root(i, false))]++;
  }
  for (int i = 1; i <= N; i++) {
    cout << count[make_pair(root(i, true), root(i, false))];
    if (i != N)
      cout << " ";
  }
  cout << endl;

  return 0;
}