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

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

const int MOVE = 0, ON = 1, OFF = 2;
int n, m;
vi d[20];
vi sw[20];
pii pre[1 << 16][20];
pii pw[1 << 16][20];
struct P {
  int a = 0;
  int bit = 0, now = 0;

  bool operator<(const P r) const { return a < r.a; }
  bool operator>(const P r) const { return a > r.a; }
};
bool ls(const pii &l, const pii &r) { return l.second < r.second; }
int main() {
  while (cin >> n >> m && n) {
    FOR(i, 20) {
      d[i].clear();
      sw[i].clear();
    }
    FOR(i, 1 << 16) FOR(j, 20) pre[i][j] = pw[i][j] = pii(-1, -1);

    FOR(i, m) {
      int s, t;
      cin >> s >> t;
      d[s].push_back(t);
      d[t].push_back(s);
    }
    P start;
    vi ss;
    FOR(i, n) {
      INT(b);
      ss.push_back(b);
    }
    reverse(ALL(ss));
    FOR(i, n) {
      start.bit |= ss[i];
      start.bit <<= 1;
    }
    start.now = 1;
    FOR(i, n) {
      INT(k);
      FOR(j, k) {
        INT(x);
        sw[i + 1].push_back(x);
      }
      sort(ALL(sw[i + 1]));
    }
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(start);
    bool ok = false;
    while (!que.empty()) {
      P now = que.top();
      que.pop();
      if (now.now == n)
        ok = true;
      FOR(i, d[now.now].size()) {
        if (!(now.bit & 1 << d[now.now][i]))
          continue;
        P next = now;
        next.a++;
        next.now = d[now.now][i];
        if (pre[next.bit][next.now] == pii(-1, -1)) {
          que.push(next);
          pre[next.bit][next.now] = pii(now.bit, now.now);
          pw[next.bit][next.now] = pii(MOVE, next.now);
        }
      }
      FOR(i, sw[now.now].size()) {
        if (now.now == sw[now.now][i])
          continue;
        P next = now;
        next.a++;
        next.bit ^= 1 << sw[now.now][i];

        if (pre[next.bit][next.now] == pii(-1, -1)) {
          que.push(next);
          pre[next.bit][next.now] = pii(now.bit, now.now);
          pw[next.bit][next.now] =
              pii(now.bit & 1 << sw[now.now][i] ? OFF : ON, sw[now.now][i]);
        }
      }
    }
    vector<pii> ans;
    if (ok) {
      if (pre[1 << n][n] != pii(-1, -1)) {
        pii now(1 << n, n);
        while (now.first != start.bit || now.second != 1) {
          ans.push_back(pw[now.first][now.second]);
          now = pre[now.first][now.second];
        }
        reverse(ALL(ans));
        for (int i = 0; i < ans.size(); i++) {
          int l, r;
          if (ans[i].first == ON || ans[i].first == OFF) {
            r = l = i;
            while (ans[r].first == ON || ans[r].first == OFF) {
              r++;
              if (r == ans.size())
                break;
            }
            sort(ans.begin() + l, ans.begin() + r, ls);
          }
        }

        printf("You can go home in %d steps.\\n", ans.size());
        FOR(i, ans.size()) {
          switch (ans[i].first) {
          case MOVE:
            printf("Move to room %d.\\n", ans[i].second);
            break;
          case OFF:
            printf("Switch off room %d.\\n", ans[i].second);
            break;
          case ON:
            printf("Switch on room %d.\\n", ans[i].second);
            break;
          }
        }
      } else {
        cout << "You can not switch off all lights." << endl;
      }
    } else {
      cout << "Help me!" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

const int MOVE = 0, ON = 1, OFF = 2;
typedef vector<pii> vp;
int n, m;
vi d[16];
vi sw[16];
map<pii, int> al;
vp ans;
void calc(int bit, int now, vp &a) {
  if (!(bit & 1 << now))
    return;
  if (al.count(pii(bit, now)) && al[pii(bit, now)] < a.size())
    return;
  al[pii(bit, now)] = a.size();
  if (bit == 1 << n && now == n)
    ans = a;
  FOR(i, d[now].size()) // move
  {
    int nbit = bit;
    int nnow = d[now][i];
    vp na = a;
    na.push_back(pii(MOVE, nnow));
    calc(nbit, nnow, na);
  }

  FOR(i, sw[now].size()) // switch
  {
    int nbit = bit ^ (1 << sw[now][i]);
    int nnow = now;
    vp na = a;
    na.push_back(pii((bit & 1 << sw[now][i]) ? OFF : ON, sw[now][i]));
    calc(nbit, nnow, na);
  }
}

int main() {
  while (cin >> n >> m && n) {
    FOR(i, 16) {
      d[i].clear();
      sw[i].clear();
    }
    al.clear();
    FOR(i, m) {
      int s, t;
      cin >> s >> t;
      d[s].push_back(t);
      d[t].push_back(s);
    }
    int start = 0;
    vi ss;
    FOR(i, n) {
      INT(b);
      ss.push_back(b);
    }
    reverse(ALL(ss));
    FOR(i, n) {
      start |= ss[i];
      start <<= 1;
    }
    FOR(i, n) {
      INT(k);
      FOR(j, k) {
        INT(x);
        sw[i + 1].push_back(x);
      }
      sort(sw[i + 1].rbegin(), sw[i + 1].rend());
    }
    vp v;
    calc(start, 1, v);
    int mn = 0;
    bool ok = false;
    for (auto i : al) {
      mn = max(mn, i.first.second);
      if (i.first == pii(1 << n, n)) {
        ok = true;
        printf("You can go home in %d steps.\\n", i.second);
        for (auto p : ans) {
          switch (p.first) {
          case MOVE:
            printf("Move to room %d.\\n", p.second);
            break;
          case ON:
            printf("Switch on room %d.\\n", p.second);
            break;
          case OFF:
            printf("Switch off room %d.\\n", p.second);
            break;
          }
        }
        break;
      }
    }
    if (!ok) {
      if (mn == n) {
        cout << "You can not switch off all lights." << endl;
      } else {
        cout << "Help me!" << endl;
      }
    }
  }
}