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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef vector<int> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];
int N, M, K;

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs.size() == M)
    return r;
  while (includes(begin(bs), end(bs), begin(is[idx[nth]]), end(is[idx[nth]]))) {
    nth++;
  }
  BS n;
  set_difference(begin(is[idx[nth]]), end(is[idx[nth]]), begin(bs), end(bs),
                 back_inserter(n));
  if (n.size() == 1) {
    BS nx;
    set_union(begin(bs), end(bs), begin(is[a[n[0]] ^ b[n[0]] ^ idx[nth]]),
              end(is[a[n[0]] ^ b[n[0]] ^ idx[nth]]), back_inserter(nx));
    return rec(nx, nth + 1, r - 1);
  } else {
    BS nx;
    set_union(begin(bs), end(bs), begin(is[idx[nth]]), end(is[idx[nth]]),
              back_inserter(nx));
    int rv = rec(nx, nth + 1, r - 1);
    BS o = bs;
    for (auto i : n) {
      BS dst;
      set_union(begin(o), end(o), begin(is[a[i] ^ b[i] ^ idx[nth]]),
                end(is[a[i] ^ b[i] ^ idx[nth]]), back_inserter(dst));
      o.swap(dst);
    }
    return max(rv, rec(o, nth + 1, r - n.size()));
  }
}

int main() {
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]].push_back(i);
    is[b[i]].push_back(i);
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef vector<int> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];
int N, M, K;

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs.size() == M)
    return r;
  while (includes(begin(bs), end(bs), begin(is[idx[nth]]), end(is[idx[nth]]))) {
    nth++;
  }
  BS n;
  set_difference(begin(is[idx[nth]]), end(is[idx[nth]]), begin(bs), end(bs),
                 back_inserter(n));
  if (n.size() == 1) {
    BS nx;
    set_union(begin(bs), end(bs), begin(is[a[n[0]] ^ b[n[0]] ^ idx[nth]]),
              end(is[a[n[0]] ^ b[n[0]] ^ idx[nth]]), back_inserter(nx));
    return rec(nx, nth + 1, r - 1);
  } else {
    BS n;
    set_union(begin(bs), end(bs), begin(is[idx[nth]]), end(is[idx[nth]]),
              back_inserter(n));
    int rv = rec(n, nth + 1, r - 1);
    BS o = bs;
    for (auto i : n) {
      BS dst;
      set_union(begin(o), end(o), begin(is[a[i] ^ b[i] ^ idx[nth]]),
                end(is[a[i] ^ b[i] ^ idx[nth]]), back_inserter(dst));
      o = move(dst);
    }
    return max(rv, rec(o, nth + 1, r - n.size()));
  }
}

int main() {
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]].push_back(i);
    is[b[i]].push_back(i);
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}