Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const LL mod = 1000000007;
const int N = 150 + 5;
const double Pi = acos(-1.0);
const int maxn = 200005;
using namespace std;
int fa[maxn];
int fb[maxn];
int find(int *fa, int x) { return x == fa[x] ? x : fa[x] = find(fa, fa[x]); }
void init() {
  for (int i = 0; i < maxn; i++) {
    fa[i] = fb[i] = i;
  }
}
set<int> S[2][maxn];
int ans[maxn];
int G[maxn];
bool vis[maxn];
map<pair<int, int>, int> M;
int main() {
#ifdef local
  freopen("in", "r", stdin);
#endif
  init();
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    u = find(fa, u);
    v = find(fa, v);
    fa[u] = v;
  }
  for (int i = 0; i < l; i++) {
    int u, v;
    cin >> u >> v;
    u = find(fb, u);
    v = find(fb, v);
    fb[u] = v;
  }
  for (int i = 1; i <= n; i++) {
    S[0][find(fa, i)].insert(i);
    S[1][find(fb, i)].insert(i);
  }
  for (int i = 1; i <= n; i++) {
    if (M[MP(fa[i], fb[i])]) {
      ans[i] = M[MP(fa[i], fb[i])];
      continue;
    }
    for (int it : S[0][fa[i]]) {
      if (S[1][fb[i]].count(it))
        ans[i]++;
    }
    M[MP(fa[i], fb[i])] = ans[i];
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const LL mod = 1000000007;
const int N = 150 + 5;
const double Pi = acos(-1.0);
const int maxn = 200005;
using namespace std;
int fa[maxn];
int fb[maxn];
int find(int *fa, int x) { return x == fa[x] ? x : fa[x] = find(fa, fa[x]); }
void init() {
  for (int i = 0; i < maxn; i++) {
    fa[i] = fb[i] = i;
  }
}
set<int> S[2][maxn];
int ans[maxn];
int G[maxn];
bool vis[maxn];
map<pair<int, int>, int> M;
int main() {
#ifdef local
  freopen("in", "r", stdin);
#endif
  init();
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    u = find(fa, u);
    v = find(fa, v);
    fa[u] = v;
  }
  for (int i = 0; i < l; i++) {
    int u, v;
    cin >> u >> v;
    u = find(fb, u);
    v = find(fb, v);
    fb[u] = v;
  }
  for (int i = 1; i <= n; i++) {
    S[0][find(fa, i)].insert(i);
    S[1][find(fb, i)].insert(i);
  }
  for (int i = 1; i <= n; i++) {
    if (M[MP(fa[i], fb[i])]) {
      ans[i] = M[MP(fa[i], fb[i])];
      continue;
    }
    int sz = S[0][fa[i]].size();
    int bz = S[1][fb[i]].size();
    if (sz < bz)
      for (int it : S[0][fa[i]]) {
        if (S[1][fb[i]].count(it))
          ans[i]++;
      }
    else
      for (int it : S[1][fb[i]]) {
        if (S[0][fa[i]].count(it))
          ans[i]++;
      }
    M[MP(fa[i], fb[i])] = ans[i];
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
}