Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
;
#define MEM(a, b) memset((a), (b), sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
class UnionFind {
public:
  int p[N];
  void init(int n = N) { memset(p, -1, sizeof(int) * n); }
  int treesize(int x) { return -p[Find(x)]; }
  int Find(int x) {
    int s = x;
    while (p[s] >= 0)
      s = p[s];
    while (x != s) {
      int t = p[x];
      p[x] = s;
      x = t;
    }
    return s;
  }
  void Union(int x, int y) {
    int px = Find(x);
    int py = Find(y);
    if (p[px] > p[py])
      swap(px, py);
    p[px] += p[py];
    p[py] = px;
  }
} uf1, uf2;
vector<int> v1[N], v2[N];
int ans[N];
int getans(vector<int> &v1, vector<int> &v2) {
  int ans = 0;
  for (auto &x : v1) {
    if (upper_bound(v2.begin(), v2.end(), x) -
        lower_bound(v2.begin(), v2.end(), x))
      ans++;
  }
  return ans;
}
int check(vector<int> &v1, vector<int> &v2) {
  if (v1.size() > v2.size())
    return getans(v2, v1);
  else
    return getans(v1, v2);
}
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  uf1.init();
  uf2.init();
  int n, m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  vector<pair<int, int>> vp;
  while (m1--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (uf1.Find(x) != uf1.Find(y))
      uf1.Union(x, y);
  }
  while (m2--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (uf2.Find(x) != uf2.Find(y))
      uf2.Union(x, y);
  }
  for (int i = 1; i <= n; i++) {
    v1[uf1.Find(i)].push_back(i);
    v2[uf2.Find(i)].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    map<int, int> mc;
    for (auto &x : v1[i]) {
      int y = uf2.Find(x);
      if (!mc.count(y))
        ans[x] = check(v1[i], v2[uf2.Find(x)]);
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d%c", ans[i], " \n"[i == n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
;
#define MEM(a, b) memset((a), (b), sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
class UnionFind {
public:
  int p[N];
  void init(int n = N) { memset(p, -1, sizeof(int) * n); }
  int treesize(int x) { return -p[Find(x)]; }
  int Find(int x) {
    int s = x;
    while (p[s] >= 0)
      s = p[s];
    while (x != s) {
      int t = p[x];
      p[x] = s;
      x = t;
    }
    return s;
  }
  void Union(int x, int y) {
    int px = Find(x);
    int py = Find(y);
    if (p[px] > p[py])
      swap(px, py);
    p[px] += p[py];
    p[py] = px;
  }
} uf1, uf2;
vector<int> v1[N], v2[N];
int ans[N];
int getans(vector<int> &v1, vector<int> &v2) {
  int ans = 0;
  for (auto &x : v1) {
    if (upper_bound(v2.begin(), v2.end(), x) -
        lower_bound(v2.begin(), v2.end(), x))
      ans++;
  }
  return ans;
}
int check(vector<int> &v1, vector<int> &v2) {
  if (v1.size() > v2.size())
    return getans(v2, v1);
  else
    return getans(v1, v2);
}
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  uf1.init();
  uf2.init();
  int n, m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  vector<pair<int, int>> vp;
  while (m1--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (uf1.Find(x) != uf1.Find(y))
      uf1.Union(x, y);
  }
  while (m2--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (uf2.Find(x) != uf2.Find(y))
      uf2.Union(x, y);
  }
  for (int i = 1; i <= n; i++) {
    v1[uf1.Find(i)].push_back(i);
    v2[uf2.Find(i)].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    map<int, int> mc;
    for (auto &x : v1[i]) {
      int y = uf2.Find(x);
      if (!mc.count(y))
        mc[y] = check(v1[i], v2[uf2.Find(x)]);
      ans[x] = mc[y];
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d%c", ans[i], " \n"[i == n]);
  return 0;
}