Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "math.h"
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ifor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long double ld;
typedef long long int lli;
const double eps = 1e-11;
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, 1, 0, -1};
lli MOD = 1000000007;
#define UF_MAX 200005
struct UFind {
  int par[UF_MAX];
  int rank[UF_MAX];
  int count[UF_MAX];
  UFind(int n) {
    rep(i, n) {
      par[i] = i;
      rank[i] = 0;
      count[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
      count[y] += count[x];
    } else {
      par[y] = x;
      count[x] += count[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UFind r(n), t(n), B(n);
  int a, b;
  rep(i, k) {
    cin >> a >> b;
    a--, b--;
    r.unite(a, b);
  }
  rep(i, l) {
    cin >> a >> b;
    a--, b--;
    t.unite(a, b);
  }
  map<int, set<int>> sr;
  map<int, set<int>> sb;
  rep(i, n) {
    sr[r.find(i)].insert(i);
    sb[t.find(i)].insert(i);
  }
  bool used[200005] = {};
  int anser[200005] = {};
  rep(i, n) {
    if (used[i])
      continue;
    set<int> ans;
    for (auto s : sr[r.find(i)]) {
      if (sb[t.find(i)].find(s) != sb[t.find(i)].end()) {
        ans.insert(s);
        used[s] = true;
      }
    }
    for (auto s : ans) {
      anser[s] = ans.size();
    }
  }
  rep(i, n) { cout << anser[i] << ' '; }
  cout << endl;
}


Fixed submission:
#include "math.h"
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ifor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long double ld;
typedef long long int lli;
const double eps = 1e-11;
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, 1, 0, -1};
lli MOD = 1000000007;
#define UF_MAX 200005
struct UFind {
  int par[UF_MAX];
  int rank[UF_MAX];
  int count[UF_MAX];
  UFind(int n) {
    rep(i, n) {
      par[i] = i;
      rank[i] = 0;
      count[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
      count[y] += count[x];
    } else {
      par[y] = x;
      count[x] += count[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UFind r(n), t(n), B(n);
  int a, b;
  rep(i, k) {
    cin >> a >> b;
    a--, b--;
    r.unite(a, b);
  }
  rep(i, l) {
    cin >> a >> b;
    a--, b--;
    t.unite(a, b);
  }
  map<int, set<int>> sr;
  map<int, set<int>> sb;
  rep(i, n) {
    sr[r.find(i)].insert(i);
    sb[t.find(i)].insert(i);
  }
  bool used[200005] = {};
  int anser[200005] = {};
  rep(i, n) {
    if (used[i])
      continue;
    set<int> ans;
    if (sr[r.find(i)].size() < sb[t.find(i)].size()) {
      for (auto s : sr[r.find(i)]) {
        if (sb[t.find(i)].find(s) != sb[t.find(i)].end()) {
          ans.insert(s);
          used[s] = true;
        }
      }
    } else {
      for (auto s : sb[t.find(i)]) {
        if (sr[r.find(i)].find(s) != sr[r.find(i)].end()) {
          ans.insert(s);
          used[s] = true;
        }
      }
    }
    for (auto s : ans) {
      anser[s] = ans.size();
    }
  }
  rep(i, n) { cout << anser[i] << ' '; }
  cout << endl;
}