Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;

struct p {
  int a;
  int b;
};

// int friendList[10001][10001];
// int blockList[10001][10001];

using Graph = vector<vector<int>>;

vector<bool> seen;

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

  int root(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return root(d[x]);
    }
  }

  bool unite(int x, int y) {

    x = root(x);
    y = root(y);

    if (x == y) {
      return false;
    } else {
      if (size(x) < size(y)) {
        swap(x, y);
      }
      d[y] += d[x];
      d[x] = y;
      return true;
    }
  }

  bool same(int x, int y) {
    if (root(x) == root(y)) {
      return true;
    } else {
      return false;
    }
  }

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

void dfs(const Graph &g, int v, UnionFind *uf, int root) {
  seen[v] = true;

  for (auto next_v : g[v]) {
    if (seen[next_v])
      continue;
    uf->unite(root, next_v);
    dfs(g, next_v, uf, root);
  }
}

int main() {

  lll ii, jj, kk;
  vector<int> ret;

  int n, m, k;

  cin >> n >> m >> k;

  vector<struct p> friends(m);
  vector<struct p> block(k);

  set<int> friendSet[n + 1];
  set<int> blockSet[n + 1];

  Graph g(n + 1);
  seen.resize(n + 1);

  int a, b, c, d;

  // フレンドリスト
  for (ii = 0; ii < m; ii++) {
    cin >> a >> b;
    struct p pp;

    pp.a = a;
    pp.b = b;

    friends.push_back(pp);

    friendSet[a].insert(b);
    friendSet[b].insert(a);
    g[a].push_back(b);
    g[b].push_back(a);
    // cout <<"a:" << a << "b" << b << endl;
  }

  struct p p1;

  // 拒否リスト
  for (ii = 0; ii < k; ii++) {
    cin >> c >> d;
    // pp = (p*)malloc(sizeof(struct p));
    p1.a = c;
    p1.b = d;
    // cout <<"c:" << c << "d" << d << endl;
    block[ii].a = c;
    block[ii].b = d;
    // block.push_back(p1);

    blockSet[c].insert(d);
    blockSet[d].insert(c);
  }

  // for(ii=0;ii<block.size();ii++){
  //   cout <<"a:" << block[ii].a << "b" << block[ii].b << endl;
  // }

  // cout << "serach Friend!" << endl;

  vector<set<int>> comb;
  int combNum = 0;

  UnionFind uf(n);
  int root;

  for (ii = 1; ii <= n; ii++) {

    if (!seen[ii]) {

      set<int> s;
      root = ii;
      dfs(g, ii, &uf, root);
      comb.push_back(s);
      combNum++;
    }
  }

  // cout << combNum << endl;

  int fc[n + 1];
  for (ii = 1; ii <= n; ii++) {
    // cout << "ii:" << ii << " " << uf.size(n) << " " << endl;
    fc[ii] = uf.size(ii) - friendSet[ii].size() - 1;
  }

  for (ii = 0; ii < k; ii++) {
    // cout << "size:" << block.size() << endl;

    a = block[ii].a;
    b = block[ii].b;

    // cout <<"a:" << a << "b" << b << endl;
    if (uf.same(a, b)) {
      fc[a]--;
      fc[b]--;
    }
  }

  for (ii = 1; ii <= n; ii++) {
    cout << fc[ii] << " ";
  }
  //  for(ii=1;ii<=n;ii++){
  //    for(jj=1;jj<=n;jj++){
  //      if(
  //    }
  //  }

  //
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;

struct p {
  int a;
  int b;
};

// int friendList[10001][10001];
// int blockList[10001][10001];

using Graph = vector<vector<int>>;

vector<bool> seen;

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

  int root(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return root(d[x]);
    }
  }

  bool unite(int x, int y) {

    x = root(x);
    y = root(y);

    if (x == y) {
      return false;
    } else {
      if (size(x) > size(y)) {
        swap(x, y);
      }
      d[y] += d[x];
      d[x] = y;
      return true;
    }
  }

  bool same(int x, int y) {
    if (root(x) == root(y)) {
      return true;
    } else {
      return false;
    }
  }

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

void dfs(const Graph &g, int v, UnionFind *uf, int root) {
  seen[v] = true;

  for (auto next_v : g[v]) {
    if (seen[next_v])
      continue;
    uf->unite(root, next_v);
    dfs(g, next_v, uf, root);
  }
}

int main() {

  lll ii, jj, kk;
  vector<int> ret;

  int n, m, k;

  cin >> n >> m >> k;

  vector<struct p> friends(m);
  vector<struct p> block(k);

  set<int> friendSet[n + 1];
  set<int> blockSet[n + 1];

  Graph g(n + 1);
  seen.resize(n + 1);

  int a, b, c, d;

  // フレンドリスト
  for (ii = 0; ii < m; ii++) {
    cin >> a >> b;
    struct p pp;

    pp.a = a;
    pp.b = b;

    friends.push_back(pp);

    friendSet[a].insert(b);
    friendSet[b].insert(a);
    g[a].push_back(b);
    g[b].push_back(a);
    // cout <<"a:" << a << "b" << b << endl;
  }

  struct p p1;

  // 拒否リスト
  for (ii = 0; ii < k; ii++) {
    cin >> c >> d;
    // pp = (p*)malloc(sizeof(struct p));
    p1.a = c;
    p1.b = d;
    // cout <<"c:" << c << "d" << d << endl;
    block[ii].a = c;
    block[ii].b = d;
    // block.push_back(p1);

    blockSet[c].insert(d);
    blockSet[d].insert(c);
  }

  // for(ii=0;ii<block.size();ii++){
  //   cout <<"a:" << block[ii].a << "b" << block[ii].b << endl;
  // }

  // cout << "serach Friend!" << endl;

  vector<set<int>> comb;
  int combNum = 0;

  UnionFind uf(n);
  int root;

  for (ii = 1; ii <= n; ii++) {

    if (!seen[ii]) {

      set<int> s;
      root = ii;
      dfs(g, ii, &uf, root);
      comb.push_back(s);
      combNum++;
    }
  }

  // cout << combNum << endl;

  int fc[n + 1];
  for (ii = 1; ii <= n; ii++) {
    // cout << "ii:" << ii << " " << uf.size(n) << " " << endl;
    fc[ii] = uf.size(ii) - friendSet[ii].size() - 1;
  }

  for (ii = 0; ii < k; ii++) {
    // cout << "size:" << block.size() << endl;

    a = block[ii].a;
    b = block[ii].b;

    // cout <<"a:" << a << "b" << b << endl;
    if (uf.same(a, b)) {
      fc[a]--;
      fc[b]--;
    }
  }

  for (ii = 1; ii <= n; ii++) {
    cout << fc[ii] << " ";
  }
  //  for(ii=1;ii<=n;ii++){
  //    for(jj=1;jj<=n;jj++){
  //      if(
  //    }
  //  }

  //
  return 0;
}