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

// 22:45~

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

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;

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

int dfs(const vvi &g, int u) {
  int res = 1 << u;
  for (int v : g[u])
    res |= dfs(g, v);
  return res;
}

int main() {
  // clock_t t0=clock();
  for (int n, m; ~scanf("%d%d", &n, &m) && n | m;) {
    array<int, 20> cs;
    vvi g(n);
    rep(i, n) {
      scanf("%d", &cs[i]);
      int k;
      scanf("%d", &k);
      rep(j, k) {
        int r;
        scanf("%d", &r);
        g[i].push_back(r);
      }
    }

    array<int, 20> bs;
    rep(i, n) bs[i] = dfs(g, i);

    static array<int, 1 << 20> sums;
    sums.fill(0);

    rep(i, n) sums[1 << i] = cs[i];
    rep(i, 1 << n) sums[i] = sums[i & -i] + sums[i - (i & -i)];

    int res = INF;
    rep(i, 1 << n) if (popcount(i) < res) {
      int b = 0;
      rep(j, n) b |= (i >> j & 1) * bs[j];
      if (sums[b] >= m)
        res = min(res, popcount(b));
    }
    printf("%d\\n", res);
  }
  // clock_t t1=clock();
  // printf("%.3f[s]\\n",1.*(t1-t0)/CLOCKS_PER_SEC);
}

Error: TLE

Fixed submission:

// 22:45~

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

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;

int dfs(const vvi &g, int u) {
  int res = 1 << u;
  for (int v : g[u])
    res |= dfs(g, v);
  return res;
}

int main() {
  // clock_t t0=clock();
  for (int n, m; ~scanf("%d%d", &n, &m) && n | m;) {
    array<int, 20> cs;
    vvi g(n);
    rep(i, n) {
      scanf("%d", &cs[i]);
      int k;
      scanf("%d", &k);
      rep(j, k) {
        int r;
        scanf("%d", &r);
        g[i].push_back(r);
      }
    }

    array<int, 20> bs;
    rep(i, n) bs[i] = dfs(g, i);

    static array<int, 1 << 20> sums;
    sums.fill(0);

    rep(i, n) sums[1 << i] = cs[i];
    rep(i, 1 << n) sums[i] = sums[i & -i] + sums[i - (i & -i)];

    int res = INF;
    rep(i, 1 << n) if (__builtin_popcount(i) < res) {
      int b = 0;
      rep(j, n) b |= (i >> j & 1) * bs[j];
      if (sums[b] >= m)
        res = min(res, __builtin_popcount(b));
    }
    printf("%d\\n", res);
  }
  // clock_t t1=clock();
  // printf("%.3f[s]\\n",1.*(t1-t0)/CLOCKS_PER_SEC);
}