Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpl;

class union_find {
private:
  int p[200000];
  int r[200000];

public:
  union_find(int n) {
    for (int i = 0; i < n; i++) {
      p[i] = i;
      r[i] = 0;
    }
  }
  int find(int x) {
    if (p[x] == x) {
      return x;
    } else {
      return p[x] = find(p[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (r[x] < r[y]) {
      p[x] = y;
    } else {
      p[y] = x;
      if (r[x] == r[y])
        r[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, l;
  cin >> n >> k >> l;
  union_find road(n);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    road.unite(p - 1, q - 1);
  }
  union_find line(n);
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    line.unite(r - 1, s - 1);
  }

  vpi id;
  rep(i, n) { id.pb(mp(road.find(i), line.find(i))); }
  vi ans(n);
  rep(i, n) {
    for (int j = i; j < n; j++) {
      if (id[i].first == id[j].first && id[i].second == id[j].second) {
        ans[i]++;
        ans[j]++;
      }
    }
  }
  rep(i, n) cout << ans[i] - 1 << " ";
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpl;

class union_find {
private:
  int p[200000];
  int r[200000];

public:
  union_find(int n) {
    for (int i = 0; i < n; i++) {
      p[i] = i;
      r[i] = 0;
    }
  }
  int find(int x) {
    if (p[x] == x) {
      return x;
    } else {
      return p[x] = find(p[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (r[x] < r[y]) {
      p[x] = y;
    } else {
      p[y] = x;
      if (r[x] == r[y])
        r[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, l;
  cin >> n >> k >> l;
  union_find road(n);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    road.unite(p - 1, q - 1);
  }
  union_find line(n);
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    line.unite(r - 1, s - 1);
  }

  map<int, map<int, int>> mp;
  rep(i, n) { mp[road.find(i)][line.find(i)]++; }

  rep(i, n) cout << mp[road.find(i)][line.find(i)] << " ";
  cout << endl;

  return 0;
}