Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

class UnionFind {
public:
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }

  bool unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return false; // xとyの根が同じ(=同じ木にある)時はそのまま
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size(int x) { return -d[root(x)]; }
};

signed _main() {
  int N, M, K;
  cin >> N >> M >> K;
  v<int> deg(N, 0);
  UnionFind Friend(N);
  v<int> block(N, 0);
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Friend.unite(a, b);
    deg[a]++;
    deg[b]++;
  }
  rep(i, 0, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (Friend.same(a, b)) {
      block[a]++;
      block[b]++;
    }
  }
  rep(i, 0, N) { cout << Friend.size(i) - deg[i] - 1 - block[i] << endl; }
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

class UnionFind {
public:
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }

  bool unite(int x, int y) { // xとyの木を併合
    x = root(x);             // xの根をrx
    y = root(y);             // yの根をry
    if (x == y)
      return false; // xとyの根が同じ(=同じ木にある)時はそのまま
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size(int x) { return -d[root(x)]; }
};

signed _main() {
  int N, M, K;
  cin >> N >> M >> K;
  v<int> deg(N, 0);
  UnionFind Friend(N);
  v<int> block(N, 0);
  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Friend.unite(a, b);
    deg[a]++;
    deg[b]++;
  }
  rep(i, 0, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (Friend.same(a, b)) {
      block[a]++;
      block[b]++;
    }
  }
  rep(i, 0, N) { cout << Friend.size(i) - deg[i] - 1 - block[i] << endl; }
  return 0;
}