Problem p00426 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define PB push_back
using namespace std;
struct Cup {
  vector<vector<int>> A;
  int cnt, b;
  Cup(vector<vector<int>> A_, int cnt_, int b_) {
    A = A_;
    cnt = cnt_;
    b = b_;
  }
  Cup() {}
};
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    queue<Cup> Q;
    int mn = m + 5;
    int cnttt = 0;
    vector<vector<int>> V(3);
    V[0].PB(0);
    V[1].PB(0);
    V[2].PB(0);
    for (int i = 0; i < 3; i++) {
      int s;
      cin >> s;
      for (int j = 0; j < s; j++) {
        int d;
        cin >> d;
        V[i].PB(d);
      }
    }
    if (V[0].back() < V[1].back()) {
      V[0].PB(V[1].back());
      V[1].pop_back();
      Q.push(Cup(V, 1, 1));
      V[1].PB(V[0].back());
      V[0].pop_back();
    } else if (V[0].back() != 0) {
      V[1].PB(V[0].back());
      V[0].pop_back();
      Q.push(Cup(V, 1, 0));
      V[0].PB(V[1].back());
      V[1].pop_back();
    }

    if (V[2].back() < V[1].back()) {
      V[2].PB(V[1].back());
      V[1].pop_back();
      Q.push(Cup(V, 1, 3));
      V[1].PB(V[2].back());
      V[2].pop_back();
    } else if (V[2].back() != 0) {
      V[1].PB(V[2].back());
      V[2].pop_back();
      Q.push(Cup(V, 1, 2));
      V[2].PB(V[1].back());
      V[1].pop_back();
    }
    if (V[0].size() == n + 1 || V[2].size() == 0) {
      cout << 0 << endl;
      continue;
    }

    while (Q.size()) {
      Cup x = Q.front();
      Q.pop();
      if (x.cnt > m)
        continue;

      if (x.A[0].size() == n + 1 || x.A[2].size() == n + 1) {
        mn = min(mn, x.cnt);
        break;
      }

      if (x.b == 2 || x.b == 3) {
        if (x.A[0].back() < x.A[1].back()) {
          x.A[0].PB(x.A[1].back());
          x.A[1].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 1));
          x.A[1].PB(x.A[0].back());
          x.A[0].pop_back();
        } else if (x.A[0].back() != 0) {
          x.A[1].PB(x.A[0].back());
          x.A[0].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 0));
          x.A[0].PB(x.A[1].back());
          x.A[1].pop_back();
        }
      } else {
        if (x.A[2].back() < x.A[1].back()) {
          x.A[2].PB(x.A[1].back());
          x.A[1].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 3));
          x.A[1].PB(x.A[2].back());
          x.A[2].pop_back();
        } else if (x.A[2].back() != 0) {
          x.A[1].PB(x.A[2].back());
          x.A[2].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 2));
          x.A[2].PB(x.A[1].back());
          x.A[1].pop_back();
        }
      }
    }

    if (mn == m + 5)
      mn = -1;
    cout << mn << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define PB push_back
using namespace std;
struct Cup {
  vector<vector<int>> A;
  int cnt, b;
  Cup(vector<vector<int>> A_, int cnt_, int b_) {
    A = A_;
    cnt = cnt_;
    b = b_;
  }
  Cup() {}
};
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    queue<Cup> Q;
    int mn = m + 5;
    int cncn = 0;
    vector<vector<int>> V(3);
    V[0].PB(0);
    V[1].PB(0);
    V[2].PB(0);
    for (int i = 0; i < 3; i++) {
      int s;
      cin >> s;
      for (int j = 0; j < s; j++) {
        int d;
        cin >> d;
        V[i].PB(d);
      }
    }
    int w;

    if (V[0].back() < V[1].back()) {
      V[0].PB(V[1].back());
      V[1].pop_back();
      Q.push(Cup(V, 1, 1));
      V[1].PB(V[0].back());
      V[0].pop_back();
    } else if (V[0].back() != 0) {
      V[1].PB(V[0].back());
      V[0].pop_back();
      Q.push(Cup(V, 1, 0));
      V[0].PB(V[1].back());
      V[1].pop_back();
    }

    if (V[2].back() < V[1].back()) {
      V[2].PB(V[1].back());
      V[1].pop_back();
      Q.push(Cup(V, 1, 3));
      V[1].PB(V[2].back());
      V[2].pop_back();
    } else if (V[2].back() != 0) {
      V[1].PB(V[2].back());
      V[2].pop_back();
      Q.push(Cup(V, 1, 2));
      V[2].PB(V[1].back());
      V[1].pop_back();
    }
    if (V[0].size() == n + 1 || V[2].size() == 0) {
      cout << 0 << endl;
      continue;
    }

    while (Q.size()) {
      Cup x = Q.front();
      Q.pop();
      if (x.cnt > m)
        continue;

      if (x.A[0].size() == n + 1)
        mn = min(mn, x.cnt);
      if (x.A[2].size() == n + 1)
        mn = min(mn, x.cnt);

      if (x.b == 2 || x.b == 3) {
        if (x.A[0].back() < x.A[1].back()) {
          x.A[0].PB(x.A[1].back());
          x.A[1].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 1));
          x.A[1].PB(x.A[0].back());
          x.A[0].pop_back();
        } else if (x.A[0].back() != 0) {
          x.A[1].PB(x.A[0].back());
          x.A[0].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 0));
          x.A[0].PB(x.A[1].back());
          x.A[1].pop_back();
        }
      } else {
        if (x.A[2].back() < x.A[1].back()) {
          x.A[2].PB(x.A[1].back());
          x.A[1].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 3));
          x.A[1].PB(x.A[2].back());
          x.A[2].pop_back();
        } else if (x.A[2].back() != 0) {
          x.A[1].PB(x.A[2].back());
          x.A[2].pop_back();
          Q.push(Cup(x.A, x.cnt + 1, 2));
          x.A[2].PB(x.A[1].back());
          x.A[1].pop_back();
        }
      }
    }
    if (mn == m + 5)
      mn = -1;
    cout << mn << endl;
  }
}