Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 10000

int main(void) {
  pll num[MAX];
  map<pll, ll> mp;
  ll s, t, r;
  vll f1[MAX], f2[MAX];
  queue<ll> que;
  ll group = 0;

  cin >> n >> k >> r;

  FOR(i, 0, k) {
    cin >> s;
    cin >> t;
    s--;
    t--;
    f1[s].push_back(t);
    f1[t].push_back(s);
  }

  FOR(i, 0, r) {
    cin >> s;
    cin >> t;
    s--;
    t--;
    f2[s].push_back(t);
    f2[t].push_back(s);
  }

  FOR(i, 0, n) {
    num[i].first = -1;
    num[i].second = -1;
  }

  FOR(i, 0, n) {
    if (num[i].first != -1)
      continue;
    que.push(i);
    num[que.front()].first = group;

    while (!que.empty()) {
      temp = que.front();
      que.pop();
      FOR(j, 0, f1[temp].size()) {
        k = f1[temp][j];
        if (num[k].first == -1) {
          que.push(k);
          num[k].first = group;
        }
      }
    }
    group++;
  }

  group = 0;

  FOR(i, 0, n) {
    if (num[i].second != -1)
      continue;
    que.push(i);
    num[que.front()].second = group;

    while (!que.empty()) {
      temp = que.front();
      que.pop();
      FOR(j, 0, f2[temp].size()) {
        k = f2[temp][j];
        if (num[k].second == -1) {
          que.push(k);
          num[k].second = group;
        }
      }
    }
    group++;
  }

  FOR(i, 0, n) { mp[num[i]]++; }

  FOR(i, 0, n) { cout << mp[num[i]] << " "; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 220000

int main(void) {
  pll num[MAX];
  map<pll, ll> mp;
  ll s, t, r;
  vll f1[MAX], f2[MAX];
  queue<ll> que;
  ll group = 0;

  cin >> n >> k >> r;

  FOR(i, 0, k) {
    cin >> s;
    cin >> t;
    s--;
    t--;
    f1[s].push_back(t);
    f1[t].push_back(s);
  }

  FOR(i, 0, r) {
    cin >> s;
    cin >> t;
    s--;
    t--;
    f2[s].push_back(t);
    f2[t].push_back(s);
  }

  FOR(i, 0, n) {
    num[i].first = -1;
    num[i].second = -1;
  }

  FOR(i, 0, n) {
    if (num[i].first != -1)
      continue;
    que.push(i);
    num[que.front()].first = group;

    while (!que.empty()) {
      temp = que.front();
      que.pop();
      FOR(j, 0, f1[temp].size()) {
        k = f1[temp][j];
        if (num[k].first == -1) {
          que.push(k);
          num[k].first = group;
        }
      }
    }
    group++;
  }

  group = 0;

  FOR(i, 0, n) {
    if (num[i].second != -1)
      continue;
    que.push(i);
    num[que.front()].second = group;

    while (!que.empty()) {
      temp = que.front();
      que.pop();
      FOR(j, 0, f2[temp].size()) {
        k = f2[temp][j];
        if (num[k].second == -1) {
          que.push(k);
          num[k].second = group;
        }
      }
    }
    group++;
  }

  FOR(i, 0, n) { mp[num[i]]++; }

  FOR(i, 0, n) { cout << mp[num[i]] << " "; }

  return 0;
}