Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

class UF {
public:
  int par[200000];
  int r[200000];
  UF(int N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
    }
  }

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

    return par[x] = find(par[x]);
  }

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

    if (r[x] < r[y])
      par[x] = y;
    else {
      par[y] = x;
      if (r[x] == r[y])
        r[x]++;
    }
  }
};

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  vector<vector<int>> md(N);
  vector<vector<int>> mt(N);

  vector<pair<int, int>> dP(K), tP(L);

  for (int i = 0; i < K; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    dP[i] = pair<int, int>(min(x, y), max(x, y));
  }

  for (int i = 0; i < L; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    tP[i] = pair<int, int>(min(x, y), max(x, y));
  }

  UF duf(N), tuf(N);

  for (int i = 0; i < K; i++) {
    duf.add(dP[i].first, dP[i].second);
  }

  for (int i = 0; i < L; i++) {
    tuf.add(tP[i].first, tP[i].second);
  }

  UF uf(N);
  for (int i = 0; i < K; i++) {
    if (tuf.find(dP[i].first) == tuf.find(dP[i].second)) {
      uf.add(dP[i].first, dP[i].second);
    }
  }
  for (int i = 0; i < L; i++) {
    if (duf.find(tP[i].first) == duf.find(tP[i].second)) {
      uf.add(tP[i].first, tP[i].second);
    }
  }

  vector<vector<int>> tmp(N);
  for (int i = 0; i < N; i++) {
    tmp[duf.find(i)].push_back(i);
  }

  vector<int> pcount(N);
  for (int i = 0; i < N; i++) {
    vector<int> count(N);
    for (int j = 0; j < tmp[i].size(); j++) {
      count[tuf.find(tmp[i][j])]++;
    }

    for (int j = 0; j < tmp[i].size(); j++) {
      pcount[tmp[i][j]] += count[tuf.find(tmp[i][j])];
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cout << pcount[i] << " ";
  }

  cout << pcount[N - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

class UF {
public:
  int par[200000];
  int r[200000];
  UF(int N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
    }
  }

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

    return par[x] = find(par[x]);
  }

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

    if (r[x] < r[y])
      par[x] = y;
    else {
      par[y] = x;
      if (r[x] == r[y])
        r[x]++;
    }
  }
};

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  vector<vector<int>> md(N);
  vector<vector<int>> mt(N);

  vector<pair<int, int>> dP(K), tP(L);

  for (int i = 0; i < K; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    dP[i] = pair<int, int>(min(x, y), max(x, y));
  }

  for (int i = 0; i < L; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    tP[i] = pair<int, int>(min(x, y), max(x, y));
  }

  UF duf(N), tuf(N);

  for (int i = 0; i < K; i++) {
    duf.add(dP[i].first, dP[i].second);
  }

  for (int i = 0; i < L; i++) {
    tuf.add(tP[i].first, tP[i].second);
  }

  UF uf(N);
  for (int i = 0; i < K; i++) {
    if (tuf.find(dP[i].first) == tuf.find(dP[i].second)) {
      uf.add(dP[i].first, dP[i].second);
    }
  }
  for (int i = 0; i < L; i++) {
    if (duf.find(tP[i].first) == duf.find(tP[i].second)) {
      uf.add(tP[i].first, tP[i].second);
    }
  }

  vector<vector<int>> tmp(N);
  for (int i = 0; i < N; i++) {
    tmp[duf.find(i)].push_back(i);
  }

  vector<int> pcount(N);
  for (int i = 0; i < N; i++) {
    map<int, int> count;
    for (int j = 0; j < tmp[i].size(); j++) {
      count[tuf.find(tmp[i][j])]++;
    }

    for (int j = 0; j < tmp[i].size(); j++) {
      pcount[tmp[i][j]] += count[tuf.find(tmp[i][j])];
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cout << pcount[i] << " ";
  }

  cout << pcount[N - 1] << endl;

  return 0;
}