Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

struct Graph {
  std::size_t sz;
  std::vector<std::set<int>> elist;
  // set avoids duplicates

  Graph(int n) : sz(n), elist(n) {}
  void add_edge(int i, int j) {
    elist[i].insert(j);
    elist[j].insert(i);
  }
  std::size_t size() { return sz; }

  unsigned connected_components(std::vector<int> &components) {
    unsigned int num = 0;
    std::vector<bool> visited(sz, false);
    for (unsigned v = 0; v < sz; ++v) {
      if (visited.at(v) == false) {
        std::vector<int> nb;
        std::vector<int> nb2;
        components.push_back(0);
        visited.at(v) = true;
        ++components.at(num);
        for (auto &e : elist[v]) {
          if (visited.at(e) == false) {
            nb.push_back(e);
            ++components.at(num);
            visited.at(e) = true;
          }
        }
        while (!nb.empty()) {
          for (auto &n : nb) {
            for (auto &e : elist.at(n)) {
              if (!visited.at(e)) {
                nb2.push_back(e);
                ++components.at(num);
                visited.at(e) = true;
              }
            }
          }
          std::swap(nb, nb2);
          nb2.clear();
        }
        ++num;
      }
    }
    return num;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  Graph g(n);

  for (int l = 0; l < m; ++l) {
    int i, j;
    std::cin >> i >> j;
    g.add_edge(i - 1, j - 1);
  }
  std::vector<int> components;
  int num = g.connected_components(components);
  if (num == 1) {
    std::cout << g.size();
    return 0;
  }
  int groups = 0;
  for (auto &m : components) {
    if (m > groups)
      groups = m;
  }

  std::cout << groups << std::endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

struct Graph {
  std::size_t sz;
  std::vector<std::set<int>> elist;
  // set avoids duplicates

  Graph(int n) : sz(n), elist(n) {}
  void add_edge(int i, int j) {
    elist[i].insert(j);
    elist[j].insert(i);
  }
  std::size_t size() { return sz; }

  unsigned connected_components(std::vector<int> &components) {
    unsigned int num = 0;
    std::vector<bool> visited(sz, false);
    for (unsigned v = 0; v < sz; ++v) {

      if (visited.at(v) == false) {
        std::vector<int> nb;
        std::vector<int> nb2;
        components.push_back(0);
        visited.at(v) = true;
        ++components.at(num);
        for (auto &e : elist[v]) {
          if (visited.at(e) == false) {
            nb.push_back(e);
            ++components.at(num);
          }
        }
        while (!nb.empty()) {
          for (auto &n : nb) {
            for (auto &n : elist.at(n)) {
              if (!visited.at(n)) {
                nb2.push_back(n);
                ++components.at(num);
                visited.at(n) = true;
              }
            }
          }
          std::swap(nb, nb2);
          nb2.clear();
        }
        ++num;
      }
    }
    return num;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  Graph g(n);

  for (int l = 0; l < m; ++l) {
    int i, j;
    std::cin >> i >> j;
    g.add_edge(i - 1, j - 1);
  }
  std::vector<int> components;
  int num = g.connected_components(components);
  if (num == 1) {
    return g.size();
  }
  int groups = 0;
  for (auto &m : components) {
    if (m > groups)
      groups = m;
  }
  std::cout << groups << std::endl;
}