Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
// #include <unordered_map>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utility>

using namespace std;

#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define RFOR(i, j, k) for (int i = j; i >= k; i--)
#define REP(i, j) FOR(i, 0, j)
#define RREP(i, j) RFOR(i, j, 0)
#define PI 3.1415926535897932384626433832795
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

#define UNION_FIND_MAX_N 100005

struct union_find {

  int par[UNION_FIND_MAX_N];
  int ran[UNION_FIND_MAX_N];

  union_find(int __n) { init(__n); }

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      ran[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 (ran[x] < ran[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (ran[x] == ran[y])
        ran[x]++;
    }
  }

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

int main() {

  int N, K, L;
  cin >> N >> K >> L;
  union_find uf1(N);
  int p, q;
  REP(i, K) {
    cin >> p >> q;
    p--;
    q--;

    uf1.unite(p, q);
  }

  union_find uf2(N);
  int r, s;
  REP(i, L) {
    cin >> r >> s;
    r--;
    s--;

    uf2.unite(r, s);
  }

  map<pair<int, int>, int> m;
  REP(i, N) {
    pair<int, int> p = make_pair(uf1.find(i), uf2.find(i));
    m[p]++;
  }

  REP(i, N) {
    pair<int, int> p = make_pair(uf1.find(i), uf2.find(i));
    cout << m[p] << " ";
    //        cout << res[i] << " ";
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
// #include <unordered_map>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utility>

using namespace std;

#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define RFOR(i, j, k) for (int i = j; i >= k; i--)
#define REP(i, j) FOR(i, 0, j)
#define RREP(i, j) RFOR(i, j, 0)
#define PI 3.1415926535897932384626433832795
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

#define UNION_FIND_MAX_N 200005

struct union_find {

  int par[UNION_FIND_MAX_N];
  int ran[UNION_FIND_MAX_N];

  union_find(int __n) { init(__n); }

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      ran[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 (ran[x] < ran[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (ran[x] == ran[y])
        ran[x]++;
    }
  }

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

int main() {

  int N, K, L;
  cin >> N >> K >> L;
  union_find uf1(N);
  int p, q;
  REP(i, K) {
    cin >> p >> q;
    p--;
    q--;

    uf1.unite(p, q);
  }

  union_find uf2(N);
  int r, s;
  REP(i, L) {
    cin >> r >> s;
    r--;
    s--;

    uf2.unite(r, s);
  }

  map<pair<int, int>, int> m;
  REP(i, N) {
    pair<int, int> p = make_pair(uf1.find(i), uf2.find(i));
    m[p]++;
  }

  REP(i, N) {
    pair<int, int> p = make_pair(uf1.find(i), uf2.find(i));
    cout << m[p] << " ";
    //        cout << res[i] << " ";
  }
  cout << endl;

  return 0;
}