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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

int c[20], v[20];
int visit[1 << 20];
int ans;
int n, U;

void dfs(int cnt, int bits, int sum) {
  if (visit[bits])
    return;
  visit[bits] = 1;
  if (ans <= cnt)
    return;
  if (sum >= U) {
    ans = cnt;
    return;
  }

  for (int i = 0; i < n; ++i) {
    if (bits & (1 << i))
      continue;
    if ((bits & v[i]) != v[i])
      continue;
    dfs(cnt + 1, bits | (1 << i), sum + c[i]);
  }
}

int main() {
  while (cin >> n >> U, n | U) {
    rep(i, n) {
      cin >> c[i];
      int k;
      cin >> k;
      v[i] = 0;
      rep(j, k) {
        int x;
        cin >> x;
        v[i] |= 1 << x;
      }
    }
    ans = n;
    MEMSET(visit, 0);
    dfs(0, 0, 0);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

int main() {
  int n, U;
  while (cin >> n >> U, n | U) {
    vector<int> c(n), v(n);
    rep(i, n) {
      cin >> c[i];
      int k;
      cin >> k;
      rep(j, k) {
        int x;
        cin >> x;
        v[i] |= 1 << x;
      }
    }
    int ans = 1 << 30;
    rep(i, 1 << n) {
      int sum = 0, cnt = 0, ok = true;
      rep(j, n) {
        if (~i & (1 << j))
          continue;
        if ((i & v[j]) != v[j])
          ok = false;
        sum += c[j];
        ++cnt;
      }
      if (ok && sum >= U)
        ans = min(ans, cnt);
    }
    cout << ans << endl;
  }

  return 0;
}