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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class Tuple, unsigned Index>
void print_tuple(ostream &os, const Tuple &t) {}

template <class Tuple, unsigned Index, class Type, class... Types>
void print_tuple(ostream &os, const Tuple &t) {
  if (Index > 0)
    os << ", ";
  os << get<Index>(t);
  print_tuple<Tuple, Index + 1, Types...>(os, t);
}

template <class... Types>
ostream &operator<<(ostream &os, const tuple<Types...> &t) {
  os << '(';
  print_tuple<tuple<Types...>, 0, Types...>(os, t);
  return os << ')';
}

template <class Iterator>
ostream &dump_range(ostream &, Iterator, const Iterator &);

template <typename T> ostream &operator<<(ostream &os, vector<T> c) {
  return dump_range(os, c.cbegin(), c.cend());
}

template <class Iterator>
ostream &dump_range(ostream &os, Iterator first, const Iterator &last) {
  os << '[';
  for (int i = 0; first != last; ++i, ++first) {
    if (i)
      os << ", ";
    os << *first;
  }
  return os << ']';
}

int ans;
int n, m;
vector<int> cover;
int used;
vector<vector<int>> edges;

void update(int value) {
  if (used == (1 << n) - 1) {
    ans = value;
  }
}

void dfs(int idx, int sum) {
  if (sum >= ans)
    return;

  if (idx == n) {
    update(sum);
  } else {
    dfs(idx + 1, sum);

    int tmp = used;
    used |= cover[idx];
    dfs(idx + 1, sum + 1);
    swap(used, tmp);
  }
}

int solve() {
  cover.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    cover[i] |= (1 << i);
    for (int j = 0; j < static_cast<int>(edges[i].size()); ++j) {
      cover[i] |= (1 << edges[i][j]);
    }
  }

  used = 0;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (edges[i].size() == 0) {
      used |= (1 << i);
      ++cnt;
    }
  }

  ans = n;
  dfs(0, cnt);
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> n >> m && n) {
    edges.assign(n, vector<int>());
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      edges[a].emplace_back(b);
      edges[b].emplace_back(a);
    }

    cout << solve() << endl;
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class Tuple, unsigned Index>
void print_tuple(ostream &os, const Tuple &t) {}

template <class Tuple, unsigned Index, class Type, class... Types>
void print_tuple(ostream &os, const Tuple &t) {
  if (Index > 0)
    os << ", ";
  os << get<Index>(t);
  print_tuple<Tuple, Index + 1, Types...>(os, t);
}

template <class... Types>
ostream &operator<<(ostream &os, const tuple<Types...> &t) {
  os << '(';
  print_tuple<tuple<Types...>, 0, Types...>(os, t);
  return os << ')';
}

template <class Iterator>
ostream &dump_range(ostream &, Iterator, const Iterator &);

template <typename T> ostream &operator<<(ostream &os, vector<T> c) {
  return dump_range(os, c.cbegin(), c.cend());
}

template <class Iterator>
ostream &dump_range(ostream &os, Iterator first, const Iterator &last) {
  os << '[';
  for (int i = 0; first != last; ++i, ++first) {
    if (i)
      os << ", ";
    os << *first;
  }
  return os << ']';
}

int ans;
int n, m;
vector<bool> used;
vector<vector<int>> edges;

void update(int value) {
  assert(value < ans);
  if (find(used.begin(), used.end(), false) == used.end()) {
    ans = value;
  }
}

void dfs(int idx, int sum) {
  if (idx == n) {
    update(sum);
  } else if (used[idx]) {
    dfs(idx + 1, sum);
    return;
  } else if (sum < ans) {
    dfs(idx + 1, sum);
    if (sum + 1 == ans)
      return;

    vector<bool> tmp = used;
    used[idx] = true;
    for (int i = 0; i < static_cast<int>(edges[idx].size()); ++i) {
      used[edges[idx][i]] = true;
    }
    dfs(idx + 1, sum + 1);
    used.swap(tmp);
  }
}

int solve() {
  used.assign(n, false);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (edges[i].size() == 0) {
      used[i] = true;
      ++cnt;
    }
  }

  ans = n;
  dfs(0, cnt);
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> n >> m && n) {
    edges.assign(n, vector<int>());
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      edges[a].emplace_back(b);
      edges[b].emplace_back(a);
    }

    cout << solve() << endl;
  }

  return EXIT_SUCCESS;
}