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

#include <iostream>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

typedef pair<char, char> PII;
typedef tuple<set<PII>, set<PII>, set<PII>> T;

bool neighbor(const set<PII> &s, PII p) {
  for (int i = 0; i < 4; i++) {
    static int d[] = {0, 1, 0, -1, 0};
    auto pp = PII(p.first + d[i], p.second + d[i + 1]);
    if (s.count(pp))
      return true;
  }
  return false;
}

void set_bit(__int128 &i, PII e) {
  i |= __int128(1) << e.first * 10 + e.second;
}

__int128 encode(const set<PII> &s) {
  __int128 i = 0;
  for (auto &e : s) {
    set_bit(i, e);
  }
  return i;
}

tuple<__int128, __int128> encode(const T &t) {
  return make_tuple(encode(get<0>(t)), encode(get<1>(t)));
}

int main() {
  for (int w, h; cin >> w >> h, w;) {
    set<PII> s;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int r;
        cin >> r;
        if (r) {
          s.insert({i, j});
        }
      }
    }
    auto b = *s.begin();
    s.erase(s.begin());
    bool flag = false;
    if (s.size() % 2) {
      for (auto e : s) {
        auto is = s;
        is.erase(e);
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            set<tuple<__int128, __int128>> ss;
            set<PII> bs, es;
            bs.insert(b);
            es.insert(e);
            T ist{bs, es, is};
            vector<T> v{ist};
            while (!v.empty()) {
              auto cs = v.back();
              if (get<2>(cs).empty())
                break;
              v.pop_back();
              if (!ss.insert(encode(cs)).second)
                continue;
              //	      cout<<get<2>(cs).size()<<endl;
              for (auto f : get<2>(cs)) {
                //		if(!neighbor(get<0>(cs),f))continue;
                auto vec = make_pair(f.first - b.first, f.second - b.second);
                if (j) {
                  swap(vec.first, vec.second);
                }
                for (int k = 0; k < i; k++) {
                  vec = make_pair(-vec.second, vec.first);
                }
                auto d = PII(e.first + vec.first, e.second + vec.second);
                const auto &rs = get<2>(cs);
                if (d != f && rs.count(d) && neighbor(get<0>(cs), f) &&
                    neighbor(get<1>(cs), d)) {
                  auto c = encode(cs);
                  set_bit(get<0>(c), f);
                  set_bit(get<1>(c), d);
                  if (ss.count(c) == 0) {
                    auto ns = cs;
                    get<0>(ns).insert(f);
                    get<1>(ns).insert(d);
                    get<2>(ns).erase(f);
                    get<2>(ns).erase(d);
                    v.push_back(move(ns));
                  }
                }
              }
            }
            flag |= !v.empty();
          }
        }
      }
    }
    cout << (flag ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

typedef pair<char, char> PII;
typedef tuple<set<PII>, set<PII>, set<PII>> T;

bool neighbor(const set<PII> &s, const PII &p) {
  for (int i = 0; i < 4; i++) {
    static int d[] = {0, 1, 0, -1, 0};
    auto pp = PII(p.first + d[i], p.second + d[i + 1]);
    if (s.count(pp))
      return true;
  }
  return false;
}

void set_bit(__int128 &i, PII e) {
  i |= __int128(1) << e.first * 10 + e.second;
}

__int128 encode(const set<PII> &s) {
  __int128 i = 0;
  for (auto &e : s) {
    set_bit(i, e);
  }
  return i;
}

tuple<__int128, __int128> encode(const T &t) {
  return make_tuple(encode(get<0>(t)), encode(get<1>(t)));
}

int main() {
  for (int w, h; cin >> w >> h, w;) {
    set<PII> s;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int r;
        cin >> r;
        if (r) {
          s.insert({i, j});
        }
      }
    }
    auto b = *s.begin();
    s.erase(s.begin());
    bool flag = false;
    if (s.size() % 2) {
      for (auto e : s) {
        auto is = s;
        is.erase(e);
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            set<tuple<__int128, __int128>> ss;
            set<PII> bs, es;
            bs.insert(b);
            es.insert(e);
            T ist{bs, es, is};
            vector<T> v{ist};
            while (!v.empty()) {
              auto cs = v.back();
              if (get<2>(cs).empty())
                break;
              v.pop_back();
              if (!ss.insert(encode(cs)).second)
                continue;
              //	      cout<<get<2>(cs).size()<<endl;
              for (auto f : get<2>(cs)) {
                if (!neighbor(get<0>(cs), f))
                  continue;
                auto vec = make_pair(f.first - b.first, f.second - b.second);
                if (j) {
                  swap(vec.first, vec.second);
                }
                for (int k = 0; k < i; k++) {
                  vec = make_pair(-vec.second, vec.first);
                }
                auto d = PII(e.first + vec.first, e.second + vec.second);
                const auto &rs = get<2>(cs);
                if (d != f && rs.count(d) && neighbor(get<1>(cs), d)) {
                  auto c = encode(cs);
                  set_bit(get<0>(c), f);
                  set_bit(get<1>(c), d);
                  if (ss.count(c) == 0) {
                    auto ns = cs;
                    get<0>(ns).insert(f);
                    get<1>(ns).insert(d);
                    get<2>(ns).erase(f);
                    get<2>(ns).erase(d);
                    v.push_back(move(ns));
                  }
                }
              }
            }
            flag |= !v.empty();
          }
        }
      }
    }
    cout << (flag ? "YES" : "NO") << endl;
  }
}