Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v, 0, sizeof(v))
#define MP(a, b) make_pair((a), (b))
#define ABS(a) ((a) > 0 ? (a) : -(a))
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)

typedef pair<int, int> pii;

int n, k, l, p, q, r, s;
int pa[2][200000], sz[2][200000];
pii rts[200000];

int root(int k, int v) {
  while (pa[k][v] != -1)
    v = pa[k][v];
  return v;
}

int merge(int k, int u, int v) {
  int ru = root(k, u), rv = root(k, v);
  if (ru == rv)
    return ru;
  if (sz[k][ru] > sz[k][rv])
    return merge(k, v, u);
  pa[k][ru] = rv;
  sz[k][ru] += sz[k][rv];
  return rv;
}

int main(int argc, char **argv) {
  cin >> n >> k >> l;
  REP(i, n) {
    pa[0][i] = pa[1][i] = -1;
    sz[0][i] = sz[1][i] = 1;
  }
  REP(i, k) {
    cin >> p >> q;
    --p;
    --q;
    merge(0, p, q);
  }
  REP(i, l) {
    cin >> r >> s;
    --r;
    --s;
    merge(1, r, s);
  }

  map<pii, int> cnt;
  REP(i, n) {
    rts[i] = MP(root(0, i), root(1, i));
    if (cnt.find(rts[i]) == cnt.end()) {
      cnt.insert(MP(rts[i], 1));
    } else {
      ++cnt[rts[i]];
    }
  }

  REP(i, n) {
    if (i != 0)
      cout << " ";
    cout << cnt[rts[i]];
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v, 0, sizeof(v))
#define MP(a, b) make_pair((a), (b))
#define ABS(a) ((a) > 0 ? (a) : -(a))
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)

typedef pair<int, int> pii;

int n, k, l, p, q, r, s;
int pa[2][200000], sz[2][200000];
pii rts[200000];

int root(int k, int v) {
  while (pa[k][v] != -1)
    v = pa[k][v];
  return v;
}

int merge(int k, int u, int v) {
  int ru = root(k, u), rv = root(k, v);
  if (ru == rv)
    return ru;
  if (sz[k][ru] > sz[k][rv])
    return merge(k, v, u);
  pa[k][ru] = rv;
  sz[k][rv] += sz[k][ru];
  return rv;
}

int main(int argc, char **argv) {
  cin >> n >> k >> l;
  REP(i, n) {
    pa[0][i] = pa[1][i] = -1;
    sz[0][i] = sz[1][i] = 1;
  }
  REP(i, k) {
    cin >> p >> q;
    --p;
    --q;
    merge(0, p, q);
  }
  REP(i, l) {
    cin >> r >> s;
    --r;
    --s;
    merge(1, r, s);
  }

  map<pii, int> cnt;
  REP(i, n) {
    rts[i] = MP(root(0, i), root(1, i));
    if (cnt.find(rts[i]) == cnt.end()) {
      cnt.insert(MP(rts[i], 1));
    } else {
      ++cnt[rts[i]];
    }
  }

  REP(i, n) {
    if (i != 0)
      cout << " ";
    cout << cnt[rts[i]];
  }
  cout << endl;

  return 0;
}