Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pie 3.141592653589793238462643383279
#define mod 998244353
#define inf 10000000000000007
#define all(vec) vec.begin(), vec.end()
#define ggr                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0;
#define ll long long
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 1)
    return 1;
  return kai(x - 1) * x;
}

int mod_pow(int x, int y, int moder) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % moder;
    x = x * x % moder;
    y >>= 1;
  }
  return res;
}

class Union_Find {
private:
  int par[100010], rankk[100010];

public:
  void init(int n) {
    rep(i, n) {
      par[i] = i;
      rankk[i] = 0;
    }
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rankk[x] < rankk[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rankk[x] == rankk[y])
        rankk[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int n, k, l, p[100010], q[100010], a[100010], b[100010];
map<pair<int, int>, int> mp;

signed main() {
  cin >> n >> k >> l;
  Union_Find ta, mu;
  ta.init(n + 1);
  mu.init(n + 1);
  rep(i, k) {
    cin >> p[i] >> q[i];
    ta.unite(p[i], q[i]);
  }
  rep(i, l) {
    cin >> a[i] >> b[i];
    mu.unite(a[i], b[i]);
  }
  for (int i = 1; i <= n; i++) {
    mp[make_pair(ta.find(i), mu.find(i))]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      cout << mp[make_pair(ta.find(i), mu.find(i))] << endl;
      return 0;
    } else
      cout << mp[make_pair(ta.find(i), mu.find(i))] << " ";
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pie 3.141592653589793238462643383279
#define mod 998244353
#define inf 10000000000000007
#define all(vec) vec.begin(), vec.end()
#define ggr                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0;
#define ll long long
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 1)
    return 1;
  return kai(x - 1) * x;
}

int mod_pow(int x, int y, int moder) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % moder;
    x = x * x % moder;
    y >>= 1;
  }
  return res;
}

class Union_Find {
private:
  int par[200010], rankk[200010];

public:
  void init(int n) {
    rep(i, n) {
      par[i] = i;
      rankk[i] = 0;
    }
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rankk[x] < rankk[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rankk[x] == rankk[y])
        rankk[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int n, k, l, p[100010], q[100010], a[100010], b[100010];
map<pair<int, int>, int> mp;

signed main() {
  cin >> n >> k >> l;
  Union_Find ta, mu;
  ta.init(n + 1);
  mu.init(n + 1);
  rep(i, k) {
    cin >> p[i] >> q[i];
    ta.unite(p[i], q[i]);
  }
  rep(i, l) {
    cin >> a[i] >> b[i];
    mu.unite(a[i], b[i]);
  }
  for (int i = 1; i <= n; i++) {
    mp[make_pair(ta.find(i), mu.find(i))]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      cout << mp[make_pair(ta.find(i), mu.find(i))] << endl;
      return 0;
    } else
      cout << mp[make_pair(ta.find(i), mu.find(i))] << " ";
  }
}