Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define REP(i, s, n) for (int i = s; i < (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
ll inf = ll(1e18);
using ll1d = vector<ll>;
using ll2d = vector<ll1d>;
using ll3d = vector<ll2d>;
// Union Find begin

class UnionFind {
private:
  vector<ll> _par;
  vector<ll> _rank;

public:
  explicit UnionFind(ll n);
  ll find(ll x);
  void unite(ll x, ll y);
  bool isSame(ll x, ll y);
};

UnionFind::UnionFind(ll n) {
  for (ll i = 0; i < n; ++i) {
    _par.push_back(i);
    _rank.push_back(0);
  }
}

ll UnionFind::find(ll x) {
  if (_par.at(static_cast<unsigned long>(x)) == x)
    return x;
  else
    return _par.at(static_cast<unsigned long>(x)) =
               find(_par.at(static_cast<unsigned long>(x)));
}

void UnionFind::unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (_rank.at(static_cast<unsigned long>(x)) <
      _rank.at(static_cast<unsigned long>(y)))
    _par.at(static_cast<unsigned long>(x)) = y;
  else {
    _par.at(static_cast<unsigned long>(y)) = x;
    if (_rank.at(static_cast<unsigned long>(x)) ==
        _rank.at(static_cast<unsigned long>(y)))
      ++_rank.at(static_cast<unsigned long>(x));
  }
}

bool UnionFind::isSame(ll x, ll y) { return find(x) == find(y); }

// Union Find end

void solve(istream &ccin = cin, ostream &ccout = cout) {
  ll n, k, l;
  ccin >> n >> k >> l;
  UnionFind unf_k(n), unf_l(n);
  rep(i, k) {
    ll p, q;
    ccin >> p >> q;
    --p;
    --q;
    unf_k.unite(p, q);
  }
  rep(i, l) {
    ll r, s;
    ccin >> r >> s;
    --r;
    --s;
    unf_l.unite(r, s);
  }

  rep(i, n) {
    ll cnt = 0;
    rep(j, n) {
      if (unf_l.isSame(i, j) and unf_k.isSame(i, j))
        ++cnt;
    }
    ccout << cnt << " ";
  }
  ccout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define REP(i, s, n) for (int i = s; i < (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
ll inf = ll(1e18);
using ll1d = vector<ll>;
using ll2d = vector<ll1d>;
using ll3d = vector<ll2d>;
// Union Find begin

class UnionFind {
private:
  vector<ll> _par;
  vector<ll> _rank;

public:
  explicit UnionFind(ll n);
  ll find(ll x);
  void unite(ll x, ll y);
  bool isSame(ll x, ll y);
};

UnionFind::UnionFind(ll n) {
  for (ll i = 0; i < n; ++i) {
    _par.push_back(i);
    _rank.push_back(0);
  }
}

ll UnionFind::find(ll x) {
  if (_par.at(static_cast<unsigned long>(x)) == x)
    return x;
  else
    return _par.at(static_cast<unsigned long>(x)) =
               find(_par.at(static_cast<unsigned long>(x)));
}

void UnionFind::unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (_rank.at(static_cast<unsigned long>(x)) <
      _rank.at(static_cast<unsigned long>(y)))
    _par.at(static_cast<unsigned long>(x)) = y;
  else {
    _par.at(static_cast<unsigned long>(y)) = x;
    if (_rank.at(static_cast<unsigned long>(x)) ==
        _rank.at(static_cast<unsigned long>(y)))
      ++_rank.at(static_cast<unsigned long>(x));
  }
}

bool UnionFind::isSame(ll x, ll y) { return find(x) == find(y); }

// Union Find end

void solve(istream &ccin = cin, ostream &ccout = cout) {
  ll n, k, l;
  ccin >> n >> k >> l;
  UnionFind unf_k(n), unf_l(n);
  rep(i, k) {
    ll p, q;
    ccin >> p >> q;
    --p;
    --q;
    unf_k.unite(p, q);
  }
  rep(i, l) {
    ll r, s;
    ccin >> r >> s;
    --r;
    --s;
    unf_l.unite(r, s);
  }

  map<pair<ll, ll>, ll> mp;

  rep(i, n) { ++mp[pair<ll, ll>(unf_k.find(i), unf_l.find(i))]; }
  rep(i, n) { ccout << mp[pair<ll, ll>(unf_k.find(i), unf_l.find(i))] << " "; }
  ccout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}