Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(s) s.begin(), s.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;
const int DIV = 1e9 + 7;

int rpar[202020];
int rrank[202020];
int tpar[202020];
int trank[202020];
int n, k, l;

void init() {
  rep(i, n) {
    rpar[i] = i;
    rrank[i] = 0;
    tpar[i] = i;
    trank[i] = 0;
  }
}

int rfind(int x) {
  if (rpar[x] == x)
    return x;
  else
    return rpar[x] = rfind(rpar[x]);
}
int tfind(int x) {
  if (tpar[x] == x)
    return x;
  else
    return tpar[x] = tfind(tpar[x]);
}

void runit(int x, int y) {
  x = rfind(x);
  y = rfind(y);
  if (x == y)
    return;

  if (rrank[x] < rrank[y])
    rpar[x] = y;
  else {
    rpar[y] = x;
    if (rrank[x] == rrank[y])
      rrank[x]++;
  }
}
void tunit(int x, int y) {
  x = tfind(x);
  y = tfind(y);
  if (x == y)
    return;

  if (trank[x] < trank[y])
    tpar[x] = y;
  else {
    tpar[y] = x;
    if (trank[x] == trank[y])
      trank[x]++;
  }
}

bool rsame(int x, int y) { return rfind(x) == rfind(y); }
bool tsame(int x, int y) { return tfind(x) == tfind(y); }

int main() {
  cin >> n >> k >> l;
  init();
  int a, b;
  rep(i, k) {
    cin >> a >> b;
    runit(a - 1, b - 1);
  }
  rep(i, l) {
    cin >> a >> b;
    tunit(a - 1, b - 1);
  }
  int ans;
  vi cand;
  rep(i, n) {
    ans = 0;
    cand.clear();
    rep(j, n) if (rpar[i] == rpar[j]) cand.pb(j);
    for (auto c : cand)
      if (tpar[c] == tpar[i])
        ans++;
    cout << ans << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(s) s.begin(), s.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;
const int DIV = 1e9 + 7;

int rpar[202020];
int rrank[202020];
int tpar[202020];
int trank[202020];
int n, k, l;

void init() {
  rep(i, n) {
    rpar[i] = i;
    rrank[i] = 0;
    tpar[i] = i;
    trank[i] = 0;
  }
}

int rfind(int x) {
  if (rpar[x] == x)
    return x;
  else
    return rpar[x] = rfind(rpar[x]);
}
int tfind(int x) {
  if (tpar[x] == x)
    return x;
  else
    return tpar[x] = tfind(tpar[x]);
}

void runit(int x, int y) {
  x = rfind(x);
  y = rfind(y);
  if (x == y)
    return;

  if (rrank[x] < rrank[y])
    rpar[x] = y;
  else {
    rpar[y] = x;
    if (rrank[x] == rrank[y])
      rrank[x]++;
  }
}
void tunit(int x, int y) {
  x = tfind(x);
  y = tfind(y);
  if (x == y)
    return;

  if (trank[x] < trank[y])
    tpar[x] = y;
  else {
    tpar[y] = x;
    if (trank[x] == trank[y])
      trank[x]++;
  }
}

bool rsame(int x, int y) { return rfind(x) == rfind(y); }
bool tsame(int x, int y) { return tfind(x) == tfind(y); }

int main() {
  cin >> n >> k >> l;
  init();
  int a, b;
  rep(i, k) {
    cin >> a >> b;
    runit(a - 1, b - 1);
  }
  rep(i, l) {
    cin >> a >> b;
    tunit(a - 1, b - 1);
  }
  map<P, int> link;
  rep(i, n) link[mp(tfind(i), rfind(i))]++;
  rep(i, n) cout << link[mp(tfind(i), rfind(i))] << " ";
  cout << endl;
  return 0;
}