Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define pii pair<int, int>
#define pdd pair<db, db>

using namespace std;

typedef long long ll;
typedef double db;

const int N = 1e4 + 10, M = 1e4 + 10, MOD = 1e9 + 7;

int fa[N * 2], fb[2 * N];
int ans[N * 2];

void init(int n) {
  for (int i = 0; i <= n; i++)
    fa[i] = fb[i] = i;
}

int finda(int x) {
  if (fa[x] != x)
    fa[x] = finda(fa[x]);
  return fa[x];
}
int findb(int x) {
  if (fb[x] != x)
    fb[x] = findb(fb[x]);
  return fb[x];
}

int main() {
  int n, k, l;
  while (~scanf("%d%d%d", &n, &k, &l)) {
    init(n);
    for (int i = 0; i < k; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int fx = finda(x), fy = finda(y);
      if (fx != fy)
        fa[fx] = fy;
    }
    for (int i = 0; i < l; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int fx = findb(x), fy = findb(y);
      if (fx != fy)
        fb[fx] = fy;
    }
    map<int, pii> m1;
    map<pii, int> m2;
    for (int i = 1; i <= n; i++) {
      int xa = finda(i), xb = findb(i);
      // cout<<xa<<" "<<xb<<endl;
      m1[i] = mp(xa, xb);
      m2[mp(xa, xb)]++;
      // cout<<"("<<m1[i].x<<","<<m1[i].y<<") "<<m2[mp(xa,xb)]<<endl<<endl;
    }

    for (int i = 1; i <= n; i++) {
      ans[i] = m2[m1[i]];
    }
    for (int i = 1; i <= n; i++)
      cout << ans[i] << " ";
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define pii pair<int, int>
#define pdd pair<db, db>

using namespace std;

typedef long long ll;
typedef double db;

const int N = 1e5 + 10, M = 1e4 + 10, MOD = 1e9 + 7;

int fa[N * 2], fb[2 * N];
int ans[N * 2];

void init(int n) {
  for (int i = 0; i <= n; i++)
    fa[i] = fb[i] = i;
}

int finda(int x) {
  if (fa[x] != x)
    fa[x] = finda(fa[x]);
  return fa[x];
}
int findb(int x) {
  if (fb[x] != x)
    fb[x] = findb(fb[x]);
  return fb[x];
}

int main() {
  int n, k, l;
  while (~scanf("%d%d%d", &n, &k, &l)) {
    init(n);
    for (int i = 0; i < k; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int fx = finda(x), fy = finda(y);
      if (fx != fy)
        fa[fx] = fy;
    }
    for (int i = 0; i < l; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int fx = findb(x), fy = findb(y);
      if (fx != fy)
        fb[fx] = fy;
    }
    map<int, pii> m1;
    map<pii, int> m2;
    for (int i = 1; i <= n; i++) {
      int xa = finda(i), xb = findb(i);
      // cout<<xa<<" "<<xb<<endl;
      m1[i] = mp(xa, xb);
      m2[mp(xa, xb)]++;
      // cout<<"("<<m1[i].x<<","<<m1[i].y<<") "<<m2[mp(xa,xb)]<<endl<<endl;
    }

    for (int i = 1; i <= n; i++) {
      ans[i] = m2[m1[i]];
    }
    for (int i = 1; i <= n; i++)
      cout << ans[i] << " ";
    cout << endl;
  }
}