Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define mfill(x, y) memset(x, y, sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w)
#define y0 y12345
#define y1 y54321

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#endif

using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> T gcd(T &a, T &b) {
  if (a < b) {
    swap(a, b);
  }
  T r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};

ll n, m, k;
vvint a, b;
vint d;
int namer = 0;
vvint mem;
int dfs(int par) {
  d[par] = namer;
  mem[namer].push_back(par);
  int ret = 1;
  rep(i, a[par].size()) {
    if (d[a[par][i]] == -1) {
      ret += dfs(a[par][i]);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  debug("debug test\n");
  cin >> n >> m >> k;
  a = vector<vint>(n);
  b = vector<vint>(n);
  d = vint(n, -1);
  rep(i, m) {
    int p, q;
    cin >> p >> q;
    a[p - 1].push_back(q - 1);
    a[q - 1].push_back(p - 1);
  }

  rep(i, k) {
    int p, q;
    cin >> p >> q;
    b[p - 1].push_back(q - 1);
    b[q - 1].push_back(p - 1);
  }
  // sort();
  vint nl;
  rep(i, n) {
    if (d[i] == -1) {
      mem.push_back(vint());
      nl.push_back(dfs(i));
      sort(all(mem[namer]));
      namer++;
    }
  }
  rep(i, namer) {
    debug("i: %d\n", i);
    rep(j, nl[i]) { debug("%d %d\n", j, mem[i][j]); }
  }
  debug("\n");
  rep(i, n) {
    if (i != 0)
      cout << ' ';
    int bcou = 0;
    rep(j, b[i].size()) {
      if (binary_search(all(mem[d[i]]), b[i][j])) {
        bcou++;
      }
    }
    cout << nl[d[i]] - a[i].size() - 1 - bcou;
  }
  cout << endl;

  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define mfill(x, y) memset(x, y, sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w)
#define y0 y12345
#define y1 y54321

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#endif

using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> T gcd(T &a, T &b) {
  if (a < b) {
    swap(a, b);
  }
  T r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};

ll n, m, k;
vvint a, b;
vint d;
int namer = 0;
vvint mem;
int dfs(int par) {
  d[par] = namer;
  mem[namer].push_back(par);
  int ret = 1;
  rep(i, a[par].size()) {
    if (d[a[par][i]] == -1) {
      ret += dfs(a[par][i]);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  debug("debug test\n");
  cin >> n >> m >> k;
  a = vector<vint>(n);
  b = vector<vint>(n);
  d = vint(n, -1);
  rep(i, m) {
    int p, q;
    cin >> p >> q;
    a[p - 1].push_back(q - 1);
    a[q - 1].push_back(p - 1);
  }

  rep(i, k) {
    int p, q;
    cin >> p >> q;
    b[p - 1].push_back(q - 1);
    b[q - 1].push_back(p - 1);
  }
  // sort();
  vint nl;
  rep(i, n) {
    if (d[i] == -1) {
      mem.push_back(vint());
      nl.push_back(dfs(i));
      sort(all(mem[namer]));
      namer++;
    }
  }
  rep(i, namer) {
    debug("i: %d\n", i);
    rep(j, nl[i]) { debug("%d %d\n", j, mem[i][j]); }
  }
  debug("\n");
  rep(i, n) {
    if (i != 0)
      cout << ' ';
    int bcou = 0;
    rep(j, b[i].size()) {
      if (d[i] == d[b[i][j]]) {
        // if(binary_search(all(mem[d[i]]), b[i][j])){
        bcou++;
      }
    }
    cout << nl[d[i]] - a[i].size() - 1 - bcou;
  }
  cout << endl;

  return 0;
}