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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n, m;
  typedef tuple<vector<char>, vector<char>, int> T;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    // vector<char> cup(n,0);
    vector<char> cup;
    vector<char> beforecup;
    cup.resize(n);

    bool flag = true;
    rep(i, 3) {
      int n1;
      cin >> n1;
      rep(j, n1) {
        int n2;
        cin >> n2;
        n2--;
        cup[n2] = i;
      }
    } // 入力ここまで
    queue<T> que;
    que.push(T(cup, cup, 0));
    while (que.size() != 0) {
      T state = que.front();
      cup = get<0>(state);
      beforecup = get<1>(state);
      int movecount = get<2>(state);
      que.pop();
      // rep(i,cup.size())cout << cup[i];
      // cout << endl;
      int bigger[3];
      int maxcount = 0;
      bool tray[3] = {true, true, true};
      if (n == count(cup.begin(), cup.end(), 0) ||
          n == count(cup.begin(), cup.end(), 2)) {
        cout << movecount << endl;
        flag = false;
        break;
      }
      rep(i, n) {
        int i2 = n - i - 1;
        rep(j, 3) {
          if (tray[j] == true && cup[i2] == j) {
            tray[j] = false;
            bigger[maxcount] = i2;
            maxcount++;
            break;
          }
        }
        if (maxcount == 3)
          break;
      }
      movecount++;
      if (movecount > m)
        break;
      if (cup[bigger[0]] - 1 >= 0) {
        cup[bigger[0]]--;
        if (cup != beforecup)
          que.push(T(cup, get<0>(state), movecount));
        cup[bigger[0]]++;
      }
      if (cup[bigger[0]] + 1 <= 2) {
        cup[bigger[0]]++;
        if (cup != beforecup)
          que.push(T(cup, get<0>(state), movecount));
        cup[bigger[0]]--;
      }
      if (maxcount >= 2) {
        if (cup[bigger[1]] - 1 >= 0 && cup[bigger[1]] - 1 != cup[bigger[0]]) {
          cup[bigger[1]]--;
          if (cup != beforecup)
            que.push(T(cup, get<0>(state), movecount));
          cup[bigger[1]]++;
        } else if (cup[bigger[1]] + 1 <= 2 &&
                   cup[bigger[1]] + 1 != cup[bigger[0]]) {
          cup[bigger[1]]++;
          if (cup != beforecup)
            que.push(T(cup, get<0>(state), movecount));
          cup[bigger[1]]--;
        }
      }
    }
    if (flag)
      cout << "-1" << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    // vector<char> cup(n,0);
    vector<char> cup;
    cup.resize(n);

    bool flag = true;
    rep(i, 3) {
      int n1;
      cin >> n1;
      rep(j, n1) {
        int n2;
        cin >> n2;
        n2--;
        cup[n2] = i;
      }
    } // 入力ここまで
    queue<vector<char>> que;
    que.push(cup);
    map<vector<char>, int> mp;
    mp[cup] = 0;
    while (que.size() != 0) {
      cup = que.front();
      que.pop();
      // rep(i,cup.size())cout << cup[i];
      // cout << endl;
      int bigger[3];
      int maxcount = 0;
      bool tray[3] = {true, true, true};
      if (n == count(cup.begin(), cup.end(), 0) ||
          n == count(cup.begin(), cup.end(), 2)) {
        cout << mp[cup] << endl;
        flag = false;
        break;
      }
      rep(i, n) {
        int i2 = n - i - 1;
        rep(j, 3) {
          if (tray[j] == true && cup[i2] == j) {
            tray[j] = false;
            bigger[maxcount] = i2;
            maxcount++;
            break;
          }
        }
        if (maxcount == 3)
          break;
      }
      int movecount = mp[cup] + 1;
      if (movecount > m)
        break;
      if (cup[bigger[0]] - 1 >= 0) {
        cup[bigger[0]]--;
        // if(mp.count(cup) == 0){
        if (mp.find(cup) == mp.end()) {
          mp[cup] = movecount;
          que.push(cup);
        }
        cup[bigger[0]]++;
      }
      if (cup[bigger[0]] + 1 <= 2) {
        cup[bigger[0]]++;
        // if(mp.count(cup) == 0){
        if (mp.find(cup) == mp.end()) {
          mp[cup] = movecount;
          que.push(cup);
        }
        cup[bigger[0]]--;
      }
      if (maxcount >= 2) {
        if (cup[bigger[1]] - 1 >= 0 && cup[bigger[1]] - 1 != cup[bigger[0]]) {
          cup[bigger[1]]--;
          // if(mp.count(cup) == 0){
          if (mp.find(cup) == mp.end()) {
            mp[cup] = movecount;
            que.push(cup);
          }
          cup[bigger[1]]++;
        } else if (cup[bigger[1]] + 1 <= 2 &&
                   cup[bigger[1]] + 1 != cup[bigger[0]]) {
          cup[bigger[1]]++;
          // if(mp.count(cup) == 0){
          if (mp.find(cup) == mp.end()) {
            mp[cup] = movecount;
            que.push(cup);
          }
          cup[bigger[1]]--;
        }
      }
    }
    if (flag)
      cout << "-1" << endl;
  }
  return 0;
}