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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"
long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}
int dp[14400000];
int N, M;
int to_hash(const vector<int> &nums) {
  int hash = 0;
  for (auto n : nums) {
    hash = 3 * hash + n;
  }
  return hash;
}
int getans(vector<int> &starts) {
  queue<pair<vector<int>, int>> que;
  que.emplace(starts, 0);
  while (!que.empty()) {
    auto v = que.front();
    que.pop();
    vector<int> nums = v.first;
    int now = v.second;
    const int hash = to_hash(nums);
    if (dp[hash] == false) {
      continue;
    } else {
      dp[hash] = false;
      if (now >= M + 1) {
        return M + 1;
      }
      if (to_hash(nums) == 0 || hash + 1 == powint(3, N)) {
        return now;
      } else {
        dp[hash] = false;
        vector<int> tops(3, -1);
        for (int i = 0; i < N; ++i) {
          tops[nums[i]] = i;
        }
        int ans = M;
        if (tops[0] < tops[1]) {
          nums[tops[1]] = 0;
          que.emplace(nums, now + 1);
          nums[tops[1]] = 1;

        } else {
          nums[tops[0]] = 1;
          que.emplace(nums, now + 1);
          nums[tops[0]] = 0;
        }
        if (tops[1] < tops[2]) {
          nums[tops[2]] = 1;
          que.emplace(nums, now + 1);
          nums[tops[2]] = 2;
        } else {
          nums[tops[1]] = 2;
          que.emplace(nums, now + 1);
          nums[tops[1]] = 1;
        }
      }
    }
  }
  return M + 1;
}

int main() {
  while (1) {
    for (int i = 0; i < 14400000; ++i) {
      dp[i] = true;
    }
    cin >> N >> M;
    if (!N)
      break;
    vector<int> nows(N);
    for (int i = 0; i < 3; ++i) {
      int a;
      cin >> a;
      for (int j = 0; j < a; ++j) {
        int b;
        cin >> b;
        b--;
        nows[b] = i;
      }
    }
    int ans = getans(nows);
    if (ans == M + 1)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

int dp[14400000];
int N, M;
int to_hash(const vector<int> &nums) {
  int hash = 0;
  for (auto n : nums) {
    hash = 3 * hash + n;
  }
  return hash;
}
int getans(vector<int> &nums, int now) {
  int hash = to_hash(nums);
  if (dp[hash] == -2) {
    return M;
  }
  if (dp[hash] == -1) {
    if (now >= M)
      return M;
    if (to_hash(nums) == 0) {
      return now;
    } else {
      dp[hash] = -2;
      vector<int> tops(3, -1);
      for (int i = 0; i < N; ++i) {
        tops[nums[i]] = i;
      }
      int ans = M;
      if (tops[0] < tops[1]) {
        if (tops[1] != -1) {
          nums[tops[1]] = 0;
          ans = min(ans, getans(nums, now + 1));
          nums[tops[1]] = 1;
        }
      } else {
        if (tops[0] != -1) {
          nums[tops[0]] = 1;
          ans = min(ans, getans(nums, now + 1));
          nums[tops[0]] = 0;
        }
      }
      if (tops[1] < tops[2]) {
        if (tops[2] != -1) {
          nums[tops[2]] = 1;
          ans = min(ans, getans(nums, now + 1));
          nums[tops[2]] = 2;
        }
      } else {
        if (tops[1] != -1) {
          nums[tops[1]] = 2;
          ans = min(ans, getans(nums, now + 1));
          nums[tops[1]] = 1;
        }
      }
      return dp[hash] = ans;
    }
  } else {
    return dp[hash];
  }
}

int main() {
  while (1) {
    for (int i = 0; i < 14400000; ++i) {
      dp[i] = -1;
    }
    cin >> N >> M;
    if (!N)
      break;
    vector<int> nows(N);
    for (int i = 0; i < 3; ++i) {
      int a;
      cin >> a;
      for (int j = 0; j < a; ++j) {
        int b;
        cin >> b;
        b--;
        nows[b] = i;
      }
    }
    int ans = getans(nows, 0);
    if (ans == M)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}