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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <stack>

using namespace std;

class P {
public:
  int cnt;
  int bef;
  stack<int> S[3];
};

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), (n | m)) {
    P f;
    for (int i = 0; i < 3; i++) {
      int x;
      scanf("%d", &x);
      for (int j = 0; j < x; j++) {
        int tmp;
        scanf("%d", &tmp);
        f.S[i].push(tmp);
      }
    }
    f.cnt = 0;
    f.bef = -1;
    queue<P> Q;
    Q.push(f);
    bool flg = true;
    while (!Q.empty()) {
      P p = Q.front();
      Q.pop();
      if (p.cnt > m) {
        flg = false;
        break;
      }
      bool h = false;
      for (int i = 0; i < 3; i++) {
        if (i == 1)
          continue;
        if (p.S[i].size() == n) {
          printf("%d\\n", p.cnt);
          h = true;
        }
      }
      if (h) {
        break;
      }
      int top0 = (p.S[0].size() > 0) ? p.S[0].top() : 0;
      int top1 = (p.S[1].size() > 0) ? p.S[1].top() : 0;
      int top2 = (p.S[2].size() > 0) ? p.S[2].top() : 0;
      int copy0 = top0, copy1 = top1, copy2 = top2;
      P sub;
      sub.cnt = p.cnt + 1;
      if (top0 < top1 && p.bef != 0) {
        p.S[1].pop();
        p.S[0].push(top1);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        sub.bef = 1;
        Q.push(sub);
        p.S[0].pop();
        p.S[1].push(copy1);
      }
      if (top0 > top1 && p.bef != 1) {
        p.S[0].pop();
        p.S[1].push(top0);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        sub.bef = 0;
        Q.push(sub);
        p.S[1].pop();
        p.S[0].push(copy0);
      }
      if (top1 < top2 && p.bef != 2) {
        p.S[2].pop();
        p.S[1].push(top2);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        sub.bef = 3;
        Q.push(sub);
        p.S[1].pop();
        p.S[2].push(copy2);
      }
      if (top1 > top2 && p.bef != 3) {
        p.S[1].pop();
        p.S[2].push(top1);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        sub.bef = 2;
        Q.push(sub);
        p.S[2].pop();
        p.S[1].push(copy1);
      }
    }
    if (!flg)
      puts("-1");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <stack>

using namespace std;

class P {
public:
  int cnt;
  stack<int> S[3];
};

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), (n | m)) {
    P f;
    for (int i = 0; i < 3; i++) {
      int x;
      scanf("%d", &x);
      for (int j = 0; j < x; j++) {
        int tmp;
        scanf("%d", &tmp);
        f.S[i].push(tmp);
      }
    }
    f.cnt = 0;
    queue<P> Q;
    Q.push(f);
    bool flg = true;
    while (!Q.empty()) {
      P p = Q.front();
      Q.pop();
      if (p.cnt > m) {
        flg = false;
        break;
      }
      bool h = false;
      for (int i = 0; i < 3; i++) {
        if (i == 1)
          continue;
        if (p.S[i].size() == n) {
          printf("%d\\n", p.cnt);
          h = true;
        }
      }
      if (h) {
        break;
      }
      int top0 = (p.S[0].size() > 0) ? p.S[0].top() : 0;
      int top1 = (p.S[1].size() > 0) ? p.S[1].top() : 0;
      int top2 = (p.S[2].size() > 0) ? p.S[2].top() : 0;
      int copy0 = top0, copy1 = top1, copy2 = top2;
      P sub;
      sub.cnt = p.cnt + 1;
      if (top0 < top1) {
        p.S[1].pop();
        p.S[0].push(top1);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        Q.push(sub);
        p.S[0].pop();
        p.S[1].push(copy1);
      } else {
        p.S[0].pop();
        p.S[1].push(top0);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        Q.push(sub);
        p.S[1].pop();
        p.S[0].push(copy0);
      }
      if (top1 < top2) {
        p.S[2].pop();
        p.S[1].push(top2);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        Q.push(sub);
        p.S[1].pop();
        p.S[2].push(copy2);
      } else {
        p.S[1].pop();
        p.S[2].push(top1);
        for (int i = 0; i < 3; i++)
          sub.S[i] = p.S[i];
        Q.push(sub);
        p.S[2].pop();
        p.S[1].push(copy1);
      }
    }
    if (!flg)
      puts("-1");
  }
  return 0;
}