Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

vector<int> par;

void Initpar(int n) {
  par = vector<int>(n);
  for (int i = 0; i < n; i++)
    par.at(i) = i;
}

int root(int x) {
  if (x == par.at(x))
    return x;
  else
    par.at(x) = root(par.at(x));
}

int rootpar(int x, vi pa) {
  if (x == pa.at(x))
    return x;
  else
    pa.at(x) = rootpar(pa.at(x), pa);
}

void unite(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx == ry)
    return;
  if (rx < ry)
    par.at(ry) = rx;
  else
    par.at(rx) = ry;
}

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

int main() {
  long n, k, l;
  cin >> n >> k >> l;
  vi par1, par2;
  Initpar(n);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q);
  }
  par1 = par;
  Initpar(n);
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    unite(r, s);
  }
  par2 = par;
  rep(i, n) {
    rootpar(i, par1);
    rootpar(i, par2);
  }
  map<pii, int> mp;
  rep(i, n) {
    pii P = {par1.at(i), par2.at(i)};
    if (mp.count(P))
      mp.at(P)++;
    else
      mp[P] = 1;
  }
  rep(i, n) {
    pii P = {par1.at(i), par2.at(i)};
    cout << mp.at(P) << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

vector<int> par;

void Initpar(int n) {
  par = vector<int>(n);
  for (int i = 0; i < n; i++)
    par.at(i) = i;
}

int root(int x) {
  if (x == par.at(x))
    return x;
  else
    par.at(x) = root(par.at(x));
}

int rootpar(int x, vi &pa) {
  if (x == pa.at(x))
    return x;
  else
    pa.at(x) = rootpar(pa.at(x), pa);
}

void unite(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx == ry)
    return;
  if (rx < ry)
    par.at(ry) = rx;
  else
    par.at(rx) = ry;
}

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

int main() {
  long n, k, l;
  cin >> n >> k >> l;
  vi par1, par2;
  Initpar(n);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q);
  }
  par1 = par;
  Initpar(n);
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    unite(r, s);
  }
  par2 = par;
  rep(i, n) {
    rootpar(i, par1);
    rootpar(i, par2);
  }
  map<pii, int> mp;
  rep(i, n) {
    pii P = {par1.at(i), par2.at(i)};
    if (mp.count(P))
      mp.at(P)++;
    else
      mp[P] = 1;
  }
  rep(i, n) {
    pii P = {par1.at(i), par2.at(i)};
    cout << mp.at(P) << " ";
  }
  cout << endl;
}