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

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

int main() {
  int N, M, K, tmp;
  while (cin >> N >> M && (N || M)) {
    vector<int> v(N + 1);
    for (int t = 0; t < 3; t++) {
      cin >> K;
      for (int k = 0; k < K; k++) {
        cin >> tmp;
        v[tmp] = t;
      }
    }

    vector<int> a(N + 1), c(N + 1), p(N + 1);
    p[N] = 2;
    a[N] = v[N];
    c[N] = 2 - v[N];
    for (int n = N; n > 1; n--) {
      p[n - 1] = 3 * p[n] + 2;
      switch (v[n - 1]) {
      case 0:
        a[n - 1] = a[n];
        c[n - 1] = c[n] + 2 * p[n] + 2;
        break;
      case 1:
        a[n - 1] = c[n] + p[n] + 1;
        c[n - 1] = a[n] + p[n] + 1;
        break;
      case 2:
        a[n - 1] = a[n] + 2 * p[n] + 2;
        c[n - 1] = c[n];
        break;
      }
    }
    int res = min(a[1], c[1]);
    if (M < res)
      res = -1;
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int INF = 10000000;

map<vector<set<int>>, int> dpp;

void dfs(vector<set<int>> sit, int n, int cnt, int m) {
  if (dpp.find(sit) != dpp.end()) {
    if (dpp[sit] <= cnt) {
      return;
    } else {
      dpp[sit] = cnt;
    }
  } else {
    dpp[sit] = cnt;
  }

  if (cnt >= m)
    return;

  if ((sit[0].size() == 0 && sit[1].size() == 0) ||
      (sit[1].size() == 0 && sit[2].size() == 0)) {
    return;
  }

  // A->B
  vector<set<int>> tmp = sit;
  if (sit[0].size() != 0) {
    set<int>::reverse_iterator it = sit[0].rbegin();
    int cont = *it;
    if (sit[1].size() == 0 || *(sit[1].rbegin()) < cont) {
      sit[0].erase(*it);
      sit[1].insert(cont);
      dfs(sit, n, cnt + 1, m);
    }
  }
  // B->A
  sit = tmp;
  if (sit[1].size() != 0) {
    set<int>::reverse_iterator it = sit[1].rbegin();
    int cont = *it;
    if (sit[0].size() == 0 || *(sit[0].rbegin()) < cont) {
      sit[1].erase(*it);
      sit[0].insert(cont);
      dfs(sit, n, cnt + 1, m);
    }
  }
  // B->C
  sit = tmp;
  if (sit[1].size() != 0) {
    set<int>::reverse_iterator it = sit[1].rbegin();
    int cont = *it;
    if (sit[2].size() == 0 || *(sit[2].rbegin()) < cont) {
      sit[1].erase(*it);
      sit[2].insert(cont);
      dfs(sit, n, cnt + 1, m);
    }
  }
  // C->B
  sit = tmp;
  if (sit[2].size() != 0) {
    set<int>::reverse_iterator it = sit[2].rbegin();
    int cont = *it;
    if (sit[1].size() == 0 || *(sit[1].rbegin()) < cont) {
      sit[2].erase(*it);
      sit[1].insert(cont);
      dfs(sit, n, cnt + 1, m);
    }
  }
}

int main() {

  while (1) {
    dpp.clear();
    int n;
    int m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    vector<set<int>> tray;
    for (int i = 0; i < 3; i++) {
      set<int> s;
      int num;
      cin >> num;
      for (int j = 0; j < num; j++) {
        int tmp;
        cin >> tmp;
        s.insert(tmp);
      }
      tray.push_back(s);
    }

    dfs(tray, n, 0, m);

    vector<set<int>> fin[3];
    for (int i = 0; i < 3; i++) {
      vector<set<int>> vsi;
      for (int j = 0; j < 3; j++) {
        set<int> si;
        if (i == j) {
          for (int k = 0; k < n; k++) {
            si.insert(k + 1);
          }
        }
        vsi.push_back(si);
      }
      fin[i] = vsi;
    }

    int minWalk = 20000000;
    for (int i = 0; i < 3; i++) {
      if (i == 1)
        continue;
      if (dpp.find(fin[i]) != dpp.end()) {
        minWalk = min(minWalk, dpp[fin[i]]);
      }
    }
    if (minWalk == 20000000) {
      cout << -1 << endl;
    } else
      cout << minWalk << endl;
  }

  return 0;
}