Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <set>
#include <vector>

struct Person {
  unsigned int index;
  unsigned int candidates;
  std::set<int> friends, blocked;
  bool operator<(const Person &other) const { return index < other.index; }
};

typedef std::vector<Person> Graph;
typedef std::vector<std::set<int>> Group;
const int N = (int)(1e5 + 10);

void bfs(const Graph &g, bool *visited, std::set<int> &group, int start) {
  std::queue<int> q;
  q.emplace(start);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (visited[cur]) {
      continue;
    }
    visited[cur] = true;
    group.emplace(cur);
    for (int person : g[cur].friends) {
      if (!visited[person]) {
        q.emplace(person);
      }
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

#if DEBUG_ON
  freopen("input", "r", stdin);
#endif

  int n, m, k;
  std::cin >> n >> m >> k;
  Graph g(n + 1);
  for (int i = 0; i < n; ++i) {
    g[i].index = i;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    g[u].friends.emplace(v);
    g[v].friends.emplace(u);
  }
  for (int i = 0; i < k; ++i) {
    int u, v;
    std::cin >> u >> v;
    g[u].blocked.emplace(v);
    g[v].blocked.emplace(u);
  }
  Group groups(m);
  int groupIndex = 0;
  bool visited[N] = {false};
  for (int i = 1; i < n; ++i) {
    if (!visited[i]) {
      bfs(g, visited, groups[groupIndex++], i);
    }
  }
  for (int i = 0; i < groupIndex; ++i) {
    for (int member : groups[i]) {
      g[member].candidates = groups[i].size() - (1 + g[member].friends.size());
      for (int blocked : g[member].blocked) {
        if (groups[i].count(blocked) && !g[member].friends.count(blocked)) {
          --g[member].candidates;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    std::cout << g[i].candidates << ' ';
  }
  std::cout << '\n';
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <set>
#include <vector>

struct Person {
  unsigned int index;
  unsigned int candidates;
  std::set<int> friends, blocked;
  bool operator<(const Person &other) const { return index < other.index; }
};

typedef std::vector<Person> Graph;
typedef std::vector<std::set<int>> Group;
const int N = (int)(1e5 + 10);

void bfs(const Graph &g, bool *visited, std::set<int> &group, int start) {
  std::queue<int> q;
  q.emplace(start);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (visited[cur]) {
      continue;
    }
    visited[cur] = true;
    group.emplace(cur);
    for (int person : g[cur].friends) {
      if (!visited[person]) {
        q.emplace(person);
      }
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

#if DEBUG_ON
  freopen("input", "r", stdin);
#endif

  int n, m, k;
  std::cin >> n >> m >> k;
  Graph g(n + 1);
  for (int i = 0; i < n; ++i) {
    g[i].index = i;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    g[u].friends.emplace(v);
    g[v].friends.emplace(u);
  }
  for (int i = 0; i < k; ++i) {
    int u, v;
    std::cin >> u >> v;
    g[u].blocked.emplace(v);
    g[v].blocked.emplace(u);
  }
  Group groups(n + 1);
  int groupIndex = 0;
  bool visited[N] = {false};
  for (int i = 1; i < n; ++i) {
    if (!visited[i]) {
      bfs(g, visited, groups[groupIndex++], i);
    }
  }
  for (int i = 0; i < groupIndex; ++i) {
    for (int member : groups[i]) {
      g[member].candidates = groups[i].size() - (1 + g[member].friends.size());
      for (int blocked : g[member].blocked) {
        if (groups[i].count(blocked) && !g[member].friends.count(blocked)) {
          --g[member].candidates;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    std::cout << g[i].candidates << ' ';
  }
  std::cout << '\n';
  return 0;
}