Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

const int N = 2e5 + 10;
int f[N], g[N];
int find(int f[], int x) {
  if (f[x] != f[f[x]])
    f[x] = find(f, f[x]);
  return f[x];
}

int main() {
  int n, m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  for (int i = 0; i < n * 4; ++i) {
    f[i] = g[i] = i;
  }
  while (m1--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    int rx = find(f, x), ry = find(f, y);
    if (rx != ry) {
      f[ry] = rx;
    }
  }
  while (m2--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    int rx = find(g, x), ry = find(g, y);
    if (rx != ry) {
      g[ry] = rx;
    }
  }
  map<ii, int> cnt;
  for (int i = 0; i < n; ++i) {
    int u = find(f, i), v = find(g, i);
    cnt[{u, v}]++;
  }
  for (int i = 0; i < n; ++i) {
    int u = find(f, i), v = find(g, i);
    printf("%d%c", cnt[{u, v}], " \n"[i + 1 == n]);
  }
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

const int N = 2e5 + 10;
int f[N], g[N];
int find(int f[], int x) {
  if (f[x] != f[f[x]])
    f[x] = find(f, f[x]);
  return f[x];
}

int main() {
  int n, m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  for (int i = 0; i < n; ++i) {
    f[i] = g[i] = i;
  }
  while (m1--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    int rx = find(f, x), ry = find(f, y);
    if (rx != ry) {
      f[ry] = rx;
    }
  }
  while (m2--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    int rx = find(g, x), ry = find(g, y);
    if (rx != ry) {
      g[ry] = rx;
    }
  }
  map<ii, int> cnt;
  for (int i = 0; i < n; ++i) {
    int u = find(f, i), v = find(g, i);
    cnt[{u, v}]++;
  }
  for (int i = 0; i < n; ++i) {
    int u = find(f, i), v = find(g, i);
    printf("%d%c", cnt[{u, v}], " \n"[i + 1 == n]);
  }
  return 0;
}