Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
const ll inf = 1e9 + 7;
const int MAX_N = 1e5 + 5;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<ll> prime;

ll inv[MAX_N], fac[MAX_N];

template <class T = int> T in() {
  T x;
  cin >> x;
  return (x);
}
inline ll GCD(ll a, ll b) {
  ll c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
inline ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
inline ll POW(ll a, ll b) {
  ll c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % inf;
    }
    a = a * a % inf;
    b >>= 1;
  }
  return c;
}
inline void _nCr() {
  fac[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    fac[i] = fac[i - 1] * i % inf;
  }
  for (int i = 0; i < MAX_N; i++) {
    inv[i] = POW(fac[i], inf - 2);
  }
}
inline ll nCr(ll n, ll r) { return (fac[n] * inv[r] % inf) * inv[n - r] % inf; }
inline void PRI(ll n) {
  bool a[n + 1];
  for (int i = 0; i < n + 1; i++) {
    a[i] = 1;
  }
  for (int i = 2; i < n + 1; i++) {
    if (a[i]) {
      prime.pb(i);
      ll b = i;
      while (b <= n) {
        a[b] = 0;
        b += i;
      }
    }
  }
}

class UnionFind {
private:
  int par[MAX_N];
  int rank[MAX_N];

public:
  void init(int n);
  int find(int x);
  void unite(int x, int y);
  bool same(int x, int y);
};

void UnionFind::init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank[i] = 0;
  }
}

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

void UnionFind::unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (rank[x] < rank[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
}

bool UnionFind::same(int x, int y) {
  x = find(x);
  y = find(y);
  return x == y;
}
UnionFind d, t;

typedef pair<pair<int, int>, int> pa;

int main() {
  int n = in(), k = in(), l = in();
  d.init(n);
  t.init(n);
  for (int i = 0; i < k; i++) {
    int x = in(), y = in();
    d.unite(x - 1, y - 1);
  }
  for (int i = 0; i < l; i++) {
    int x = in(), y = in();
    t.unite(x - 1, y - 1);
  }
  int ans[n];
  vector<pa> dtpar;
  for (int i = 0; i < n; i++) {
    ans[i] = 0;
    dtpar.pb({make_pair(d.find(i), t.find(i)), (int)i});
  }
  sort(dtpar.begin(), dtpar.end());
  int spos = 0, epos = 0, score = 1;
  for (int i = 1; i < dtpar.size(); i++) {
    // cout << dtpar[i].first.first << " " << dtpar[i].first.second << endl;
    if (dtpar[i - 1].first == dtpar[i].first) {
      epos = i;
      score++;
    } else {
      for (int j = spos; j <= epos; j++) {
        ans[dtpar[j].second] += score;
      }
      score = 1;
      spos = i;
      epos = i;
    }
  }
  for (int j = spos; j <= epos; j++) {
    ans[dtpar[j].second] += score;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
const ll inf = 2e9 + 7;
const int MAX_N = 5e5 + 5;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<ll> prime;

ll inv[MAX_N], fac[MAX_N];

template <class T = int> T in() {
  T x;
  cin >> x;
  return (x);
}
inline ll GCD(ll a, ll b) {
  ll c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
inline ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
inline ll POW(ll a, ll b) {
  ll c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % inf;
    }
    a = a * a % inf;
    b >>= 1;
  }
  return c;
}
inline void _nCr() {
  fac[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    fac[i] = fac[i - 1] * i % inf;
  }
  for (int i = 0; i < MAX_N; i++) {
    inv[i] = POW(fac[i], inf - 2);
  }
}
inline ll nCr(ll n, ll r) { return (fac[n] * inv[r] % inf) * inv[n - r] % inf; }
inline void PRI(ll n) {
  bool a[n + 1];
  for (int i = 0; i < n + 1; i++) {
    a[i] = 1;
  }
  for (int i = 2; i < n + 1; i++) {
    if (a[i]) {
      prime.pb(i);
      ll b = i;
      while (b <= n) {
        a[b] = 0;
        b += i;
      }
    }
  }
}

class UnionFind {
private:
  int par[MAX_N];
  int rank[MAX_N];

public:
  void init(int n);
  int find(int x);
  void unite(int x, int y);
  bool same(int x, int y);
};

void UnionFind::init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank[i] = 0;
  }
}

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

void UnionFind::unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (rank[x] < rank[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
}

bool UnionFind::same(int x, int y) {
  x = find(x);
  y = find(y);
  return x == y;
}
UnionFind d, t;

typedef pair<pair<int, int>, int> pa;

int main() {
  int n = in(), k = in(), l = in();
  d.init(n);
  t.init(n);
  for (int i = 0; i < k; i++) {
    int x = in(), y = in();
    d.unite(x - 1, y - 1);
  }
  for (int i = 0; i < l; i++) {
    int x = in(), y = in();
    t.unite(x - 1, y - 1);
  }
  int ans[n];
  vector<pa> dtpar;
  for (int i = 0; i < n; i++) {
    ans[i] = 0;
    dtpar.pb({make_pair(d.find(i), t.find(i)), (int)i});
  }
  sort(dtpar.begin(), dtpar.end());
  int spos = 0, epos = 0, score = 1;
  for (int i = 1; i < dtpar.size(); i++) {
    // cout << dtpar[i].first.first << " " << dtpar[i].first.second << endl;
    if (dtpar[i - 1].first == dtpar[i].first) {
      epos = i;
      score++;
    } else {
      for (int j = spos; j <= epos; j++) {
        ans[dtpar[j].second] += score;
      }
      score = 1;
      spos = i;
      epos = i;
    }
  }
  for (int j = spos; j <= epos; j++) {
    ans[dtpar[j].second] += score;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}