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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
  int N, M;
  while (cin >> N >> M && N) {
    const int goal = (1 << N) - 1;
    int start[3] = {};
    int s_top[3] = {};
    memset(s_top, -1, sizeof(s_top));
    REP(i, 3) {
      int k;
      cin >> k;
      REP(j, k) {
        int t;
        cin >> t;
        start[i] |= 1 << (t - 1);
        s_top[i] = max(s_top[i], t - 1);
      }
    }
    int ans = INF;
    for (int iter = 0; iter < 2; iter++) {
      int last_move = iter; // 0 : A<->B, 1 : B<->C
      int state[3];
      int top[3];
      memcpy(state, start, sizeof(state));
      memcpy(top, s_top, sizeof(top));
      for (int time = 0; time <= M; time++) {
        if (state[0] == goal || state[2] == goal) {
          ans = min(ans, time);
          break;
        }
        int a, b;
        if (last_move == 0) {
          last_move = 1;
          a = 1, b = 2;
        } else {
          last_move = 0;
          a = 0, b = 1;
        }
        if (top[a] > top[b])
          swap(a, b);
        assert(top[b] >= 0); // i can move it
        top[a] = top[b];
        state[a] |= 1 << top[a];    // add top[a]
        state[b] &= ~(1 << top[a]); // remove top[a]
        // find next top
        for (int i = top[a] - 1; i >= -1; i--) {
          if (i == -1 || (state[b] >> i & 1)) {
            top[b] = i;
            break;
          }
        }
      }
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<vvi, int> pvi;
bool ok(vvi a) {
  return (a[0].empty() && a[1].empty()) || (a[1].empty() && a[2].empty());
}
void print(vvi a) {
  REP(i, a.size()) {
    printf("%d:", i + 1);
    REP(j, a[i].size()) { printf("%d ", a[i][j]); }
    putchar('\\n');
  }
  putchar('\\n');
}
int toi(vvi a) {
  int res = 0;
  REP(i, a[1].size()) res += pow(3.0, a[1][i]);
  REP(i, a[2].size()) res += pow(3.0, a[2][i]) * 2;
  return res;
}
bool used[14350000];
int solve(vvi a, int m) {
  queue<pvi> que;
  que.push(pvi(a, 0));
  memset(used, 0, sizeof(used));
  while (!que.empty()) {
    pvi p = que.front();
    que.pop();
    vvi na = p.first;
    int nt = p.second;
    if (nt > m)
      return -1;
    if (ok(na))
      return nt;
    int useidx = toi(na);
    if (used[useidx])
      continue;
    used[useidx] = true;
    // printf("--------------------\\n");
    // print(na);
    REP(i, 3) REP(j, 3) {
      if (abs(i - j) == 1 && na[i].size() &&
          (na[j].empty() || na[i].back() > na[j].back())) {
        vvi nna = na;
        nna[j].push_back(nna[i].back());
        nna[i].pop_back();
        useidx = toi(nna);
        if (used[useidx])
          continue;
        que.push(pvi(nna, nt + 1));
        // print(nna);
      }
    }
  }
  return -1;
}

int main() {
  int n, m;
  while (cin >> n >> m) {
    cin.ignore();
    vvi a(3);
    REP(i, 3) {
      int ps;
      cin >> ps;
      while (ps--) {
        int tmp;
        cin >> tmp;
        a[i].push_back(tmp);
      }
    }
    cout << solve(a, m) << endl;
  }
  return 0;
}