Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF 1000000000
#define EPS 1e-9
#define PI acos(-1)

typedef long long ll;

#define MAX_N 51
#define MAX_F 31

vector<int> ans_list;

int main() {

  while (true) {

    int N;
    bool flag[MAX_N][MAX_F];
    memset(flag, 0, sizeof(flag));
    cin >> N;

    if (N == 0)
      break;

    for (int i = 1; i <= N; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int t;
        cin >> t;
        flag[i][t] = true;
      }
    }

    int ans = 0;

    bool have[MAX_N][MAX_N];
    memset(have, 0, sizeof(have));

    for (int i = 1; i <= N; i++) {
      have[i][i] = true;
    }

    for (ans = 1; ans <= 30; ans++) {
      bool S[MAX_N];
      memset(S, 0, sizeof(S));

      for (int i = 1; i <= N; i++) {
        if (flag[i][ans]) {
          for (int j = 1; j <= N; j++) {
            if (have[i][j]) {
              S[j] = true;
            }
          }
        }
      }

      int cnt = 0;
      for (int i = 1; i <= N; i++) {
        if (S[i])
          cnt++;
      }

      if (cnt == N)
        break;

      for (int i = 1; i <= N; i++) {
        if (flag[i][ans]) {
          for (int j = 1; j <= N; j++) {
            if (S[j]) {
              have[i][j] = true;
            }
          }
        }
      }
    }

    if (ans <= 30)
      ans_list.push_back(ans);
    else
      ans_list.push_back(-1);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF 1000000000
#define EPS 1e-9
#define PI acos(-1)

typedef long long ll;

#define MAX_N 51
#define MAX_F 31

vector<int> ans_list;

int main() {

  while (true) {
    int N;
    bool flag[MAX_N][MAX_F];
    memset(flag, 0, sizeof(flag));
    cin >> N;

    if (N == 0)
      break;

    for (int i = 1; i <= N; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int t;
        cin >> t;
        flag[i][t] = true;
      }
    }

    int ans = 0;
    ll dp[MAX_N];
    memset(dp, 0, sizeof(dp));

    for (int i = 1; i <= N; i++) {
      dp[i] = (1 << i) - 1;
    }

    for (ans = 1; ans <= 30; ans++) {

      ll S = 0;
      vector<int> tmp;

      for (int i = 1; i <= N; i++) {
        if (flag[i][ans]) {
          tmp.push_back(i);
          S |= dp[i];
        }
      }

      if (S == (1 << N) - 1)
        break;

      for (int i = 1; i <= tmp.size(); i++) {
        dp[tmp[i]] |= S;
      }
    }

    if (ans <= 30)
      ans_list.push_back(ans);
    else
      ans_list.push_back(-1);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}