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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define REP(i, N) for (int i = 0; i < (int)(N); ++i)

int unit[20];
int need[20];

int nextInt() {
  int r = 0, ch;
  while (isspace(ch = getchar()))
    ;
  do {
    r = r * 10 + ch - '0';
  } while (isdigit(ch = getchar()));
  return r;
}

int best;
int N, U;
int tno;
int gain[20];
char visited[1 << 20];
void dfs(int mask, int one, int have) {
  if (best <= one)
    return;
  if (have >= U) {
    best = one;
    return;
  }
  if (have + gain[best - one - 1] < U)
    return;

  for (int i = 0; i < N; i++)
    if (visited[mask | (1 << i)] != tno && (mask | need[i]) == mask) {
      visited[mask | (1 << i)] = tno;
      dfs(mask | (1 << i), one + 1, have + unit[i]);
    }
}

int main() {
  for (tno = 1;; tno++) {
    N = nextInt();
    U = nextInt();
    if (N == 0 && U == 0)
      break;
    REP(i, N) need[i] = 0;

    int all = 0;
    REP(i, N) {
      unit[i] = nextInt();
      all += unit[i];
      gain[i] = unit[i];
      int K = nextInt();
      REP(k, K) {
        int x = nextInt();
        need[i] |= (1 << x);
      }
    }

    sort(gain, gain + N);
    reverse(gain, gain + N);
    for (int i = 1; i < N; i++)
      gain[i] += gain[i - 1];

    if (all == U) {
      printf("%d\\n", N);
      continue;
    }

    best = N;
    dfs(0, 0, 0);
    printf("%d\\n", best);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define REP(i, N) for (int i = 0; i < (int)(N); ++i)

int unit[20];
int need[20];

int nextInt() {
  int r = 0, ch;
  while (isspace(ch = getchar()))
    ;
  do {
    r = r * 10 + ch - '0';
  } while (isdigit(ch = getchar()));
  return r;
}

int best;
int N, U;
int tno;
// char visited[1 << 20];
void dfs(int mask, int one, int have) {
  if (best <= one)
    return;
  if (have >= U) {
    best = one;
    return;
  }

  for (int i = 0; i < N; i++)
    if ( // visited[mask | (1 << i)] != tno &&
        (mask | need[i]) == mask) {
      //         visited[mask | (1 << i)] = tno;
      dfs(mask | (1 << i), one + 1, have + unit[i]);
    }
}

int main() {
  for (tno = 1;; tno++) {
    N = nextInt();
    U = nextInt();
    if (N == 0 && U == 0)
      break;
    REP(i, N) need[i] = 0;

    int all = 0;
    REP(i, N) {
      unit[i] = nextInt();
      all += unit[i];
      int K = nextInt();
      REP(k, K) {
        int x = nextInt();
        need[i] |= (1 << x);
      }
    }
    if (all == U) {
      printf("%d\\n", N);
      continue;
    }
    best = N;
    dfs(0, 0, 0);
    printf("%d\\n", best);
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int popcount32(unsigned int x) {
  x = (x >> 1 & 0x55555555) + (x & 0x55555555);
  x = (x >> 2 & 0x33333333) + (x & 0x33333333);
  x = (x >> 4 & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
  x = (x >> 8 & 0x00ff00ff) + (x & 0x00ff00ff);
  return (x >> 16) + (x & 0x0000ffff);
}

int main() {
  int tanni[20];
  int prelearn[20];
  int n, u;
  while (cin >> n >> u, n | u) {
    memset(tanni, 0, sizeof(tanni));
    memset(prelearn, 0, sizeof(prelearn));
    for (int i = 0; i < n; i++) {
      int c, k;
      cin >> c >> k;
      tanni[i] = c;
      for (int j = 0; j < k; j++) {
        int r;
        cin >> r;
        prelearn[i] |= 1 << r;
      }
    }
    int ans = n;
    for (int i = 0; i < (1 << n); i++) {
      int pop = popcount32(i);
      if (ans <= pop)
        continue;
      int pre = 0;
      for (int j = 0; j < n; j++) {
        pre |= (i >> j & 1) * prelearn[j];
      }
      if ((i & pre) != pre)
        continue;
      int s = 0;
      for (int j = 0; j < n; j++) {
        s += (i >> j & 1) * tanni[j];
      }
      if (u <= s)
        ans = pop;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int popcount32(unsigned int x) {
  x = (x >> 1 & 0x55555555) + (x & 0x55555555);
  x = (x >> 2 & 0x33333333) + (x & 0x33333333);
  x = (x >> 4 & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
  x = (x >> 8 & 0x00ff00ff) + (x & 0x00ff00ff);
  return (x >> 16) + (x & 0x0000ffff);
}

int main() {
  int tanni[20];
  int prelearn[20];
  int n, u;
  while (cin >> n >> u, n | u) {
    memset(tanni, 0, sizeof(tanni));
    memset(prelearn, 0, sizeof(prelearn));
    for (int i = 0; i < n; i++) {
      int c, k;
      cin >> c >> k;
      tanni[i] = c;
      for (int j = 0; j < k; j++) {
        int r;
        cin >> r;
        prelearn[i] |= 1 << r;
      }
    }
    int ans = n;
    for (int i = 0; i < (1 << n); i++) {
      int pop = popcount32(i);
      if (ans <= pop)
        continue;
      int pre = 0;
      int s = 0;
      for (int j = 0; j < n; j++) {
        if ((i >> j & 1)) {
          s += tanni[j];
          pre |= prelearn[j];
        }
      }
      if ((i & pre) == pre && u <= s)
        ans = pop;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

vector<int> cv, pv;
int n, U;

bool visited[1 << 20];

struct state {
  int used, cost;
  state(int used, int cost) : used(used), cost(cost) { ; }
};

int pop(int x) {
  int n = 0;
  while (x != 0) {
    n++;
    x &= (x - 1);
  }
  return n;
}

int bfs() {
  queue<state> Q;
  Q.push(state(0, 0));
  int i;

  while (!Q.empty()) {
    state ns = Q.front();
    Q.pop();
    if (visited[ns.used])
      continue;
    if (ns.cost >= U) {
      return pop(ns.used);
    }

    visited[ns.used] = 1;

    for (i = 0; i < n; ++i) {
      if (!(ns.used & (1 << i)) && (ns.used & pv[i]) == pv[i]) {
        Q.push(state(ns.used | (1 << i), ns.cost + cv[i]));
      }
    }
  }
  return 0;
}

int main() {
  int i, j, k, c, r, tmp;
  while (cin >> n >> U, n | U) {
    cv.clear();
    pv.clear();
    pv.resize(n);

    memset(visited, false, sizeof(visited));
    rep(i, n) {
      cin >> c >> k;
      cv.push_back(c);
      tmp = 0;
      rep(j, k) {
        cin >> r;
        tmp |= (1 << r);
      }
      pv[i] = tmp;
    }

    cout << bfs() << endl;
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

// vector<int> cv,pv;
int cv[20], pv[20];
int n, U;

bool visited[1 << 20];

struct state {
  int used, cost;
  state(int used, int cost) : used(used), cost(cost) { ; }
};

int pop(int x) {
  int n = 0;
  while (x != 0) {
    n++;
    x &= (x - 1);
  }
  return n;
}

int bfs() {
  queue<state> Q;
  Q.push(state(0, 0));
  int i;

  while (!Q.empty()) {
    state ns = Q.front();
    Q.pop();
    if (visited[ns.used])
      continue;
    if (ns.cost >= U) {
      return pop(ns.used);
    }

    visited[ns.used] = 1;

    for (i = 0; i < n; ++i) {
      if (!(ns.used & (1 << i)) && (ns.used & pv[i]) == pv[i]) {
        Q.push(state(ns.used | (1 << i), ns.cost + cv[i]));
      }
    }
  }
  return 0;
}

int main() {
  int i, j, k, c, r, tmp;
  while (cin >> n >> U, n | U) {
    //        cv.clear();
    //        pv.clear();
    //        pv.resize(n);

    memset(visited, false, sizeof(visited));
    rep(i, n) {
      cin >> c >> k;
      cv[i] = c;
      //            cv.push_back(c);
      tmp = 0;
      rep(j, k) {
        cin >> r;
        tmp |= (1 << r);
      }
      pv[i] = tmp;
    }

    cout << bfs() << endl;
  }
}
Problem p00618 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <deque>
#include <iostream>
using namespace std;

struct Pox {
  int state, cost;
  Pox(int state, int cost) : state(state), cost(cost) {}
};

int main() {
  while (1) {
    int n, U, k, r;
    scanf("%d %d", &n, &U);
    if (n + U == 0)
      break;
    int mincost[1 << n];
    for (int i = 0; i < (1 << n); i++)
      mincost[i] = (1 << n);
    deque<Pox> deq;
    int G[n];
    int c[n];
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &c[i], &k);
      G[i] = 0;

      for (int j = 0; j < k; j++) {
        scanf("%d", &r);
        G[i] |= (1 << r);
      }
    }

    deq.push_back(Pox(0, 0));
    mincost[0] = 0;
    int ans = (1 << n);
    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      int num = __builtin_popcount(pox.state);
      if (num >= ans)
        continue;

      if (pox.cost >= U) {
        ans = min(ans, num);
        continue;
      }

      for (int i = 0; i < n; i++) {
        if ((pox.state >> i) & 1)
          continue;
        int pre = G[i];
        pre &= pox.state;
        if (pre != G[i])
          continue;
        if (mincost[pox.state | (1 << i)] <= num + 1)
          continue;
        mincost[pox.state | (1 << i)] = num + 1;
        if (pox.cost + c[i] < U)
          deq.push_back(Pox(pox.state | (1 << i), pox.cost + c[i]));
        else
          ans = min(ans, num + 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <deque>
#include <iostream>
using namespace std;

struct Pox {
  int state, cost;
  Pox(int state, int cost) : state(state), cost(cost) {}
};

int main() {
  while (1) {
    int n, U, k, r;
    scanf("%d %d", &n, &U);
    if (n + U == 0)
      break;
    deque<Pox> deq;
    int G[n];
    int c[n];
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &c[i], &k);
      G[i] = 0;
      if (!k)
        deq.push_back(Pox((1 << i), c[i]));
      for (int j = 0; j < k; j++) {
        scanf("%d", &r);
        G[i] |= (1 << r);
      }
    }

    int ans = n;
    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      int num = __builtin_popcount(pox.state);
      if (num >= ans)
        continue;
      for (int i = 0; i < n; i++) {
        if ((pox.state >> i) & 1)
          continue;
        int pre = G[i];
        pre &= pox.state;
        if (pre != G[i])
          continue;
        if (pox.cost + c[i] < U)
          deq.push_back(Pox(pox.state | (1 << i), pox.cost + c[i]));
        else
          ans = min(ans, num + 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

vector<int> cv, pv;
int n, U;

bool visited[1 << 20] = {};

struct state {
  int used, cost;
  state(int used, int cost) : used(used), cost(cost) { ; }
};

int pop(int x) {
  int n = 0;
  while (x != 0) {
    n++;
    x &= (x - 1);
  }
  return n;
}

int bfs() {
  queue<state> Q;
  Q.push(state(0, 0));
  int i;

  while (!Q.empty()) {
    state ns = Q.front();
    Q.pop();
    if (visited[ns.used])
      continue;
    if (ns.cost >= U) {
      return pop(ns.used);
    }

    visited[ns.used] = 1;

    for (i = 0; i < n; ++i) {
      if (!(ns.used & (1 << i)) && (ns.used & pv[i]) == pv[i]) {
        Q.push(state(ns.used | (1 << i), ns.cost + cv[i]));
      }
    }
  }
  return 0;
}

int main() {
  int i, j, k, c, r, tmp;
  while (cin >> n >> U, n | U) {
    cv.clear();
    pv.clear();
    pv.resize(n);

    rep(i, 1 << n) visited[i] = false;
    rep(i, n) {
      cin >> c >> k;
      cv.push_back(c);
      tmp = 0;
      rep(j, k) {
        cin >> r;
        tmp |= (1 << r);
      }
      pv[i] = tmp;
    }

    cout << bfs() << endl;
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

vector<int> cv, pv;
int n, U;

bool visited[1 << 20];

struct state {
  int used, cost;
  state(int used, int cost) : used(used), cost(cost) { ; }
};

int pop(int x) {
  int n = 0;
  while (x != 0) {
    n++;
    x &= (x - 1);
  }
  return n;
}

int bfs() {
  queue<state> Q;
  Q.push(state(0, 0));
  int i;

  while (!Q.empty()) {
    state ns = Q.front();
    Q.pop();
    if (visited[ns.used])
      continue;
    if (ns.cost >= U) {
      return pop(ns.used);
    }

    visited[ns.used] = 1;

    for (i = 0; i < n; ++i) {
      if (!((ns.used >> i) & 1) && (ns.used & pv[i]) == pv[i]) {
        Q.push(state((ns.used | (1 << i)), ns.cost + cv[i]));
      }
    }
  }

  assert(false);
}

int main() {
  int i, j, k, c, r, tmp;
  while (cin >> n >> U, n | U) {
    cv.clear();
    pv.clear();
    pv.resize(n);
    memset(visited, false, sizeof(visited));

    rep(i, n) {
      cin >> c >> k;
      cv.push_back(c);
      tmp = 0;
      rep(j, k) {
        cin >> r;
        tmp |= (1 << r);
      }
      pv[i] = tmp;
    }

    cout << bfs() << endl;
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

int need[30];
int tani[30];

int main() {
  int n, u;
  for (;;) {
    cin >> n >> u;
    if (n == 0 && u == 0)
      break;
    rep(i, n) {
      int k;
      cin >> tani[i] >> k;
      int bit = 0;
      rep(j, k) {
        int r;
        cin >> r;
        bit |= 1 << r;
      }
      need[i] = bit;
    }
    int res = n;
    rep(bit, 1 << n) {
      int koma = __builtin_popcount(bit);
      if (res <= koma)
        continue;
      int sum = 0, b = bit;
      while (b) {
        int clz = 31 - __builtin_clz(b);
        if ((bit & need[clz]) != need[clz])
          goto aaa;
        b ^= 1 << clz;
        sum += tani[clz];
      }
      if (sum < u)
        continue;
      res = koma;
    aaa:;
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

int need[30];
int tani[30];

int main() {
  int n, u;
  for (;;) {
    cin >> n >> u;
    if (n == 0 && u == 0)
      break;
    rep(i, n) {
      int k;
      cin >> tani[i] >> k;
      int bit = 0;
      rep(j, k) {
        int r;
        cin >> r;
        bit |= 1 << r;
      }
      need[i] = bit;
    }
    int res = n;
    rep(bit, 1 << n) {
      int koma = __builtin_popcount(bit);
      if (res <= koma)
        continue;
      int sum = 0;
      rep(i, n) if (bit & 1 << i) {
        if ((bit & need[i]) != need[i])
          goto aaa;
        sum += tani[i];
      }
      if (sum < u)
        continue;
      res = koma;
    aaa:;
    }
    cout << res << endl;
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

template <unsigned T> bool nextBitset(bitset<T> &bs, int digit) {
  bitset<T> x, y, z;
  x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);
  y = bs.to_ulong() + x.to_ulong() + 0ULL;
  z = bs & ~y;
  if (bs[digit - 1] && bs == z)
    return false;
  bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;
  bs |= y;
  return true;
}

int solve(int u, vector<int> &unit, vector<bitset<20>> condition) {
  int n = unit.size();

  int a = 1;
  int b = n;
  while (a < b) {
    int c = (a + b) / 2;
    bitset<20> bs((1 << c) - 1);
    bool ok = false;
    do {
      int tmp = 0;
      bitset<20> bs1;
      for (int j = 0; j < n; ++j) {
        if (bs[j]) {
          tmp += unit[j];
          bs1 |= condition[j];
        }
      }
      if (tmp < u || (~bs & bs1).any())
        continue;
      b = c;
      ok = true;
      break;
    } while (nextBitset(bs, n));

    if (!ok)
      a = c + 1;
  }

  return a;
}

int main() {
  for (;;) {
    int n, u;
    cin >> n >> u;
    if (n == 0)
      return 0;

    vector<int> unit(n);
    vector<bitset<20>> condition(n);
    for (int i = 0; i < n; ++i) {
      cin >> unit[i];
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int tmp;
        cin >> tmp;
        condition[i][tmp] = true;
      }
    }

    cout << solve(u, unit, condition) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int solve(int u, vector<int> &unit, vector<bitset<20>> condition) {
  int n = unit.size();

  int ret = INT_MAX;
  for (int i = 0; i < (1 << n); ++i) {
    bitset<20> bs(i);
    int num = bs.count();
    if (num >= ret)
      continue;

    int tmp = 0;
    bitset<20> bs1;
    for (int j = 0; j < n; ++j) {
      if (bs[j]) {
        tmp += unit[j];
        bs1 |= condition[j];
      }
    }
    if (tmp < u || (~bs & bs1).any())
      continue;

    ret = bs.count();
  }

  return ret;
}

int main() {
  for (;;) {
    int n, u;
    cin >> n >> u;
    if (n == 0)
      return 0;

    vector<int> unit(n);
    vector<bitset<20>> condition(n);
    for (int i = 0; i < n; ++i) {
      cin >> unit[i];
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int tmp;
        cin >> tmp;
        condition[i][tmp] = true;
      }
    }

    cout << solve(u, unit, condition) << endl;
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

const int INF = 1 << 30;

typedef struct {
  int c, r;
} Subject;

int n, U;
Subject sub[20];
int memo[1 << 20];

int solve(int bit, int cnt) {
  if (memo[bit] != -1)
    return memo[bit];

  if (cnt >= U) {
    memo[bit] = __builtin_popcount(bit);
    return memo[bit];
  }

  int ret = INF;
  for (int i = 0; i < n; ++i) {
    if (!(bit & (1 << i)) && (sub[i].r & bit) == sub[i].r)
      ret = min(ret, solve(bit | (1 << i), cnt + sub[i].c));
  }
  memo[bit] = ret;
  return ret;
}

int main() {
  while (~scanf("%d %d", &n, &U)) {
    if (n == 0 && U == 0)
      break;

    for (int i = 0; i < (1 << 20); ++i)
      memo[i] = -1;

    for (int i = 0; i < n; ++i) {
      sub[i].r = 0;
      int k, r = 0;
      scanf("%d %d", &sub[i].c, &k);
      for (int j = 0; j < k; ++j) {
        int r;
        scanf("%d", &r);
        sub[i].r |= (1 << r);
      }
    }
    printf("%d\\n", solve(0, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

const int INF = 1 << 30;

typedef struct {
  int c, r;
} Subject;

int n, U;
Subject sub[20];
bool memo[1 << 20];

int solve(int bit, int cnt) {
  if (memo[bit])
    return __builtin_popcount(bit);

  if (cnt >= U) {
    memo[bit] = true;
    return __builtin_popcount(bit);
  }

  int ret = INF;
  for (int i = 0; i < n; ++i) {
    if (!(bit & (1 << i)) && (sub[i].r & bit) == sub[i].r)
      ret = min(ret, solve(bit | (1 << i), cnt + sub[i].c));
  }
  return ret;
}

int main() {
  while (~scanf("%d %d", &n, &U)) {
    if (n == 0 && U == 0)
      break;

    for (int i = 0; i < (1 << 20); ++i)
      memo[i] = false;

    for (int i = 0; i < n; ++i) {
      sub[i].r = 0;
      int k, r = 0;
      scanf("%d %d", &sub[i].c, &k);
      for (int j = 0; j < k; ++j) {
        int r;
        scanf("%d", &r);
        sub[i].r |= (1 << r);
      }
    }
    printf("%d\\n", solve(0, 0));
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

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

using namespace std;

int main() {
  static int lg[1 << 20];
  rep(u, 20) lg[1 << u] = u;

  for (int n, lb; scanf("%d%d", &n, &lb), n;) {
    int score[20];
    bool wf[20][20] = {};
    rep(u, n) {
      int m;
      scanf("%d%d", score + u, &m);
      rep(i, m) {
        int v;
        scanf("%d", &v);
        wf[v][u] = true;
      }
    }

    rep(k, n) rep(i, n) rep(j, n) wf[i][j] |= wf[i][k] & wf[k][j];

    int a[20] = {};
    rep(u, n) rep(v, n) if (u != v && wf[v][u]) a[u] |= 1 << v;

    static int pc[1 << 20], sum[1 << 20], A[1 << 20];

    int ans = n;
    for (int S = 1; S < (1 << n); S++) {
      int T = S & ~(S & (-S));
      int lsb = lg[S & (-S)];
      pc[S] = pc[T] + 1;
      sum[S] = sum[T] + score[lsb];
      A[S] = A[T] | a[lsb];
      if ((S & A[S]) == A[S] && sum[S] >= lb)
        ans = min(ans, pc[S]);
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

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

using namespace std;

int main() {
  for (int n, U; scanf("%d%d", &n, &U), n;) {
    int score[20];
    bool wf[20][20] = {};
    rep(u, n) {
      int m;
      scanf("%d%d", score + u, &m);
      rep(i, m) {
        int v;
        scanf("%d", &v);
        wf[v][u] = true;
      }
    }

    rep(k, n) rep(i, n) rep(j, n) wf[i][j] |= wf[i][k] & wf[k][j];

    int A[20] = {}; // A[u] := m[h u ÌæcSÌ
    rep(u, n) rep(v, n) if (u != v && wf[v][u]) A[u] |= 1 << v;

    int ans = n;
    int nn = 1 << n;
    rep(S, nn) {
      int pc = 0, tot = 0;
      bool ok = true;
      rep(u, n) if (S & (1 << u)) {
        pc++;
        tot += score[u];
        if ((S & A[u]) != A[u]) {
          ok = false;
          break;
        }
      }
      if (ok && tot >= U)
        ans = min(ans, pc);
    }
    printf("%d\\n", ans);
  }

  return 0;
}