Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
typedef long long int ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for (int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define BIT(b, i) (((b) >> (i)) & 1)
#define LOG_F 1
#define LOG(...)                                                               \
  if (LOG_F)                                                                   \
  fprintf(stderr, __VA_ARGS__)

using namespace std;

ll pow(int x, int n) { return n == 0 ? 1 : x * pow(x, n - 1); }
struct union_find {
  vector<int> par;
  vector<int> rank;
  union_find(int n) {
    rank = vector<int>(n, 0);
    for (int i = 0; i < n; i++) {
      par.push_back(i);
    }
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx == ry)
      return;

    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry])
        rank[rx]++;
    }
  }

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

/*
n k l
p1 q1
...
pk qk
r1 s1
...
rl sl

n 2*10^5
k.l 10^5
p q r s <= n
p < q
r < s
 */
int n, k, l;

int main() {
  cin >> n >> k >> l;
  union_find uf1(n);
  union_find uf2(n);
  REP(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    uf1.unite(p, q);
  }
  REP(j, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    uf2.unite(r, s);
  }
  int root[n][n];
  REP(i, n)
  fill(root[i], root[i] + n, 0);

  REP(i, n)
  root[uf1.root(i)][uf2.root(i)]++;

  REP(i, n)
  cout << root[uf1.root(i)][uf2.root(i)] << " ";

  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
typedef long long int ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for (int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define BIT(b, i) (((b) >> (i)) & 1)
#define LOG_F 1
#define LOG(...)                                                               \
  if (LOG_F)                                                                   \
  fprintf(stderr, __VA_ARGS__)

using namespace std;

ll pow(int x, int n) { return n == 0 ? 1 : x * pow(x, n - 1); }
struct union_find {
  vector<int> par;
  vector<int> rank;
  union_find(int n) {
    rank = vector<int>(n, 0);
    for (int i = 0; i < n; i++) {
      par.push_back(i);
    }
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx == ry)
      return;

    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry])
        rank[rx]++;
    }
  }

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

/*
n k l
p1 q1
...
pk qk
r1 s1
...
rl sl

n 2*10^5
k.l 10^5
p q r s <= n
p < q
r < s
 */
int n, k, l;

int main() {
  cin >> n >> k >> l;
  union_find uf1(n);
  union_find uf2(n);
  REP(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    uf1.unite(p, q);
  }
  REP(j, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    uf2.unite(r, s);
  }
  map<int, map<int, int>> root;

  REP(i, n)
  root[uf1.root(i)][uf2.root(i)]++;

  REP(i, n)
  cout << root[uf1.root(i)][uf2.root(i)] << " ";

  cout << endl;
  return 0;
}