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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int MAX = 14348907; // 3 ^ 15

int N, M, init;
bool memo[MAX];
vector<int> v[3];

struct state {
  int n, bit;
  state(int n = 0, int bit = init) : n(n), bit(bit) {}
};

int calcBit(int n, int pos) { return (int)pow(3, (double)pos) * n; }

int calcN(int bit, int pos) { return (bit / (int)pow(3, (double)pos)) % 3; }

void add(int &bit, int pos, int n) {
  bit -= calcBit(calcN(bit, pos), pos);
  bit += calcBit(n, pos);
}

void bfs() {
  queue<state> Q;
  state u, v;
  int max[3];
  fill(memo, memo + MAX, false);
  memo[init] = true;
  for (Q.push(u); !Q.empty();) {
    u = Q.front();
    Q.pop();

    max[0] = max[1] = max[2] = -1;
    for (int i = N - 1; i >= 0; i--) {
      if (max[calcN(u.bit, i)] == -1)
        max[calcN(u.bit, i)] = i;
    }

    if (max[1] == -1 && (max[0] == -1 || max[2] == -1)) {
      // for(int i=0;i<N;i++) cout << calcN(u.bit,i) << ' ';
      cout << u.n << endl;
      return;
    }
    if (u.n >= M)
      break;

    if (max[0] > max[1]) {
      v = u;
      add(v.bit, max[0], 1);
      v.n++;
      if (!memo[v.bit]) {
        memo[v.bit] = true;
        Q.push(v);
      }
    }
    if (max[1] > max[0]) {
      v = u;
      add(v.bit, max[1], 0);
      v.n++;
      if (!memo[v.bit]) {
        memo[v.bit] = true;
        Q.push(v);
      }
    }
    if (max[2] > max[1]) {
      v = u;
      add(v.bit, max[2], 1);
      v.n++;
      if (!memo[v.bit]) {
        memo[v.bit] = true;
        Q.push(v);
      }
    }
    if (max[1] > max[2]) {
      v = u;
      add(v.bit, max[1], 2);
      v.n++;
      if (!memo[v.bit]) {
        memo[v.bit] = true;
        Q.push(v);
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  while (cin >> N >> M && (N | M)) {
    init = 0;
    for (int i = 0; i < 3; i++) {
      int x;
      cin >> x;
      for (int j = 0; j < x; j++) {
        int c;
        cin >> c;
        c--;
        v[i].push_back(c);
        add(init, c, i);
      }
    }
    bfs();
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int n, m;
vector<int> a;
vector<vector<int>> input;
set<vector<int>> s;

struct state {
  int n;
  vector<int> v;
  vector<vector<int>> v2;
  state() {
    n = 0;
    v.assign(n, 0);
    v2.assign(3, vector<int>());
  }
};

void bfs() {
  bool f;
  int res;
  queue<state> Q;
  state u, v;
  u.v = a;
  u.v2 = input;
  for (Q.push(u); !Q.empty();) {
    u = Q.front();
    Q.pop();

    f = false;
    res = u.v[0];
    for (int i = 0; i < n; i++) {
      if (u.v[i] == 1 || u.v[i] != res) {
        f = true;
        break;
      }
    }
    if (!f) {
      cout << u.n << endl;
      return;
    }
    if (u.n == 0)
      s.insert(u.v);
    else if (u.n >= m)
      break;

    if (u.v2[0].size() > 0 &&
        (u.v2[1].size() == 0 ||
         u.v2[0][u.v2[0].size() - 1] > u.v2[1][u.v2[1].size() - 1])) {
      v = u;
      v.v2[1].push_back(u.v2[0][u.v2[0].size() - 1]);
      v.v2[0].pop_back();
      v.v[v.v2[1][v.v2[1].size() - 1]] = 1;
      v.n++;
      if (s.find(v.v) == s.end()) {
        s.insert(v.v);
        Q.push(v);
      }
    }

    if (u.v2[1].size() > 0 &&
        (u.v2[0].size() == 0 ||
         u.v2[1][u.v2[1].size() - 1] > u.v2[0][u.v2[0].size() - 1])) {
      v = u;
      v.v2[0].push_back(u.v2[1][u.v2[1].size() - 1]);
      v.v2[1].pop_back();
      v.v[v.v2[0][v.v2[0].size() - 1]] = 0;
      v.n++;
      if (s.find(v.v) == s.end()) {
        s.insert(v.v);
        Q.push(v);
      }
    }

    if (u.v2[1].size() > 0 &&
        (u.v2[2].size() == 0 ||
         u.v2[1][u.v2[1].size() - 1] > u.v2[2][u.v2[2].size() - 1])) {
      v = u;
      v.v2[2].push_back(u.v2[1][u.v2[1].size() - 1]);
      v.v2[1].pop_back();
      v.v[v.v2[2][v.v2[2].size() - 1]] = 2;
      v.n++;
      if (s.find(v.v) == s.end()) {
        s.insert(v.v);
        Q.push(v);
      }
    }

    if (u.v2[2].size() > 0 &&
        (u.v2[1].size() == 0 ||
         u.v2[2][u.v2[2].size() - 1] > u.v2[1][u.v2[1].size() - 1])) {
      v = u;
      v.v2[1].push_back(u.v2[2][u.v2[2].size() - 1]);
      v.v2[2].pop_back();
      v.v[v.v2[1][v.v2[1].size() - 1]] = 1;
      v.n++;
      if (s.find(v.v) == s.end()) {
        s.insert(v.v);
        Q.push(v);
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  while (cin >> n >> m && (n | m)) {
    a.assign(n, 0);
    input.assign(3, vector<int>());
    for (int i = 0; i < 3; i++) {
      int x;
      cin >> x;
      for (int j = 0; j < x; j++) {
        int c;
        cin >> c;
        input[i].push_back(c - 1);
        a[c - 1] = i;
      }
    }
    bfs();
  }
}