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

#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> mat;

int n, m;

bool isChange(int &from, int &to, mat &g) {
  auto s = g[from];
  auto t = g[to];
  if (s.size() == 0)
    return false;
  if (t.size() > 0 && t[t.size() - 1] > s[s.size() - 1])
    return false;
  t.push_back(s[s.size() - 1]);
  s.erase(s.begin() + s.size() - 1);
  g[from] = s;
  g[to] = t;

  return true;
}

bool change(int &now, int &pre, mat &g) {
  vector<int> from = {0, 1, 1, 2};
  vector<int> to = {1, 2, 0, 1};
  for (int i = 0; i < 4; i++) {
    if (to[i] == pre && from[i] == now)
      continue;
    if (isChange(from[i], to[i], g)) {
      now = to[i];
      pre = from[i];

      return true;
    }
  }

  return false;
}

int main() {
  while (cin >> n >> m, n || m) {
    mat v(3);
    for (auto &i : v) {
      int c;
      cin >> c;
      for (int j = 0; j < c; j++) {
        int x;
        cin >> x;
        i.push_back(x);
      }
    }

    mat g1(3), g2(3);
    for (int i = 1; i <= n; i++) {
      g1[0].push_back(i);
      g2[2].push_back(i);
    }

    int res = 1 << 28;
    vector<int> now = {0, 1, 1, 2};
    vector<int> pre = {1, 0, 2, 1};
    for (int i = 0; i < 4; i++) {
      int tmp = 0;
      auto g(v);
      while (tmp <= m) {
        if (g[0].size() == n || g[2].size() == n)
          break;
        if (!change(now[i], pre[i], g)) {
          tmp = 1 << 28;
          break;
        }
        tmp++;
      }
      res = min(res, tmp);
    }

    cout << (res > m ? -1 : res) << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> mat;

const int INF = 1 << 28;
int n, m;

bool check(mat &v) {
  if (v[0].size() == n + 1 || v[2].size() == n + 1)
    return true;
  return false;
}

string stom(mat &v) {
  string res;
  res = "";
  for (int i = 0; i < 3; i++) {
    for (auto j : v[i]) {
      if (j < 0)
        continue;
      res = res + (char)('a' + j);
    }
    for (int j = v[i].size(); j <= n; j++) {
      res = res + '0';
    }
  }
  return res;
}

void hoge(mat &v, vector<mat> &next, set<string> &now, set<string> &pre) {
  for (int i = 0; i < 3; i++) {
    if (v[i].size() > 1) {
      for (int j = 0; j < 3; j++) {
        if (i == j || v[i][v[i].size() - 1] < v[j][v[j].size() - 1])
          continue;
        if (i == 0 && j == 2)
          continue;
        if (i == 2 && j == 0)
          continue;
        v[j].push_back(v[i][v[i].size() - 1]);
        v[i].erase(v[i].end() - 1);
        if (pre.find(stom(v)) == pre.end()) {
          next.push_back(v);
          now.insert(stom(v));
        }
        v[i].push_back(v[j][v[j].size() - 1]);
        v[j].erase(v[j].end() - 1);
      }
    }
  }
}

int main() {
  while (cin >> n >> m, n || m) {
    mat v(3);
    for (int i = 0; i < 3; i++) {
      int n_;
      cin >> n_;
      v[i].push_back(-1);
      for (int j = 0; j < n_; j++) {
        int x;
        cin >> x;
        v[i].push_back(x);
      }
    }

    int res = 0;
    vector<mat> G;
    G.push_back(v);
    set<string> now;
    now.insert(stom(v));
    while (res <= m) {
      vector<mat> G_(G);
      G.resize(0);
      auto pre(now);
      now.clear();
      for (auto g : G_) {
        if (check(g)) {
          goto escap;
        }
        hoge(g, G, now, pre);
      }
      res++;
    }
  escap:
    cout << (res > m ? -1 : res) << endl;
  }

  return 0;
}