Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
static const double EPS = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof(a))
#define minus(a) memset(a, 0xff, sizeof(a))
#define chmax(a, b) a = min((a), (b))
#define chmin(a, b) a = max((a), (b))

#define MOD 1000000007

typedef pair<int, int> P; // pairは小文字

class uf {
public:
  int par[20005];
  int level[20005];

  void init(int n) {
    rep(i, n) {
      par[i] = i;
      level[i] = 0;
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (level[x] < level[y])
      par[x] = y;
    else {
      par[y] = x;
    }
    if (level[x] == level[y])
      level[x]++;
  }

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

int n, k, l;

int main() {
  int a, b;
  scanf("%d %d %d ", &n, &k, &l);
  uf uf1, uf2;
  uf1.init(n);
  uf2.init(n);
  rep(i, k) {
    scanf("%d %d ", &a, &b);
    a--;
    b--; //
    uf1.unite(a, b);
  }
  rep(i, l) {
    scanf("%d %d ", &a, &b);
    a--;
    b--;
    uf2.unite(a, b);
  }
  map<P, int> m;
  rep(i, n) {
    a = uf1.find(i);
    b = uf2.find(i);
    m[P(a, b)]++;
  }
  rep(i, n) {
    a = uf1.find(i);
    b = uf2.find(i);
    cout << m[P(a, b)] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
static const double EPS = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof(a))
#define minus(a) memset(a, 0xff, sizeof(a))
#define chmax(a, b) a = min((a), (b))
#define chmin(a, b) a = max((a), (b))

#define MOD 1000000007

typedef pair<int, int> P; // pairは小文字

class uf {
public:
  int par[200005];
  int level[200005];

  void init(int n) {
    rep(i, n) {
      par[i] = i;
      level[i] = 0;
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (level[x] < level[y])
      par[x] = y;
    else {
      par[y] = x;
    }
    if (level[x] == level[y])
      level[x]++;
  }

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

int n, k, l;

int main() {
  int a, b;
  scanf("%d %d %d ", &n, &k, &l);
  uf uf1, uf2;
  uf1.init(n);
  uf2.init(n);
  rep(i, k) {
    scanf("%d %d ", &a, &b);
    a--;
    b--; //
    uf1.unite(a, b);
  }
  rep(i, l) {
    scanf("%d %d ", &a, &b);
    a--;
    b--;
    uf2.unite(a, b);
  }
  map<P, int> m;
  rep(i, n) {
    a = uf1.find(i);
    b = uf2.find(i);
    m[P(a, b)]++;
  }
  rep(i, n) {
    a = uf1.find(i);
    b = uf2.find(i);
    cout << m[P(a, b)] << " ";
  }
  cout << endl;
  return 0;
}