Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  vector<int> a(m), b(m), c(m), d(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    uf.unite(a[i], b[i]);
  }
  rep(i, k) {
    cin >> c[i] >> d[i];
    c[i]--;
    d[i]--;
  }
  vector<int> cntfr(n), cntbl(n);
  rep(i, m) {
    cntfr[a[i]]++;
    cntfr[b[i]]++;
  }
  rep(i, k) {
    if (uf.find(c[i], d[i])) {
      cntbl[c[i]]++;
      cntbl[d[i]]++;
    }
  }
  rep(i, n) {
    cout << uf.size(i) - 1 - cntfr[i] - cntbl[i] << " \n"[i == n - 1];
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  vector<int> a(m), b(m), c(k), d(k);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    uf.unite(a[i], b[i]);
  }
  rep(i, k) {
    cin >> c[i] >> d[i];
    c[i]--;
    d[i]--;
  }
  vector<int> cntfr(n), cntbl(n);
  rep(i, m) {
    cntfr[a[i]]++;
    cntfr[b[i]]++;
  }
  rep(i, k) {
    if (uf.find(c[i], d[i])) {
      cntbl[c[i]]++;
      cntbl[d[i]]++;
    }
  }
  rep(i, n) {
    cout << uf.size(i) - 1 - cntfr[i] - cntbl[i] << " \n"[i == n - 1];
  }
}