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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

struct clasure {
  int x[3];
  int y[3];
  bool operator<(const clasure &c) const {
    REP(i, 3) if (x[i] != c.x[i]) return x[i] < c.x[i];
    REP(i, 3) if (y[i] != c.y[i]) return y[i] < c.y[i];
    return false;
  }
};

int to_id(char c) {
  if (isupper(c))
    return c - 'A';
  else
    return c - 'a' + 26;
}

struct SAT3 {
  vector<clasure> cls;
  static const int N = 26;
  int state[26];
  SAT3(vector<clasure> cls_) : cls(cls_) { memset(state, -1, sizeof(state)); }
  vector<int> solve() {
    if (!dfs(0))
      return vector<int>(100, -1);
    vector<int> res;
    REP(i, N) if (state[i] == 1) { res.push_back(i); }
    return res;
  }
  bool dfs(int k) {
    clasure &c = cls[k];
    if (k == cls.size())
      return true;
    int memo[26];
    memcpy(memo, state, sizeof(state));
    for (int i = 0; i < 3; i++) {
      bool ok = true;
      for (int j = 0; j <= i; j++) {
        int ny = c.y[j] ^ (j != i);
        if (state[c.x[j]] != -1 && state[c.x[j]] != ny)
          ok = false;
        state[c.x[j]] = ny;
      }
      if (ok) {
        if (dfs(k + 1))
          return true;
      }
      memcpy(state, memo, sizeof(state));
    }
    return false;
  }
};

int main() {
  int N;
  while (cin >> N && N) {
    string s[3];
    REP(i, 3) cin >> s[i];
    set<clasure> cls;
    REP(i, N) {
      REP(S, 1 << 3) {
        clasure c;
        REP(j, 3) {
          c.x[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) % 26;
          c.y[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) / 26;
        }
        REP(j, 3)
        REP(i, j) if (make_pair(c.x[i], c.y[i]) > make_pair(c.x[j], c.y[j])) {
          swap(c.x[i], c.x[j]);
          swap(c.y[i], c.y[j]);
        }
        cls.insert(c);
      }
    }

    SAT3 solver(vector<clasure>(cls.begin(), cls.end()));
    vector<int> res = solver.solve();
    if (res.size() > 30) {
      cout << -1 << endl;
    } else {
      cout << res.size();
      REP(i, res.size()) cout << " " << (char)(res[i] + 'A');
      cout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

struct clasure {
  int x[3];
  int y[3];
};

int to_id(char c) {
  if (isupper(c))
    return c - 'A';
  else
    return c - 'a' + 26;
}

struct SAT3 {
  vector<clasure> cls;
  static const int N = 26;
  int state[26];
  SAT3(vector<clasure> cls_) : cls(cls_) { memset(state, -1, sizeof(state)); }
  vector<int> solve() {
    if (!dfs(0))
      return vector<int>(100, -1);
    vector<int> res;
    REP(i, N) if (state[i] == 1) { res.push_back(i); }
    return res;
  }
  bool dfs(int k) {
    clasure &c = cls[k];
    if (k == cls.size())
      return true;
    int memo[26];
    memcpy(memo, state, sizeof(state));
    for (int i = 0; i < 3; i++) {
      bool ok = true;
      for (int j = 0; j <= i; j++) {
        int ny = c.y[j] ^ (j != i);
        if (state[c.x[j]] != -1 && state[c.x[j]] != ny)
          ok = false;
        state[c.x[j]] = ny;
      }
      if (ok) {
        if (dfs(k + 1))
          return true;
      }
      memcpy(state, memo, sizeof(state));
    }
    return false;
  }
};

int main() {
  int N;
  while (cin >> N && N) {
    string s[3];
    REP(i, 3) cin >> s[i];
    vector<clasure> cls;
    REP(i, N) {
      REP(S, 1 << 3) {
        clasure c;
        REP(j, 3) {
          c.x[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) % 26;
          c.y[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) / 26;
        }
        cls.push_back(c);
      }
    }
    random_shuffle(cls.begin(), cls.end());

    SAT3 solver(cls);
    vector<int> res = solver.solve();
    if (res.size() > 30) {
      cout << -1 << endl;
    } else {
      cout << res.size();
      REP(i, res.size()) cout << " " << (char)(res[i] + 'A');
      cout << endl;
    }
  }
  return 0;
}