Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
// now

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int Find(int x) {
    if (d[x] < 0)
      return x;
    return Find(d[x]);
  }
  bool unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (d[x] < d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { return Find(x) == Find(y); }
  int size(int x) { return (-d[Find(x)]); }
};

int deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);

  REP(i, m) {
    int a, b;
    cin >> a >> b;

    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }

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

  REP(i, n) {
    int ans = uf.size(i) - 1 - deg[i];

    for (int x : to[i]) {
      if (uf.same(i, x))
        ans--;
    }
    cout << ans << " ";
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
// now

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int Find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = Find(d[x]);
  }
  bool unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (d[x] < d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { return Find(x) == Find(y); }
  int size(int x) { return (-d[Find(x)]); }
};

int deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);

  REP(i, m) {
    int a, b;
    cin >> a >> b;

    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }

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

  REP(i, n) {
    int ans = uf.size(i) - 1 - deg[i];

    for (int x : to[i]) {
      if (uf.same(i, x))
        ans--;
    }
    cout << ans << " ";
  }
  cout << endl;
}