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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n && n != 0) {
    set<int> schedules[101];
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        int p;
        cin >> p;
        schedules[i].insert(p);
      }
      // sort(schedules[i].begin(),schedules[i].end());
    }
    // ツつサツづェツつシツづェツつェツつ「ツづ慊づづ個地ツ図ツづーツ篠敖づづつ「ツづゥツつゥ
    set<int> having[101];
    for (int i = 0; i < n; i++)
      having[i].insert(i);
    int idx = 31;
    for (int i = 1; i <= 30; i++) {
      vector<int> v;
      set<int> s;
      for (int j = 0; j < n; j++) {
        if (schedules[j].find(i) != schedules[j].end()) {
          v.push_back(j);
          for (set<int>::iterator it = having[j].begin(); it != having[j].end();
               it++)
            s.insert(*it);
        }
      }
      if (s.size() == n) {
        idx = i;
        break;
      }
      for (int j = 0; j < v.size(); j++)
        having[v[j]] = s;
    }
    if (idx == 31)
      cout << -1 << endl;
    else
      cout << idx << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

////
///dp[i][j]->iúÚÉjÌÆ±ëÉWßé±ÆÌÅ«én}ÌÅå
int having[101];
vector<int> schedules[101];
const int INF = 100000000;

int minDays;
int n;
void dfs(int d) {
  if (d > 31)
    return;

  vector<int> emp;
  vector<int> bks;
  int sum = 0;
  // ¡ñXPW[ªó¢Ä¢élÌXgðì¬
  for (int i = 0; i < n; i++) {
    if (having[i] == 0 ||
        !binary_search(schedules[i].begin(), schedules[i].end(), d))
      continue;
    sum += having[i];
    bks.push_back(having[i]);
    having[i] = 0;
    emp.push_back(i);
  }
  if (bks.size() == 0) {
    dfs(d + 1);
    return;
  }

  // »ê¼êÉÚµÏ¦ÄÝé
  for (int i = 0; i < emp.size(); i++) {
    int to = emp[i];
    having[to] = sum;
    if (having[to] == n) {
      minDays = min(minDays, d);
    }
    // ÄA
    // ÌÔÑÖ
    vector<int>::iterator it =
        upper_bound(schedules[to].begin(), schedules[to].end(), d);
    if (it != schedules[to].end()) {
      dfs(*it);
    }
    having[to] = 0;
  }
  // zñð³Éß·
  for (int i = 0; i < emp.size(); i++)
    having[emp[i]] = bks[i];
  return;
}

int main() {

  while (cin >> n && n != 0) {
    minDays = INF;
    for (int i = 0; i < n; i++) {
      having[i] = 1;
      schedules[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        int a;
        cin >> a;
        if (a >= 31)
          continue;
        schedules[i].push_back(a);
      }
      sort(schedules[i].begin(), schedules[i].end());
    }
    dfs(1);
    if (minDays == INF)
      cout << -1 << endl;
    else
      cout << minDays << endl;

    // dfs();
    // bool no=true;
    //// 30úÚÜÅTõ
    // for(int i = 1; i <= 30; i++){
    //     for(int j = 0; j < n; j++){

    //    }
    //}

    // if(no)
    //     cout<<-1<<endl;
    // else{

    //}
  }

  return 0;
}