Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(n) for (int i = 0; i < n; i++)
typedef pair<int, int> pii;

typedef long long ll;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
const int INF = 1 << 29;

int par[200001], n, k, l, pa[200001];
int ans[200001];
void init(int n) {

  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    par[i] = i;
    ans[i] = 1;
  }
}

int root(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = root(par[x]);
}
int roo(int x) {
  if (pa[x] == x)
    return x;
  else
    return pa[x] = roo(pa[x]);
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}
void unit(int x, int y) {
  x = roo(x);
  y = roo(y);
  if (x == y)
    return;
  pa[x] = y;
}

int main() {

  cin >> n >> k >> l;
  init(n);

  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unit(r, s);
  }
  map<int, int> str[100001];
  for (int i = 1; i <= n; i++) {
    str[root(i)][roo(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << str[root(i)][roo(i)] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(n) for (int i = 0; i < n; i++)
typedef pair<int, int> pii;

typedef long long ll;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
const int INF = 1 << 29;

int par[200001], n, k, l, pa[200001];
int ans[200001];
void init(int n) {

  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    par[i] = i;
    ans[i] = 1;
  }
}

int root(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = root(par[x]);
}
int roo(int x) {
  if (pa[x] == x)
    return x;
  else
    return pa[x] = roo(pa[x]);
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}
void unit(int x, int y) {
  x = roo(x);
  y = roo(y);
  if (x == y)
    return;
  pa[x] = y;
}

int main() {

  cin >> n >> k >> l;
  init(n);

  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unit(r, s);
  }
  map<int, int> str[200001];
  for (int i = 1; i <= n; i++) {
    str[root(i)][roo(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << str[root(i)][roo(i)] << endl;
  }

  return 0;
}