Problem p03857 (C++)

Buggy submission (status: Runtime Error):
// Written by Zhu Zeqi
// Come on,baby
// Hack,please
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 1000000000
typedef long long ll;
// orz yht
using namespace std;
int a[100005], b[100005], n, k, l;
map<pair<int, int>, int> m;
int f1(int x) {
  if (a[x] == x)
    return x;
  else
    return a[x] = f1(a[x]);
}
int f2(int x) {
  if (b[x] == x)
    return x;
  else
    return b[x] = f2(b[x]);
}
int main() {
  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    b[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    a[f1(x)] = f1(y);
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    cin >> x >> y;
    b[f2(x)] = f2(y);
  }
  for (int i = 1; i <= n; i++)
    m[mp(f1(i), f2(i))]++;
  for (int i = 1; i <= n; i++)
    cout << m[mp(a[i], b[i])] << " ";
  // system("pause");
  return 0;
}

Fixed submission:
// Written by Zhu Zeqi
// Come on,baby
// Hack,please
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 1000000000
typedef long long ll;
// orz yht
using namespace std;
int a[200005], b[200005], n, k, l;
map<pair<int, int>, int> m;
int f1(int x) {
  if (a[x] == x)
    return x;
  else
    return a[x] = f1(a[x]);
}
int f2(int x) {
  if (b[x] == x)
    return x;
  else
    return b[x] = f2(b[x]);
}
int main() {
  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    b[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    a[f1(x)] = f1(y);
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    cin >> x >> y;
    b[f2(x)] = f2(y);
  }
  for (int i = 1; i <= n; i++)
    m[mp(f1(i), f2(i))]++;
  for (int i = 1; i <= n; i++)
    cout << m[mp(a[i], b[i])] << " ";
  // system("pause");
  return 0;
}