Problem p02762 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

struct UnionFind {
  vi par, count;

  UnionFind(int N) : par(N), count(N, 1) {
    for (int i = 0; i < N; i++)
      par.at(i) = i;
  }

  int root(int x) {
    if (par.at(x) == x)
      return x;
    return par.at(x) = root(par.at(x));
  }

  int unite(int x, int y) {
    if (root(x) == root(y))
      return 0;
    if (size(x) < size(y)) {
      count.at(root(y)) += count.at(root(x));
      par.at(root(x)) = root(y);
    } else {
      count.at(root(x)) += count.at(root(y));
      par.at(root(y)) = root(x);
    }
    return 1;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int size(int x) { return count.at(root(x)); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M, K;
  cin >> N >> M >> K;
  UnionFind uf(N);

  vll ans(N, 0LL);

  for (int i = 0; i < M; i++) {
    ll A, B;
    cin >> A >> B;
    A--;
    B--;
    uf.unite(A, B);
    ans.at(A)--;
    ans.at(B)--;
  }

  for (int i = 0; i < N; i++) {
    ans.at(i) += uf.size(i) - 1;
  }

  vector<vector<bool>> blo(N, vector<bool>(N, false));
  for (int i = 0; i < K; i++) {
    ll C, D;
    cin >> C >> D;
    C--;
    D--;
    if (uf.same(C, D)) {
      ans.at(C)--;
      ans.at(D)--;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans.at(i);
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

struct UnionFind {
  vi par, count;

  UnionFind(int N) : par(N), count(N, 1) {
    for (int i = 0; i < N; i++)
      par.at(i) = i;
  }

  int root(int x) {
    if (par.at(x) == x)
      return x;
    return par.at(x) = root(par.at(x));
  }

  int unite(int x, int y) {
    if (root(x) == root(y))
      return 0;
    if (size(x) < size(y)) {
      count.at(root(y)) += count.at(root(x));
      par.at(root(x)) = root(y);
    } else {
      count.at(root(x)) += count.at(root(y));
      par.at(root(y)) = root(x);
    }
    return 1;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int size(int x) { return count.at(root(x)); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M, K;
  cin >> N >> M >> K;
  UnionFind uf(N);

  vll ans(N, 0LL);

  for (int i = 0; i < M; i++) {
    ll A, B;
    cin >> A >> B;
    A--;
    B--;
    uf.unite(A, B);
    ans.at(A)--;
    ans.at(B)--;
  }

  for (int i = 0; i < N; i++) {
    ans.at(i) += uf.size(i) - 1;
  }

  for (int i = 0; i < K; i++) {
    ll C, D;
    cin >> C >> D;
    C--;
    D--;
    if (uf.same(C, D)) {
      ans.at(C)--;
      ans.at(D)--;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans.at(i);
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}