Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

#define MP make_pair
#define PB push_back
#define FF first
#define SS second

#define FORN(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)(n)-1; i >= 0; i--)

#define DEBUG(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define PR0(A, n)                                                              \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FORN(_, n) cout << A[_] << ' ';                                            \
    cout << endl;                                                              \
  }

#define MOD 1000000007
#define INF 2000000000

int GLL(LL &x) { return scanf("%lld", &x); }

int GI(int &x) { return scanf("%d", &x); }

const int MAXN = 100005;
int par1[MAXN], par2[MAXN];

void union_init(int d[], int s) {
  for (int i = 0; i < s; i++)
    d[i] = i;
}
int union_query(int d[], int n) {
  int res = n;
  while (d[res] != res)
    res = d[res];
  int m;
  while (d[n] != n) {
    m = d[n];
    d[n] = res;
    n = m;
  }
  return res;
};
int union_merge(int d[], int x, int y) {
  x = union_query(d, x);
  y = union_query(d, y);
  if (x == y)
    return -1;
  d[x] = y;
  return 1;
}

int n, k, l;

map<PII, int> cnt;

int main() {
  GI(n);
  GI(k);
  GI(l);

  union_init(par1, n + 1);
  union_init(par2, n + 1);

  int u, v;

  FORN(i, k) {
    GI(u);
    GI(v);
    union_merge(par1, u, v);
  }

  FORN(i, l) {
    GI(u);
    GI(v);
    union_merge(par2, u, v);
  }

  FOR1(i, n) { cnt[MP(union_query(par1, i), union_query(par2, i))]++; }

  FOR1(i, n) {
    printf("%d ", cnt[MP(union_query(par1, i), union_query(par2, i))]);
  }
  printf("\n");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

#define MP make_pair
#define PB push_back
#define FF first
#define SS second

#define FORN(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)(n)-1; i >= 0; i--)

#define DEBUG(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define PR0(A, n)                                                              \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FORN(_, n) cout << A[_] << ' ';                                            \
    cout << endl;                                                              \
  }

#define MOD 1000000007
#define INF 2000000000

int GLL(LL &x) { return scanf("%lld", &x); }

int GI(int &x) { return scanf("%d", &x); }

const int MAXN = 200005;
int par1[MAXN], par2[MAXN];

void union_init(int d[], int s) {
  for (int i = 0; i < s; i++)
    d[i] = i;
}
int union_query(int d[], int n) {
  int res = n;
  while (d[res] != res)
    res = d[res];
  int m;
  while (d[n] != n) {
    m = d[n];
    d[n] = res;
    n = m;
  }
  return res;
};
int union_merge(int d[], int x, int y) {
  x = union_query(d, x);
  y = union_query(d, y);
  if (x == y)
    return -1;
  d[x] = y;
  return 1;
}

int n, k, l;

map<PII, int> cnt;

int main() {
  GI(n);
  GI(k);
  GI(l);

  union_init(par1, n + 1);
  union_init(par2, n + 1);

  int u, v;

  FORN(i, k) {
    GI(u);
    GI(v);
    union_merge(par1, u, v);
  }

  FORN(i, l) {
    GI(u);
    GI(v);
    union_merge(par2, u, v);
  }

  FOR1(i, n) { cnt[MP(union_query(par1, i), union_query(par2, i))]++; }

  FOR1(i, n) {
    printf("%d ", cnt[MP(union_query(par1, i), union_query(par2, i))]);
  }
  printf("\n");

  return 0;
}