Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <list>
#include <unordered_map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9

using namespace std;
typedef long long ll;

// class to represent a disjoint set
class DisjointSet {
  unordered_map<int, int> parent;

public:
  unordered_map<int, int> size;

  // perform MakeSet operation
  void makeSet(vector<int> const &universe) {
    // create n disjoint sets (one for each item)
    for (int i : universe) {
      parent[i] = i;
      size[i] = 1;
    }
  }

  // Find the root of the set in which element k belongs
  int Find(int k) {
    if (parent[k] == k)
      return k;             // if k is root
    return Find(parent[k]); // recur for parent until we find root
  }

  // Perform Union of two subsets
  void Union(int a, int b) {
    // find root of the sets in which elements x and y belongs
    int x = Find(a);
    int y = Find(b);
    parent[x] = y;
    // increment size when union happens
    if (x != y)
      size[y] += size[x];
  }

  // Get number of elements in the set which a belongs to
  int getSize(int a) {
    int x = Find(a);
    return size[x];
  }
};

int main() {
  int n, m, k, a, b, c, d;
  cin >> n >> m >> k;

  DisjointSet ds;
  vector<int> recs;
  REP(i, n) recs.push_back(i + 1);
  ds.makeSet(recs);

  vector<int> f(n, 0);
  map<int, vector<int>> bl;

  REP(j, m) {
    cin >> c >> d;
    ds.Union(c, d);
    // fb[c].push_back(d);
    // fb[d].push_back(c);
    f[c - 1]++;
    f[d - 1]++;
  }

  REP(i, k) {
    cin >> a >> b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }

  // vector<int> sizee(n, 0);
  // REP(i, n) sizee[i] = ds.getSize(i+1);

  int ans;
  REP(i, n) {
    ans = ds.size[ds.Find(i + 1)] - 1;
    ans -= f[i];
    for (auto x : bl[i + 1]) {
      if (ds.Find(x) == ds.Find(i + 1)) {
        ans--;
      }
    }
    cout << ans << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <list>
#include <unordered_map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9

using namespace std;
typedef long long ll;

// class to represent a disjoint set
class DisjointSet {
  unordered_map<int, int> parent;

public:
  unordered_map<int, int> size;

  // perform MakeSet operation
  void makeSet(vector<int> const &universe) {
    // create n disjoint sets (one for each item)
    for (int i : universe) {
      parent[i] = i;
      size[i] = 1;
    }
  }

  // Find the root of the set in which element k belongs
  int Find(int k) {
    if (parent[k] == k)
      return k;                         // if k is root
    return parent[k] = Find(parent[k]); // recur for parent until we find root
  }

  // Perform Union of two subsets
  void Union(int a, int b) {
    // find root of the sets in which elements x and y belongs
    int x = Find(a);
    int y = Find(b);
    parent[x] = y;
    // increment size when union happens
    if (x != y)
      size[y] += size[x];
  }

  // Get number of elements in the set which a belongs to
  int getSize(int a) {
    int x = Find(a);
    return size[x];
  }
};

int main() {
  int n, m, k, a, b, c, d;
  cin >> n >> m >> k;

  DisjointSet ds;
  vector<int> recs;
  REP(i, n) recs.push_back(i + 1);
  ds.makeSet(recs);

  vector<int> f(n, 0);
  map<int, vector<int>> bl;

  REP(j, m) {
    cin >> c >> d;
    ds.Union(c, d);
    // fb[c].push_back(d);
    // fb[d].push_back(c);
    f[c - 1]++;
    f[d - 1]++;
  }

  REP(i, k) {
    cin >> a >> b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }

  // vector<int> sizee(n, 0);
  // REP(i, n) sizee[i] = ds.getSize(i+1);

  int ans;
  REP(i, n) {
    ans = ds.size[ds.Find(i + 1)] - 1;
    ans -= f[i];
    for (auto x : bl[i + 1]) {
      if (ds.Find(x) == ds.Find(i + 1)) {
        ans--;
      }
    }
    cout << ans << " ";
  }
  cout << endl;
}