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

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;
const double PI = 3.141592653589793238;
const Int INF = 999999999999;
using namespace std;

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

struct state {
  vector<int> A, B, C;
  int pre_a, pre_c;
  Int step;
  state() : step(0), pre_a(-1), pre_c(-1) {}
};

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    queue<state> que;

    state s;
    REP(i, 3) {
      int k;
      cin >> k;
      REP(j, k) {
        int a;
        cin >> a;
        if (i == 0)
          s.A.push_back(a);
        if (i == 1)
          s.B.push_back(a);
        if (i == 2)
          s.C.push_back(a);
      }
    }

    que.push(s);
    while (!que.empty()) {
      state now = que.front();
      que.pop();

      if ((now.B.empty() && now.C.empty()) ||
          (now.B.empty() && now.A.empty())) {
        cout << now.step << endl;
        break;
      }

      if (now.step > m) {
        cout << -1 << endl;
        break;
      }

      if (!now.A.empty() && (now.B.empty() || (now.A.back() > now.B.back()))) {
        state next = now;
        if (now.pre_a != next.A.back()) {
          next.B.push_back(next.A.back());
          next.pre_a = next.A.back();
          next.pre_c = -1;
          next.A.pop_back();
          ++next.step;
          que.push(next);
        }
      }
      if (!now.B.empty() && (now.A.empty() || (now.A.back() < now.B.back()))) {
        state next = now;
        if (now.pre_a != next.B.back()) {
          next.A.push_back(next.B.back());
          next.pre_a = next.B.back();
          next.pre_c = -1;
          next.B.pop_back();
          ++next.step;
          que.push(next);
        }
      }
      if (!now.B.empty() && (now.C.empty() || (now.B.back() > now.C.back()))) {
        state next = now;
        if (now.pre_c != next.B.back()) {
          next.C.push_back(next.B.back());
          next.pre_c = next.B.back();
          next.pre_a = -1;
          next.B.pop_back();
          ++next.step;
          que.push(next);
        }
      }
      if (!now.C.empty() && (now.B.empty() || (now.B.back() < now.C.back()))) {
        state next = now;
        if (now.pre_c != next.C.back()) {
          next.B.push_back(next.C.back());
          next.pre_c = next.C.back();
          next.pre_a = -1;
          next.C.pop_back();
          ++next.step;
          que.push(next);
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;
const double PI = 3.141592653589793238;
const Int INF = 999999999999;
using namespace std;

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

struct state {
  vector<int> A, B, C;
  Int step;
  state() : step(0) {}
};

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    queue<state> que;

    state s;
    REP(i, 3) {
      int k;
      cin >> k;
      REP(j, k) {
        int a;
        cin >> a;
        if (i == 0)
          s.A.push_back(a);
        if (i == 1)
          s.B.push_back(a);
        if (i == 2)
          s.C.push_back(a);
      }
    }

    que.push(s);
    while (!que.empty()) {
      state now = que.front();
      que.pop();

      if ((now.B.empty() && now.C.empty()) ||
          (now.B.empty() && now.A.empty())) {
        cout << now.step << endl;
        break;
      }

      if (now.step > m) {
        cout << -1 << endl;
        break;
      }

      if (!now.A.empty() && (now.B.empty() || (now.A.back() > now.B.back()))) {
        state next = now;
        next.B.push_back(next.A.back());
        next.A.pop_back();
        ++next.step;
        que.push(next);
      }
      if (!now.B.empty() && (now.A.empty() || (now.A.back() < now.B.back()))) {
        state next = now;
        next.A.push_back(next.B.back());
        next.B.pop_back();
        ++next.step;
        que.push(next);
      }
      if (!now.B.empty() && (now.C.empty() || (now.B.back() > now.C.back()))) {
        state next = now;
        next.C.push_back(next.B.back());
        next.B.pop_back();
        ++next.step;
        que.push(next);
      }
      if (!now.C.empty() && (now.B.empty() || (now.B.back() < now.C.back()))) {
        state next = now;
        next.B.push_back(next.C.back());
        next.C.pop_back();
        ++next.step;
        que.push(next);
      }
    }
  }
  return 0;
}