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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int saiki(int a, int b, int c) {
  if (!a && !b)
    return 0;
  if (c & 1)
    return saiki(a >> 1, b >> 1, c >> 1);
  if (b & 1)
    return saiki(c >> 1, b >> 1, a >> 1) + saiki((a | b | c) >> 1, 0, 0) + 1;
  if (a & 1)
    return saiki(a >> 1, b >> 1, c >> 1) + 2 * saiki((a | b | c) >> 1, 0, 0) +
           2;
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int cup[3];
    memset(cup, 0, sizeof(cup));
    REP(i, 3) {
      int t;
      cin >> t;
      REP(j, t) {
        int temp;
        cin >> temp;
        cup[i] |= 1 << (temp - 1);
      }
    }
    int ans = min(saiki(cup[0], cup[1], cup[2]), saiki(cup[2], cup[1], cup[0]));
    cout << (ans > m ? -1 : ans) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    set<pair<short, short>> s;
    vector<vector<short>> cup(3);
    REP(i, 3) {
      int t;
      cin >> t;
      REP(j, t) {
        int temp;
        cin >> temp;
        cup[i].PB(temp);
      }
    }
    queue<vector<vector<short>>> q;
    queue<int> qv;
    q.push(cup);
    qv.push(0);
    int ans = 2000000000;
    while (!q.empty()) {
      cup = q.front();
      int v = qv.front();
      q.pop();
      qv.pop();
      pair<short, short> temp(0, 0);
      REP(j, cup[0].size()) { temp.first += 1 << (cup[0][j] - 1); }
      REP(j, cup[1].size()) { temp.second += 1 << (cup[1][j] - 1); }

      if (s.find(temp) != s.end())
        continue;
      else
        s.insert(temp);
      if (cup[1].size() == 0 && (cup[0].size() == 0 || cup[2].size() == 0)) {
        ans = v;
        break;
      }
      REP(i, 2) {
        if (cup[i].size() > 0 &&
            (cup[i + 1].size() == 0 || cup[i].back() > cup[i + 1].back())) {
          cup[i + 1].PB(cup[i].back());
          cup[i].erase(cup[i].end() - 1);
          q.push(cup);
          qv.push(v + 1);
          cup[i].PB(cup[i + 1].back());
          cup[i + 1].erase(cup[i + 1].end() - 1);
        }
        if (cup[i + 1].size() > 0 &&
            (cup[i].size() == 0 || cup[i].back() < cup[i + 1].back())) {
          cup[i].PB(cup[i + 1].back());
          cup[i + 1].erase(cup[i + 1].end() - 1);
          q.push(cup);
          qv.push(v + 1);
          cup[i + 1].PB(cup[i].back());
          cup[i].erase(cup[i].end() - 1);
        }
      }
    }
    if (ans > m)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}