Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

class UnionFind {
public:
  UnionFind(int n) { par.resize(n); }

  void init() {
    for (int i = 0, n = par.size(); i < n; ++i) {
      par[i] = i;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    par[x] = y;
  }

private:
  vector<int> par;
};

int main() {
  int N;
  cin >> N;
  UnionFind uf1(N + 1), uf2(N + 1);
  uf1.init();
  uf2.init();
  int K, L;
  cin >> K >> L;
  int s, d;
  REP(i, K) {
    cin >> s >> d;
    uf1.unite(s, d);
  }
  REP(i, L) {
    cin >> s >> d;
    uf2.unite(s, d);
  }
  REP(i, N) {
    int count = 0;
    REP(j, N) {
      if (uf1.same(i + 1, j + 1) && uf2.same(i + 1, j + 1)) {
        ++count;
      }
    }
    cout << count << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

class UnionFind {
public:
  UnionFind(int n) { par.resize(n); }

  void init() {
    for (int i = 0, n = par.size(); i < n; ++i) {
      par[i] = i;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    par[x] = y;
  }

private:
  vector<int> par;
};

int main() {
  int N;
  cin >> N;
  UnionFind uf1(N + 1), uf2(N + 1);
  uf1.init();
  uf2.init();
  int K, L;
  cin >> K >> L;
  int s, d;
  REP(i, K) {
    cin >> s >> d;
    uf1.unite(s, d);
  }
  REP(i, L) {
    cin >> s >> d;
    uf2.unite(s, d);
  }
  map<pair<int, int>, int> M;
  REP(i, N) { ++M[make_pair(uf1.find(i + 1), uf2.find(i + 1))]; }
  REP(i, N) { cout << M[make_pair(uf1.find(i + 1), uf2.find(i + 1))] << " "; }
  cout << endl;
  return 0;
}