alexjercan/bugnet huggingface dataset:
Problem p00616 (C++) — buggy submission, verdict: Runtime Error

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

#define MP make_pair
#define PB push_back

int main(void) {
  int n, h;

  while (cin >> n >> h) {
    if ((n | h) == 0)
      break;
    string str;
    int p, q, ret = n * n * n;
    vector<pair<int, int>> xy, xz, yz;
    for (int i = 0; i < h; i++) {
      cin >> str >> p >> q;
      if (str == "xy") {
        xy.PB(MP(p, q));
      } else if (str == "xz") {
        xz.PB(MP(p, q));
      } else {
        yz.PB(MP(p, q));
      }
    }
    // xy
    ret -= xy.size() * n;
    // xz
    for (int i = 0; i < xz.size(); i++) {
      int tmp = n;
      for (int j = 0; j < xy.size(); j++) {
        if (xz[i].first == xy[j].first)
          tmp--;
      }
      ret -= tmp;
    }
    // yz
    for (int i = 0; i < yz.size(); i++) {
      int tmp = n;
      for (int j = 1; j <= n; j++) {
        bool dosub = false;
        for (int k = 0; k < xy.size(); k++) {
          if (j == xy[k].first && xy[k].second == yz[i].first) {
            dosub = true;
          }
        }
        for (int k = 0; k < xz.size(); k++) {
          if (j == xz[k].first && xz[k].second == yz[i].second) {
            dosub = true;
          }
        }
        if (dosub)
          tmp--;
      }
      ret -= tmp;
    }

    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

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

int main(void) {
  int n, h;

  while (cin >> n >> h) {
    if ((n | h) == 0)
      break;
    bool cube[n][n][n];
    string str;
    int p, q, ret = 0;
    memset(cube, false, sizeof(cube));

    for (int i = 0; i < h; i++) {
      cin >> str >> p >> q;
      if (str == "xy") {
        for (int j = 0; j < n; j++)
          cube[p - 1][q - 1][j] = true;
      } else if (str == "xz") {
        for (int j = 0; j < n; j++)
          cube[p - 1][j][q - 1] = true;
      } else {
        for (int j = 0; j < n; j++)
          cube[j][p - 1][q - 1] = true;
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          if (cube[i][j][k] == false)
            ret++;
    cout << ret << endl;
  }
}
Problem p00616 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int key(int a, int b, int c) { return a * 500 * 500 + b * 500 + c; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (int n, h; cin >> n >> h && n;) {
    unordered_set<int> st;
    rep(i, 0, h) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--, b--;
      if (c == "xy") {
        rep(j, 0, n) st.insert(key(a, b, j));
      } else if (c == "xz") {
        rep(j, 0, n) st.insert(key(a, j, b));
      } else {
        rep(j, 0, n) st.insert(key(j, a, b));
      }
    }
    cout << n * n * n - st.size() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

bool f[510][510][510] = {};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (int n, h; cin >> n >> h && n;) {
    memset(f, 0, sizeof(f));
    rep(i, 0, h) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--, b--;
      if (c == "xy") {
        rep(j, 0, n) f[a][b][j] = true;
      } else if (c == "xz") {
        rep(j, 0, n) f[a][j][b] = true;
      } else {
        rep(j, 0, n) f[j][a][b] = true;
      }
    }
    int ans = n * n * n;
    rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) { ans -= f[i][j][k]; }
    cout << ans << endl;
  }
  return 0;
}
Problem p00616 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, h;
  for (; cin >> n >> h, n;) {
    long long int sum = n * n * n;
    long long int cnt = 0;
    map<pair<pair<int, int>, int>, bool> hash;
    for (int i = 0; i < h; i++) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy")
        for (int i = 0; i < n; i++) {
          if (!hash[make_pair(make_pair(a, b), i)]) {
            hash[make_pair(make_pair(a, b), i)] = true;
            cnt++;
          }
        }
      else if (c == "xz")
        for (int i = 0; i < n; i++) {
          if (!hash[make_pair(make_pair(a, i), b)]) {
            hash[make_pair(make_pair(a, i), b)] = true;
            cnt++;
          }
        }
      else if (c == "yz")
        for (int i = 0; i < n; i++) {
          if (!hash[make_pair(make_pair(i, a), b)]) {
            hash[make_pair(make_pair(i, a), b)] = true;
            cnt++;
          }
        }
    }
    cout << sum - cnt << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, h;
  for (; cin >> n >> h, n;) {
    bool data[n][n][n];
    memset(data, true, sizeof(data));
    for (int i = 0; i < h; i++) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy")
        for (int i = 0; i < n; i++)
          data[a][b][i] = false;
      else if (c == "xz")
        for (int i = 0; i < n; i++)
          data[a][i][b] = false;
      else if (c == "yz")
        for (int i = 0; i < n; i++)
          data[i][a][b] = false;
    }
    int cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++) {
          if (data[i][j][k]) {
            //		   cout<<i<<" "<<j<<" "<<k<<endl;
            cnt++;
          }
        }
    cout << cnt << endl;
  }
}
Problem p00616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool t[3][500][500];
int n, h, x, y;
string s;
int main() {
  while (cin >> n >> h && n + h) {
    int cnt, ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++) {
          t[i][j][k] = 0;
        }
    }
    for (int i = 0; i < h; i++) {
      cin >> s >> x >> y;
      x--, y--;
      if (s == "xy") {
        t[0][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[1][x][i])
            cnt++;
          if (t[2][y][i])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "xz") {
        t[1][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][x][i])
            cnt++;
          if (t[2][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "yz") {
        t[2][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][i][x])
            cnt++;
          if (t[1][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
    }
    cout << n * (n * n - h) + ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool t[3][200][200];
int n, h, x, y;
string s;
int main() {
  while (cin >> n >> h && n + h) {
    int cnt, ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++) {
          t[i][j][k] = 0;
        }
    }
    for (int i = 0; i < h; i++) {
      cin >> s >> x >> y;
      x--, y--;
      if (s == "xy") {
        t[0][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[1][x][i])
            cnt++;
          if (t[2][y][i])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "xz") {
        t[1][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][x][i])
            cnt++;
          if (t[2][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "yz") {
        t[2][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][i][x])
            cnt++;
          if (t[1][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
    }
    cout << n * (n * n - h) + ans << endl;
  }
  return 0;
}
Problem p00616 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> PII;
typedef pair<int, PII> PIP;
typedef set<PIP> SET;

int main() {
  int n, h;
  while (cin >> n >> h && n && h) {
    SET S;
    for (int i = 0; i < h; ++i) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy") {
        for (int i = 0; i < n; ++i) {
          S.insert(PIP(a, PII(b, i)));
        }
      } else if (c == "xz") {
        for (int i = 0; i < n; ++i) {
          S.insert(PIP(a, PII(i, b)));
        }
      } else if (c == "yz") {
        for (int i = 0; i < n; ++i) {
          S.insert(PIP(i, PII(a, b)));
        }
      }
    }
    cout << n * n * n - S.size() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
bool U[500][500][500];

int main() {
  int n, h;
  while (cin >> n >> h) {
    if (n == 0 && h == 0)
      break;
    memset(U, false, sizeof(U));

    ll answer = n * n * n;
    for (int i = 0; i < h; i++) {
      string a;
      int b, c;
      cin >> a >> b >> c;

      // x, y, z
      if (a == "xy") {
        for (int j = 0; j < n; j++) {
          if (!U[b][c][j]) {
            answer--;
            U[b][c][j] = true;
          }
        }
      } else if (a == "yz") {
        for (int j = 0; j < n; j++) {
          if (!U[j][b][c]) {
            answer--;
            U[j][b][c] = true;
          }
        }
      } else if (a == "xz") {
        for (int j = 0; j < n; j++) {
          if (!U[b][j][c]) {
            answer--;
            U[b][j][c] = true;
          }
        }
      }
    }
    cout << answer << endl;
  }

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

#include "bits/stdc++.h"
using namespace std;

int main() {
  while (1) {
    int N, H;
    cin >> N >> H;
    if (!N && !H)
      break;
    set<tuple<int, int, int>> aset;
    for (int i = 0; i < H; ++i) {
      string st;
      int a, b;
      cin >> st >> a >> b;
      a--;
      b--;
      for (int j = 0; j < N; ++j) {
        if (st == "xy") {
          aset.insert(make_tuple(a, b, j));
        } else if (st == "xz") {
          aset.insert(make_tuple(a, j, b));
        } else {
          aset.insert(make_tuple(j, a, b));
        }
      }
    }
    int ans = N * N * N - aset.size();
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

int main() {
  while (1) {
    int N, H;
    cin >> N >> H;
    if (!N)
      break;
    vector<vector<vector<int>>> field(N,
                                      vector<vector<int>>(N, vector<int>(N)));
    for (int i = 0; i < H; ++i) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy") {
        for (int j = 0; j < N; ++j) {
          field[a][b][j] = true;
        }
      } else if (c == "xz") {
        for (int j = 0; j < N; ++j) {
          field[a][j][b] = true;
        }
      } else {
        for (int j = 0; j < N; ++j) {
          field[j][a][b] = true;
        }
      }
    }
    int ans = 0;
    for (int x = 0; x < N; ++x) {
      for (int y = 0; y < N; ++y) {
        for (int z = 0; z < N; ++z) {
          ans += field[z][y][x];
        }
      }
    }
    cout << N * N * N - ans << endl;
  }
  return 0;
}
Problem p00616 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <utility>
using namespace std;

int main() {
  int n, h;
  while (cin >> n >> h && (n || h)) {
    set<pair<pair<int, int>, int>> s;
    for (int i = 0; i < h; i++) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy") {
        for (int j = 0; j < n; j++)
          s.insert(make_pair(make_pair(a, b), j));
      } else if (c == "xz") {
        for (int j = 0; j < n; j++)
          s.insert(make_pair(make_pair(a, j), b));
      } else {
        for (int j = 0; j < n; j++)
          s.insert(make_pair(make_pair(j, a), b));
      }
    }
    cout << n * n * n - s.size() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

bool hole[500][500][500];

int main() {
  int n, h;
  while (cin >> n >> h && (n || h)) {
    memset(hole, false, sizeof(hole));
    for (int i = 0; i < h; i++) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy") {
        for (int j = 0; j < n; j++)
          hole[a][b][j] = true;
      } else if (c == "xz") {
        for (int j = 0; j < n; j++)
          hole[a][j][b] = true;
      } else {
        for (int j = 0; j < n; j++)
          hole[j][a][b] = true;
      }
    }
    int cnt = 0;
    for (int x = 0; x < n; x++)
      for (int y = 0; y < n; y++)
        for (int z = 0; z < n; z++)
          if (!hole[x][y][z])
            cnt++;
    cout << cnt << endl;
  }
  return 0;
}
Problem p00616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
#define mp make_pair

typedef pair<int, pair<int, int>> P;

int main() {
  while (true) {
    int N, ql;
    cin >> N >> ql;
    if (!N)
      return 0;
    set<P> ps;
    REP(qi, ql) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy")
        REP(z, N) ps.insert(mp(a, mp(b, z)));
      if (c == "xz")
        REP(y, N) ps.insert(mp(a, mp(y, b)));
      if (c == "yz")
        REP(x, N) ps.insert(mp(x, mp(a, b)));
    }
    cout << N * N * N - ps.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)

struct Q {
  string c;
  int x, y, z;
  Q() {}
  Q(string c_, int x_, int y_, int z_) {
    c = c_;
    x = x_;
    y = y_;
    z = z_;
  }
};

int main() {
  while (true) {
    int N, ql;
    cin >> N >> ql;
    if (!N)
      return 0;
    Q qs[ql];
    REP(qi, ql) {
      string c;
      int a, b;
      cin >> c >> a >> b;
      a--;
      b--;
      if (c == "xy")
        qs[qi] = Q(c, a, b, -1);
      if (c == "xz")
        qs[qi] = Q(c, a, -1, b);
      if (c == "yz")
        qs[qi] = Q(c, -1, a, b);
    }
    int re = 0;
    REP(x, N) REP(y, N) REP(z, N) {
      REP(qi, ql) {
        Q q = qs[qi];
        if ((q.c == "xy" && q.x == x && q.y == y) ||
            (q.c == "xz" && q.x == x && q.z == z) ||
            (q.c == "yz" && q.y == y && q.z == z)) {
          re++;
          break;
        }
      }
    }
    cout << INT(pow(N, 3)) - re << endl;
  }
}
Problem p00617 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Panel {
  string name;
  int x1, y1, x2, y2;
  Panel *parent;
  vector<Panel> child;

  void init() {
    name = "";
    x1 = y1 = x2 = y2 = 0;
    parent = NULL;
    child.clear();
  }
};

Panel list;
pair<string, int> ans;
int maxDepth;

void parse(string s) {
  stringstream ss;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == ',' || s[i] == '<' || s[i] == '>')
      s[i] = ' ';
  ss.str(s);

  stack<string> st;

  Panel *now = &list;
  ss >> list.name >> list.x1 >> list.y1 >> list.x2 >> list.y2;
  st.push(list.name);

  while (!st.empty()) {
    Panel p = {"", 0, 0, 0, 0, NULL};
    ss >> p.name;
    if (p.name[0] == '/') {
      st.pop();
      now = (*now).parent;
      continue;
    }
    st.push(p.name);
    p.parent = now;
    ss >> p.x1 >> p.y1 >> p.x2 >> p.y2;
    (*now).child.push_back(p);
    now = &(*now).child[(*now).child.size() - 1];
  }
}

void solve(Panel p, int x, int y, int depth) {
  if (x < p.x1 || p.x2 < x || y < p.y1 || p.y2 < y)
    return;
  if (maxDepth < depth) {
    maxDepth = depth;
    ans.first = p.name;
    ans.second = p.child.size();
  }
  vector<Panel>::iterator it;
  for (it = p.child.begin(); it != p.child.end(); it++)
    solve(*it, x, y, depth + 1);
}

int main() {
  string s;
  int n, x, y;
  while (cin >> n, n) {
    cin >> s;
    list.init();
    parse(s);
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      maxDepth = -1;
      solve(list, x, y, 0);
      if (maxDepth == -1) {
        ans.first = "OUT OF MAIN PANEL";
        ans.second = 1;
      }
      cout << ans.first << " " << ans.second << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Panel {
  string name;
  int x1, y1, x2, y2;
  vector<Panel> child;
};

int i = 0;
pair<string, int> ans;
int maxDepth;

Panel parse(string s) {
  Panel p = {"", 0, 0, 0, 0};
  for (i++; s[i] != '>'; i++)
    p.name += s[i];
  char c;
  stringstream ss;
  ss.str(s.substr(i + 1, s.size() - i - 1));
  ss >> p.x1 >> c >> p.y1 >> c >> p.x2 >> c >> p.y2;
  while (s[i] != '<')
    i++;
  while (s[i + 1] != '/')
    p.child.push_back(parse(s));
  while (s[i] != '>')
    i++;
  i++;
  return p;
}

void solve(Panel p, int x, int y, int depth) {}

int main() {
  int n, x, y;
  string s;
  while (cin >> n, n) {
    cin >> s;
    s = "<OUT OF MAIN PANEL>-1,-1,10001,10001" + s + "</OUT OF MAIN PANEL>";
    Panel p = parse(s);
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      maxDepth = -1;
      solve(p, x, y, 0);
      if (ans.first == "OUT OF MAIN PANEL")
        ans.second = 1;
      cout << ans.first << " " << ans.second << endl;
    }
  }
}
Problem p00618 (C++) — buggy submission, verdict: Memory 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;
}

char visited[1 << 20];

struct State {
  int one, mask, have;
};

int solve(int N, int U, int tno) {
  queue<State> up;
  static State s;
  s.one = s.mask = s.have = 0;
  visited[s.mask] = tno;
  up.push(s);
  while (!up.empty()) {
    s = up.front();
    up.pop();

    if (s.have >= U)
      return s.one;

    for (int i = 0; i < N; i++)
      if (visited[s.mask | (1 << i)] != tno && (s.mask | need[i]) == s.mask) {
        static State next;
        next.mask = s.mask | (1 << i);
        next.one = s.one + 1;
        next.have = s.have + unit[i];
        visited[next.mask] = tno;
        up.push(next);
      }
  }
  return -1;
}

int main() {
  for (int tno = 1;; tno++) {
    int N, U;
    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;
    }
    int best = solve(N, U, tno);
    printf("%d\\n", best);
  }
}

Error: MLE

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;
}

char visited[1 << 20];

struct State {
  int one, mask, have;
};

int solve(int N, int U, int tno) {
  queue<State> up;
  State s;
  s.one = s.mask = s.have = 0;
  visited[s.mask] = tno;
  up.push(s);
  while (!up.empty()) {
    s = up.front();
    up.pop();

    if (s.have >= U)
      return s.one;

    for (int i = 0; i < N; i++)
      if (visited[s.mask | (1 << i)] != tno && (s.mask | need[i]) == s.mask) {
        State next;
        next.mask = s.mask | (1 << i);
        next.one = s.one + 1;
        next.have = s.have + unit[i];
        visited[next.mask] = 1;
        up.push(next);
      }
  }
  return -1;
}

int main() {
  for (int tno = 1;; tno++) {
    int N, U;
    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;
    }
    int best = solve(N, U, tno);
    printf("%d\\n", best);
  }
}