alexjercan/bugnet huggingface dataset:
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int n, U;

vector<int> req;
vector<int> unit;

int main() {
  while (scanf("%d%d", &n, &U), n || U) {
    req = vector<int>(n, 0);
    unit = vector<int>(n);
    int k, r;
    for (int i = 0; i < n; ++i) {
      scanf("%d%d", &unit[i], &k);
      for (int j = 0; j < k; ++j) {
        scanf("%d", &r);
        req[i] |= 1 << r;
      }
    }
    int j, sol = 100, u, c, skip;
    for (int i = 0; i < (1 << n); ++i) {
      u = c = 0;
      for (j = n - 1; j >= 0; --j) {
        if (!(i & (1 << j)))
          continue;
        c++;
        if ((i & req[j]) != req[j] || sol < c) {
          i += (((skip = ((i & req[j]) ^ req[j] | 1 << j)) & -skip) - 1) & ~i;
          break;
        }
        u += unit[j];
      }
      sol = j < 0 && u >= U ? min(sol, c) : sol;
    }
    printf("%d\\n", sol);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, U, K;
  while (cin >> N >> U, N | U) {
    pair<int, vector<int>> piv[N];
    int ans = INT_MAX;
    for (int i = 0; i < N; i++) {
      cin >> piv[i].first >> K;
      for (int j = 0; j < K; j++) {
        int a;
        cin >> a;
        piv[i].second.push_back(a);
      }
    }
    for (int i = 1; i < (1 << N); i++) {
      int cnt = 0;
      int sum = 0;
      int tmp = 0;
      for (int j = 0; (1 << j) <= i; j++) {
        if (i & (1 << j)) {
          tmp++;
          if (tmp > ans)
            break;
          int k;
          for (k = 0; k < piv[j].second.size(); k++)
            if (!(i & (1 << piv[j].second[k])))
              break;
          if (k == piv[j].second.size()) {
            // printf("i:%d  j:%d\\n",i,j);
            sum += piv[j].first;
            cnt++;
          }
        }
      }
      if (tmp == cnt && sum >= U && ans > cnt) {
        ans = cnt;
        // puts("_____ok_____");
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)

int n, U;
int ans;
int c[22],
    need[22]; // eÈÚÌPÊ,
              // C·éÌÉKvÈÈÚ(rbgÇ)

// ÈÚÌæèûÉµªÈ¢©Ç¤©`FbN·é
bool check(int used) {
  rep(i, n) {
    // CÈÚÉKvÈÈÚÌPÊªæêÄé©`FbN
    if ((used & (1 << i)) > 0 && (used & need[i]) != need[i]) {
      return false;
    }
  }
  return true;
}

// id:ÈÚÔ, used:ÇÌÈÚæÁ½©(rbgÇ),
// sum:PÊv, cnt:CÈÚ
void solve(int id, int used, int sum, int cnt) {
  if (id == n) {
    if (sum >= U && ans > cnt && check(used)) {
      ans = cnt;
    }
    return;
  }

  solve(id + 1, used, sum, cnt);                         // ÆçÈ¢
  solve(id + 1, used | (1 << id), sum + c[id], cnt + 1); // Æé
}

int main(void) {
  while (scanf("%d%d", &n, &U), n || U) {
    rep(i, n) {
      int k;
      scanf("%d%d", &c[i], &k);
      need[i] = 0;

      while (k--) {
        int r;
        scanf("%d", &r);
        need[i] |= (1 << r);
      }
    }

    ans = INT_MAX;
    solve(0, 0, 0, 0);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)

int n, m, c[20], k[20];
int ans, bit[20];
bool r[20][20]; // ツ療猟静堋配ツ療ア

bool correct(string s) {
  int x = 0;
  rep(i, s.length()) if (s[i] == '1') x += (1 << i);
  rep(i, s.length()) if (s[i] == '1' && (x & bit[i]) != bit[i]) {
    return false;
  }
  return true;
}

void solve(string s, int now, int sum) {
  if (s.length() == n) {
    if (sum >= m && ans > now && correct(s))
      ans = now;
    return;
  }

  solve(s + "0", now, sum);
  solve(s + "1", now + 1, sum + c[s.length()]);
}

void writeBit(int id, int b) {
  bit[id] |= b;
  rep(i, n) if (r[i][id] && (b & (1 << i)) == 0) { writeBit(i, b | (1 << i)); }
}

int main(void) {
  while (cin >> n >> m && (n || m)) {
    memset(r, 0, sizeof(r));
    rep(i, n) {
      cin >> c[i] >> k[i];
      rep(j, k[i]) {
        int to;
        cin >> to;
        r[i][to] = true;
      }
    }

    memset(bit, 0, sizeof(bit));
    rep(i, n) if (k[i] == 0) { writeBit(i, 1 << i); }

    ans = n;
    solve("", 0, 0);
    cout << ans << endl;
  }

  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define LET(name, value) __typeof(value) name = value
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (LET(i, (c).begin()); i != (c).end(); ++i)

int c[30];
int deps[30];

inline unsigned next_set(unsigned x) {
  unsigned smallest = x & -x;
  unsigned ripple = x + smallest;
  unsigned new_smallest = ripple & -ripple;
  unsigned ones = ((new_smallest / smallest) >> 1) - 1;
  return ripple | ones;
}

inline int bsf(int x) {
  __asm__("bsf %1, %0;" : "=r"(x) : "r"(x));
  return x;
}

int main() {
  int n, U;
  while (scanf("%d%d", &n, &U), n | U) {
    memset(deps, 0, sizeof(deps));
    REP(i, n) {
      int k;
      scanf("%d%d", &c[i], &k);
      REP(j, k) {
        int r;
        scanf("%d", &r);
        deps[i] |= 1 << r;
      }
    }
    FOREQ(bits, 1, n) {
      for (int S = (1 << bits) - 1; S < (1 << n); S = next_set(S)) {
        int sum = 0;
        bool ok = true;
        for (int T = S; T; T &= T - 1) {
          int i = bsf(T);
          sum += c[i];
          if ((S | deps[i]) != S) {
            ok = false;
            break;
          }
        }
        if (sum >= U && ok) {
          printf("%d\\n", bits);
          goto next;
        }
      }
    }
  next:;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define LET(name, value) __typeof(value) name = value
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (LET(i, (c).begin()); i != (c).end(); ++i)

int c[30];
int deps[30];

int main() {
  int n, U;
  while (scanf("%d%d", &n, &U), n | U) {
    memset(deps, 0, sizeof(deps));
    REP(i, n) {
      int k;
      scanf("%d%d", &c[i], &k);
      REP(j, k) {
        int r;
        scanf("%d", &r);
        deps[i] |= 1 << r;
      }
    }
    int ans = n;
    REP(S, 1 << n) {
      int bitcount = __builtin_popcount(S);
      if (bitcount >= ans) {
        continue;
      }
      int sum = 0;
      REP(i, n) {
        if (S & (1 << i)) {
          sum += c[i];
        }
      }
      if (sum < U) {
        continue;
      }
      bool ok = true;
      REP(i, n) {
        if (S & (1 << i)) {
          if ((S | deps[i]) != S) {
            ok = false;
            break;
          }
        }
      }
      if (ok) {
        ans = bitcount;
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

static inline int getint() {
  int ret = 0;
  int g;
  while ((g = getchar() - '0'), g < 0)
    ;
  ret = g;
  while ((g = getchar() - '0'), g >= 0) {
    ret *= 10;
    ret += g;
  }
  return ret;
}

static inline int cnt(unsigned int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int memo[1 << 20];

int main() {
  int c[20], k, r[20];
  int n, u;
  int *m = memo;
  register unsigned int i, j, l;
  for (i = 0; i < (1 << 20); i++)
    *m++ = cnt(i);

  while (n = getint(), u = getint(), n + u) {
    int ans = n;
    m = &memo[(1 << n) - 1];
    for (i = 0; i - n; i++) {
      c[i] = getint();
      k = getint();
      r[i] = 0;
      for (j = 0; j < k; j++) {
        int t = getint();
        r[i] |= (1 << t);
      }
    }

    for (i = (1 << n) - 1; i; i--) {
      int tni = 0;
      int ok = 0xFFFFFFFF;
      int cc = *m--;
      if (cc >= ans)
        continue;

      for (j = 0; ok & j - n; j++) {
        if (i & (1 << j))
          tni += c[j];

        // if(!(!(i & (1<<j)) || !((i & r[i]) == r[j])))
        if (!((!(i & (1 << j))) | (!((i & r[j]) ^ r[j]))))
          ok = 0;
      }

      if (tni >= u & ok)
        ans = cc;
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

static inline int getint() {
  int ret = 0;
  int g;
  while ((g = getchar() - '0'), g < 0)
    ;
  ret = g;
  while ((g = getchar() - '0'), g >= 0) {
    ret *= 10;
    ret += g;
  }
  return ret;
}

static inline int cnt(unsigned int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int memo[1 << 20];

int main() {
  int c[20], k, r[20];
  int n, u;
  register unsigned int i, j, l;
  for (i = (1 << 20); i; i--)
    memo[i] = cnt(i);

  while (n = getint(), u = getint(), n + u) {
    int ans = n;
    int *m = &memo[(1 << n) - 1];
    for (i = 0; i - n; i++) {
      c[i] = getint();
      k = getint();
      r[i] = 0;
      for (j = 0; j < k; j++) {
        int t = getint();
        r[i] |= (1 << t);
      }
    }

    for (i = (1 << n) - 1; i; i--) {
      int tni = 0;
      int ok = 0xFFFFFFFF;
      int cc = *m--;
      if (cc >= ans)
        continue;

      for (j = 0; ok & j - n; j++) {
        if (i & (1 << j))
          tni += c[j];

        // if(!(!(i & (1<<j)) || !((i & r[i]) == r[j])))
        if (!((!(i & (1 << j))) | (!((i & r[j]) ^ r[j]))))
          ok = 0;
      }

      if (tni >= u & ok)
        ans = cc;
    }
    printf("%d\\n", ans);
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;

int deg[1 << 20];
int c[20];
int need[20];

int bitscount(int n) {
  int res = 0;
  while (n) {
    res++;
    n -= (n & -n);
  }
  return res;
}

int main() {
  int n, U, k, r;
  while (cin >> n >> U && (n || U)) {
    memset(deg, -1, sizeof(deg));
    memset(need, 0, sizeof(need));
    int res = INF;
    rep(i, n) {
      cin >> c[i] >> k;
      rep(j, k) {
        cin >> r;
        need[i] |= (1 << r);
      }
    }
    deg[0] = 0;
    rep(i, (1 << n)) {
      if (deg[i] == -1)
        continue;
      if (deg[i] >= U) {
        res = min(res, bitscount(i));
        continue;
      }
      rep(j, n) {
        if (!(i & (1 << j)) && ((i & need[j]) == need[j])) {
          deg[i | (1 << j)] = c[j] + deg[i];
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;

int deg[1 << 20];
int c[20];
int need[20];

int bitscount(int n) {
  int res = 0;
  while (n) {
    res++;
    n -= (n & -n);
  }
  return res;
}

int main() {
  int n, U, k, r;
  while (cin >> n >> U && (n || U)) {
    memset(deg, -1, sizeof(deg));
    memset(need, 0, sizeof(need));
    int res = INF;
    rep(i, n) {
      cin >> c[i] >> k;
      rep(j, k) {
        cin >> r;
        need[i] |= (1 << r);
      }
    }
    deg[0] = 0;
    rep(i, (1 << n)) {
      if (deg[i] == -1)
        continue;
      if (deg[i] >= U)
        res = min(res, bitscount(i));
      rep(j, n) {
        if (!(i & (1 << j)) && ((i & need[j]) == need[j])) {
          deg[i | (1 << j)] = c[j] + deg[i];
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Tag {
  int pat, value, cost;
  Tag(int p, int v, int c) : pat(p), value(v), cost(c) {}
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N, U;
    cin >> N >> U;
    if (!N && !U)
      break;

    vector<int> need_mask(N, 0);
    vector<int> value(N, 0);
    for (int i = 0; i < N; ++i) {
      int K;
      cin >> value[i] >> K;
      int mask = 0;
      while (K--) {
        int R;
        cin >> R;
        mask |= (1 << R);
      }
      need_mask[i] = mask;
    }
    int ans = N;
    vector<int> seen(1 << N, 0);
    queue<Tag> q;
    q.push(Tag(0, 0, 0));
    seen[0] = 1;
    while (!q.empty()) {
      Tag t = q.front();
      if (t.value >= U) {
        ans = t.cost;
        break;
      }
      q.pop();
      for (int i = 0; i < N; ++i) {
        if (!(t.pat & (1 << i)) && (t.pat & need_mask[i]) == need_mask[i]) {
          int next_pat = t.pat | (1 << i);
          if (!seen[next_pat]) {
            seen[next_pat] = 1;
            q.push(Tag(next_pat, t.value + value[i], t.cost + 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N, U;
    cin >> N >> U;
    if (!N && !U)
      break;

    vector<int> need_mask(N, 0);
    vector<int> value(N, 0);
    for (int i = 0; i < N; ++i) {
      int K;
      cin >> value[i] >> K;
      int mask = 0;
      while (K--) {
        int R;
        cin >> R;
        mask |= (1 << R);
      }
      need_mask[i] = mask;
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < N; ++i) {
        int tmp = need_mask[i];
        for (int j = 0; j < N; ++j) {
          if (need_mask[i] & (1 << j)) {
            tmp |= need_mask[j];
          }
        }
        if (tmp != need_mask[i]) {
          need_mask[i] = tmp;
          update = true;
        }
      }
      if (!update)
        break;
    }
    vector<int> seen(1 << N, 0);
    int ans = N;
    for (int pattern = 0; pattern < (1 << N); ++pattern) {
      if (seen[pattern])
        continue;
      int pat = pattern;
      for (int pos = 0; pos < N; ++pos) {
        if (pattern & (1 << pos)) {
          pat |= need_mask[pos];
        }
      }
      if (seen[pat])
        continue;

      int sum = 0;
      for (int pos = 0; pos < N; ++pos) {
        if (pat & (1 << pos))
          sum += value[pos];
      }
      if (sum >= U) {
        ans = min(ans, __builtin_popcount(pat));
      }
      seen[pat] = 1;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int n, u;
  while (cin >> n >> u, n || u) {
    int a[20], b[20] = {};
    for (i = 0; i < n; ++i) {
      cin >> a[i];
      int m;
      cin >> m;
      for (j = 0; j < m; ++j) {
        int p;
        cin >> p;
        b[i] |= 1 << p;
      }
    }
    bool c[1 << 20] = {};
    c[0] = true;
    queue<pair<pair<int, int>, int>> d;
    d.push(make_pair(make_pair(0, 0), 0));
    while (!d.empty()) {
      int p, q, r;
      p = d.front().first.first;
      q = d.front().first.second;
      r = d.front().second;
      if (q >= u)
        break;
      d.pop();
      for (i = 0; i < n; ++i) {
        if ((p & (1 << i)) || (p & b[i]) != b[i] || c[p | (1 << i)])
          continue;
        c[p | (1 << i)] = true;
        d.push(make_pair(make_pair(p | (1 << i), q + a[i]), r + 1));
      }
    }
    cout << d.front().second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[20], b[20][20], c[20], d[1 << 20], n, u;
int fi(int p, int q, int r) {
  int i, j;
  int mn = 1 << 30;
  if (d[p]) {
    if (r >= u) {
      mn = q;
    } else {
      for (i = 0; i < n; ++i) {
        if (c[i] == 0) {
          for (j = 0; b[i][j] != -1; ++j) {
            if (c[b[i][j]] == 0) {
              p |= 1 << b[i][j];
              ++q;
              r += a[b[i][j]];
            }
            ++c[b[i][j]];
          }
          mn = min(mn, fi(p | (1 << i), q + 1, r + a[i]));
          for (j = 0; b[i][j] != -1; ++j) {
            --c[b[i][j]];
            if (c[b[i][j]] == 0) {
              p &= ~(1 << b[i][j]);
              --q;
              r -= a[b[i][j]];
            }
          }
        }
      }
    }
  }
  return mn;
}
int main() {
  int i, j;
  while (cin >> n >> u, n || u) {
    fill(b[0], b[0] + 20 * 20, -1);
    for (i = 0; i < n; ++i) {
      cin >> a[i];
      int m;
      cin >> m;
      for (j = 0; j < m; ++j)
        cin >> b[i][j];
    }
    fill(c, c + 20, 0);
    fill(d, d + (1 << 20), -1);
    cout << fi(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <queue>
using namespace std;

struct Data {
  int used;
  int credit;
};

int n, U, c, k, r;
int credit[20], required[20];

int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; }
int BFS() {
  bool visited[1 << 20] = {0};
  queue<Data> q;
  Data now = {0, 0};
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    if (visited[now.used])
      continue;
    visited[now.used] = true;
    if (now.credit >= U)
      return bitcount(now.used);
    for (int i = 0; i < n; i++) {
      if (now.used & (1 << i) || (now.used & required[i]) != required[i])
        continue;
      Data next = now;
      next.used ^= (1 << i);
      next.credit += credit[i];
      q.push(next);
    }
  }
  return -1;
}

int main() {
  while (cin >> n >> U, n | U) {
    for (int i = 0; i < n; i++) {
      cin >> c >> k;
      credit[i] = c;
      required[i] = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        required[i] ^= (1 << r);
      }
    }
    cout << BFS() << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

struct Data {
  int used;
  int credit;
};

int n, U, c, k, r;
int credit[20], required[20];

int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; }
int BFS() {
  bool visited[1 << 20] = {0};
  queue<Data> q;
  Data now = {0, 0};
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    if (visited[now.used])
      continue;
    visited[now.used] = true;
    if (now.credit >= U)
      return bitcount(now.used);
    for (int i = 0; i < n; i++) {
      if (now.used & (1 << i) || (now.used & required[i]) != required[i])
        continue;
      Data next = now;
      next.used ^= (1 << i);
      next.credit += credit[i];
      q.push(next);
    }
  }
  return -1;
}

int main() {
  while (cin >> n >> U, n | U) {
    for (int i = 0; i < n; i++) {
      cin >> c >> k;
      credit[i] = c;
      required[i] = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        required[i] ^= (1 << r);
      }
    }
    cout << BFS() << endl;
  }
}
Problem p00618 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int a, b, c;
  NODE(int x, int y, int z) { a = x, b = y, c = z; }
};

#define INF (1 << 21)
int n, U, unit[20];
char done[1 << 20];
int main() {
  while (cin >> n >> U, n) {

    int graph[21] = {0};
    memset(done, 0, sizeof(done));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> unit[i] >> k;
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        graph[i] |= (1 << a);
      }
    }
    queue<NODE> Q;
    Q.push(NODE(0, 0, 0));
    done[0] = 1;
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (q.b >= U) {
        cout << q.c << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i])
          if (!done[q.a | (1 << i)])
            Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1)),
                done[q.a | (1 << i)] = true;
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int a, b, c;
  NODE(int x, int y, int z) { a = x, b = y, c = z; }
};

#define INF (1 << 21)
int n, U, unit[20];
char done[1 << 20];
int main() {
  while (cin >> n >> U, n) {

    int graph[21] = {0};
    memset(done, 0, sizeof(done));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> unit[i] >> k;
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        graph[i] |= (1 << a);
      }
    }
    queue<NODE> Q;
    Q.push(NODE(0, 0, 0));
    done[0] = 1;
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (q.b >= U) {
        cout << q.c << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i])
          if (!done[q.a | (1 << i)])
            Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1));
      }
    }
  }
}