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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

int nextcomb(int S) {
  int x = S & -S;
  int y = S + x;
  S = ((S & ~y) / x >> 1) | y;
  return S;
}

int main() {
  for (int N, M, first = 1; cin >> N >> M && N; first = 0) {
    if (!first)
      cout << endl;

    vector<string> lang(N);
    map<string, int> lang_dic;
    REP(i, N) {
      string s;
      cin >> s;
      lang[i] = s;
      lang_dic[s] = i;
    }

    bool have[30][30] = {};
    for (int i = 0; i < M; i++) {
      int K;
      cin >> K;
      while (K--) {
        string s;
        cin >> s;
        have[i][lang_dic[s]] = true;
      }
    }

    vector<int> ans;

    bool b[30][30] = {};
    REP(i, N) REP(j, N) REP(k, M) b[i][j] |= have[k][i] & have[k][j];

    for (int K = 1; K < 6; K++) {
      if (ans.size() > 0)
        break;
      int S = (1 << K) - 1;
      while (S < 1 << N) {
        vector<int> v;
        for (int i = 0; i < N; i++)
          if (S >> i & 1)
            v.push_back(i);
        S = nextcomb(S);
        bool ok = true;

        REP(i, M)
        if (none_of(v.begin(), v.end(), [&](int vi) { return have[i][vi]; }))
            ok = false;

        if (not ok)
          continue;

        bool bb[30][30] = {};
        for (auto vi : v)
          for (auto vj : v)
            bb[vi][vj] = b[vi][vj];
        for (auto vk : v)
          for (auto vi : v)
            for (auto vj : v)
              bb[vi][vj] |= bb[vi][vk] & bb[vk][vj];

        REP(i, M) REP(j, i) {
          bool talk = false;
          for (auto vi : v)
            for (auto vj : v)
              if (bb[vi][vj] && have[i][vi] && have[j][vj]) {
                goto NEXT;
              }
          ok = false;
          goto NEXT2;

        NEXT:;
        }

        if (ok)
          ans = v;
      NEXT2:;
      }
    }
    if (ans.empty()) {
      cout << "Impossible" << endl;
      continue;
    }
    cout << ans.size() << endl;
    for (auto ai : ans) {
      cout << lang[ai] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

int nextcomb(int S) {
  int x = S & -S;
  int y = S + x;
  S = ((S & ~y) / x >> 1) | y;
  return S;
}

int main() {
  for (int N, M, first = 1; cin >> N >> M && N; first = 0) {
    if (!first)
      cout << endl;

    vector<string> lang(N);
    map<string, int> lang_dic;
    REP(i, N) {
      string s;
      cin >> s;
      lang[i] = s;
      lang_dic[s] = i;
    }

    bool have[30][30] = {};
    for (int i = 0; i < M; i++) {
      int K;
      cin >> K;
      while (K--) {
        string s;
        cin >> s;
        have[i][lang_dic[s]] = true;
      }
    }

    vector<int> ans;

    bool b[30][30] = {};
    REP(i, N) REP(j, N) REP(k, M) b[i][j] |= have[k][i] & have[k][j];

    for (int K = 1; K < 6; K++) {
      if (ans.size() > 0)
        break;
      int S = (1 << K) - 1;
      while (S < 1 << N) {
        vector<int> v;
        for (int i = 0; i < N; i++)
          if (S >> i & 1)
            v.push_back(i);
        bool ok = true;

        REP(i, M)
        if (none_of(v.begin(), v.end(), [&](int vi) { return have[i][vi]; }))
            ok = false;

        bool bb[30][30] = {};
        for (auto vi : v)
          for (auto vj : v)
            bb[vi][vj] = b[vi][vj];
        for (auto vk : v)
          for (auto vi : v)
            for (auto vj : v)
              bb[vi][vj] |= bb[vi][vk] & bb[vk][vj];

        REP(i, M) REP(j, M) {
          bool talk = false;
          for (auto vi : v)
            for (auto vj : v)
              if (bb[vi][vj] && have[i][vi] && have[j][vj])
                talk = true;
          if (not talk)
            ok = false;
        }

        if (ok)
          ans = v;

        S = nextcomb(S);
      }
    }
    if (ans.empty()) {
      cout << "Impossible" << endl;
      continue;
    }
    cout << ans.size() << endl;
    for (auto ai : ans) {
      cout << lang[ai] << endl;
    }
  }
  return 0;
}