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

// 17
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

struct S {
  int t;
  pii p;
  vector<int> v;
  int l;
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    bool d[16][16] = {{}};
    while (m--) {
      int s, t;
      cin >> s >> t;
      d[s][t] = d[t][s] = true;
    }
    pii is(1, 0);
    for (int i = 1; i <= n; i++) {
      int l;
      cin >> l;
      is.second |= l << i;
    }
    bool l[16][16] = {{}};
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      while (k--) {
        int r;
        cin >> r;
        l[i][r] = true;
      }
    }
    queue<S> que;
    S s;
    s.p = is;
    s.t = s.l = 0;
    que.push(s);
    set<int> p;
    bool f = false;
    while (!que.empty()) {
      S c = que.front();
      if (c.p.first == n) {
        f = true;
        if (c.p.second == 1 << n) {
          break;
        }
      }
      que.pop();
      if (!(c.p.second >> c.p.first & 1) ||
          !p.insert(((c.p.first * 16) + c.l << 16) + c.p.second).second)
        continue;
      for (int i = c.l + 1; i <= n; i++) {
        if (l[c.p.first][i]) {
          S n = c;
          n.l = i;
          n.p.second ^= 1 << i;
          n.v.push_back(i + (n.p.second >> i & 1) * 100);
          n.t++;
          que.push(n);
        }
      }
      for (int i = 1; i <= n; i++) {
        if (d[c.p.first][i] && (c.p.second >> i & 1)) {
          S n = c;
          n.l = 0;
          n.v.push_back(-i);
          n.p.first = i;
          n.t++;
          que.push(n);
        }
      }
    }
    if (que.empty()) {
      cout << (f ? "You can not switch off all lights." : "Help me!") << endl;
    } else {
      cout << "You can go home in " << que.front().t << " steps." << endl;
      vector<int> v = que.front().v;
      for (int i = 0; i < v.size(); i++) {
        if (v[i] > 0) {
          if (v[i] > 100) {
            cout << "Switch on room " << v[i] - 100 << '.' << endl;
          } else {
            cout << "Switch off room " << v[i] << '.' << endl;
          }
        } else {
          cout << "Move to room " << -v[i] << '.' << endl;
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 17
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

struct S {
  int t;
  pii p;
  vector<int> v;
  int l;
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    bool d[16][16] = {{}};
    while (m--) {
      int s, t;
      cin >> s >> t;
      d[s][t] = d[t][s] = true;
    }
    pii is(1, 0);
    for (int i = 1; i <= n; i++) {
      int l;
      cin >> l;
      is.second |= l << i;
    }
    bool l[16][16] = {{}};
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      while (k--) {
        int r;
        cin >> r;
        l[i][r] = true;
      }
    }
    queue<S> que;
    S s;
    s.p = is;
    s.t = s.l = 0;
    que.push(s);
    static bool p[1 << 23];
    fill(p, p + (1 << 23), false);
    bool f = false;
    while (!que.empty()) {
      S c = que.front();
      if (c.p.first == n) {
        f = true;
        if (c.p.second == 1 << n) {
          break;
        }
      }
      que.pop();
      if (p[((c.p.first * 16) + c.l << 15) + c.p.second / 2]++)
        continue;
      for (int i = c.l + 1; i <= n; i++) {
        if (l[c.p.first][i]) {
          S n = c;
          n.l = i;
          n.p.second ^= 1 << i;
          n.v.push_back(i + (n.p.second >> i & 1) * 100);
          n.t++;
          que.push(n);
        }
      }
      for (int i = 1; i <= n; i++) {
        if (d[c.p.first][i] && (c.p.second >> i & 1)) {
          S n = c;
          n.l = 0;
          n.v.push_back(-i);
          n.p.first = i;
          n.t++;
          que.push(n);
        }
      }
    }
    if (que.empty()) {
      cout << (f ? "You can not switch off all lights." : "Help me!") << endl;
    } else {
      cout << "You can go home in " << que.front().t << " steps." << endl;
      vector<int> v = que.front().v;
      for (int i = 0; i < v.size(); i++) {
        if (v[i] > 0) {
          if (v[i] > 100) {
            cout << "Switch on room " << v[i] - 100 << '.' << endl;
          } else {
            cout << "Switch off room " << v[i] << '.' << endl;
          }
        } else {
          cout << "Move to room " << -v[i] << '.' << endl;
        }
      }
    }
  }
  return 0;
}