Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define typeof(x) __typeof__(x)
#define int long long int
#define double long double
#define mod(x) ((x % MOD) + MOD) % MOD
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SZ(c) (int)((c).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define LB(c, x) (int)(lower_bound(ALL(c), x) - (c).begin())
#define UB(c, x) (int)(upper_bound(ALL(c), x) - (c).begin())
#define COUNT(c, x) (UB(c, x) - LB(c, x))
#define UNIQUE(c)                                                              \
  SORT(c);                                                                     \
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (s).find(e) != (s).end()
#define PB push_back
#define MP make_pair
#define vec vector
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;

using namespace std;

typedef pair<int, int> P;
struct edge {
  int from, to;
  int cost;
};
ostream &operator<<(ostream &s, const edge &e) {
  s << "(" << e.from << "->" << e.to << ": " << e.cost << ")";
  return s;
}
bool operator<(const edge &edge1, const edge &edge2) {
  return edge1.cost < edge2.cost;
};

const int INF = 1e18;
const int MOD = 1e9 + 7;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  for (int i = 0; i < v.size(); i++) {
    s << v[i];
    if (i < v.size() - 1)
      s << " ";
  }
  s << "]";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{";
  for (auto i = m.begin(); i != m.end(); ++i) {
    s << i->first << ":" << i->second;
    if (next(i) != m.end())
      s << ", ";
  }
  s << "}";
  return s;
}

class UnionFind {
private:
  vector<int> par, myrank;

public:
  UnionFind(int n) {
    par.resize(n);
    myrank.resize(n, 0);
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (myrank[x] < myrank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (myrank[x] == myrank[y])
        myrank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  vec<int> p(K), q(K), r(K), s(K);
  REP(i, 0, K) {
    cin >> p[i] >> q[i];
    p[i]--;
    q[i]--;
  }
  REP(i, 0, L) {
    cin >> r[i] >> s[i];
    r[i]--;
    s[i]--;
  }

  UnionFind uni0(N), uni1(N);
  REP(i, 0, K) { uni0.unite(p[i], q[i]); }
  REP(i, 0, L) { uni1.unite(r[i], s[i]); }

  map<P, int> count;
  REP(i, 0, N) { count[MP(uni0.find(i), uni1.find(i))]++; }

  REP(i, 0, N) { cout << count[MP(uni0.find(i), uni1.find(i))] << endl; }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define typeof(x) __typeof__(x)
#define int long long int
#define double long double
#define mod(x) ((x % MOD) + MOD) % MOD
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SZ(c) (int)((c).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define LB(c, x) (int)(lower_bound(ALL(c), x) - (c).begin())
#define UB(c, x) (int)(upper_bound(ALL(c), x) - (c).begin())
#define COUNT(c, x) (UB(c, x) - LB(c, x))
#define UNIQUE(c)                                                              \
  SORT(c);                                                                     \
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (s).find(e) != (s).end()
#define PB push_back
#define MP make_pair
#define vec vector
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;

using namespace std;

typedef pair<int, int> P;
struct edge {
  int from, to;
  int cost;
};
ostream &operator<<(ostream &s, const edge &e) {
  s << "(" << e.from << "->" << e.to << ": " << e.cost << ")";
  return s;
}
bool operator<(const edge &edge1, const edge &edge2) {
  return edge1.cost < edge2.cost;
};

const int INF = 1e18;
const int MOD = 1e9 + 7;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  for (int i = 0; i < v.size(); i++) {
    s << v[i];
    if (i < v.size() - 1)
      s << " ";
  }
  s << "]";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{";
  for (auto i = m.begin(); i != m.end(); ++i) {
    s << i->first << ":" << i->second;
    if (next(i) != m.end())
      s << ", ";
  }
  s << "}";
  return s;
}

class UnionFind {
private:
  vector<int> par, myrank;

public:
  UnionFind(int n) {
    par.resize(n);
    myrank.resize(n, 0);
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (myrank[x] < myrank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (myrank[x] == myrank[y])
        myrank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

signed main() {
  int N, K, L;
  cin >> N >> K >> L;
  vec<int> p(K), q(K), r(L), s(L);
  REP(i, 0, K) {
    cin >> p[i] >> q[i];
    p[i]--;
    q[i]--;
  }
  REP(i, 0, L) {
    cin >> r[i] >> s[i];
    r[i]--;
    s[i]--;
  }

  UnionFind uni0(N), uni1(N);
  REP(i, 0, K) { uni0.unite(p[i], q[i]); }
  REP(i, 0, L) { uni1.unite(r[i], s[i]); }

  map<P, int> count;
  REP(i, 0, N) { count[MP(uni0.find(i), uni1.find(i))]++; }

  REP(i, 0, N) { cout << count[MP(uni0.find(i), uni1.find(i))] << endl; }

  return 0;
}