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

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

int costs[32];
int N, U, C, K;
int ans;
pair<int, vector<int>> pivv[32];

void dfs(int now, int nowcost, int cnt, int next) {
  if (ans < cnt)
    return;
  if (nowcost >= U) {
    ans = cnt;
    return;
  }
  for (int i = next; i < N; i++) {
    if (!(now & (1 << i))) {
      int j;
      for (j = 0; j < pivv[i].second.size(); j++)
        if (!(now & (1 << pivv[i].second[j])))
          break;
      if (j == pivv[i].second.size()) {
        dfs(now | (1 << i), nowcost + pivv[i].first, cnt + 1, i + 1);
      }
    }
  }
  return;
}

int main() {
  while (cin >> N >> U, N | U) {
    pair<int, vector<int>> piv[N];
    // vector<vector<int> > vv(N);
    memset(costs, 0, sizeof(costs));
    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);
      }
    }

    // sort
    pair<int, vector<int>> tmp[N], pivtmp[N];
    vector<int> cv;
    int check[N];
    memset(check, 0, sizeof(check));
    for (int i = 0; i < N; i++)
      pivtmp[i] = piv[i];

    for (int i = 0; i < N; i++) {
      int pos = -1, tmin = N + 1;
      for (int j = 0; j < N; j++) {
        int k;
        int c = 0;
        int n = pivtmp[j].second.size();

        for (k = 0; k < n; k++) {
          int l;
          for (l = 0; l < i; l++)
            if (cv[l] == pivtmp[j].second[k])
              break;
          if (l < i || l == 0)
            c++;
        }

        if (c == n && tmin > n && !check[j]) {
          pos = j;
          tmin = n;
        }
      }

      // printf("i:%d , pos:%d\\n",i,pos);
      for (int j = 0; j < N; j++) {
        if (check[j])
          continue;
        for (int l = 0; l < pivtmp[j].second.size(); l++) {
          if (pivtmp[j].second[l] == pos) {
            piv[j].second[l] = i;
            // printf("j:%d\\n",j);
          }
        }
      }
      tmp[i] = piv[pos];
      check[pos] = 1;
      cv.push_back(pos);

      // __ debug __
      /*
      puts("_____debug_____");
      for(int j=0; j<N; j++){
              printf("i:%d, C:%d _ ",i,piv[j].first);
              for(int k=0; k<piv[j].second.size(); k++)
                      printf("%d,",piv[j].second[k]);
              puts("");
      }
      puts("_______________");
      */
    }

    /*
    for(int i=0; i<N; i++){
            printf("i:%d, C:%d _ ",i,tmp[i].first);
            for(int j=0; j<tmp[i].second.size(); j++)
                    printf("%d,",tmp[i].second[j]);
            puts("");
    }
    */
    for (int i = 0; i < N; i++)
      pivv[i] = tmp[i];
    dfs(0, 0, 0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int costs[32];
int N, U, C, K;
int ans;

void dfs(vector<vector<int>> &vv, int now, int nowcost, int cnt) {
  if (ans < cnt)
    return;
  if (nowcost >= U) {
    ans = cnt;
    return;
  }
  for (int i = 0; i < N; i++) {
    if (!(now & (1 << i))) {
      int j;
      for (j = 0; j < vv[i].size(); j++)
        if (!(now & (1 << vv[i][j])))
          break;
      if (j == vv[i].size()) {
        dfs(vv, now | (1 << i), nowcost + costs[i], cnt + 1);
      }
    }
  }
  return;
}

int main() {
  while (cin >> N >> U, N | U) {
    vector<vector<int>> vv(N);
    memset(costs, 0, sizeof(costs));
    ans = INT_MAX;
    for (int i = 0; i < N; i++) {
      cin >> costs[i] >> K;
      for (int j = 0; j < K; j++) {
        int a;
        cin >> a;
        vv[i].push_back(a);
      }
    }
    dfs(vv, 0, 0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}