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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n;
  while (cin >> n, n) {
    int in[50][30] = {0};
    rep(i, n) {
      int a;
      cin >> a;
      rep(j, a) {
        int b;
        cin >> b;
        in[i][b - 1] = true;
      }
    }
    vvi dp(n, vi(31, 0));
    rep(i, n) dp[i][0] = (ll)1 << i;
    int out = -1;
    rep(i, 30) {
      ll t = -1;
      rep(j, n) if (in[j][i]) {
        if (t + 1)
          dp[j][i + 1] = t;
        else {
          t = 0;
          rep(k, n) if (in[k][i]) t |= dp[k][i];
          if (t == ((ll)1 << n) - 1) {
            out = i + 1;
            break;
          }
          dp[j][i + 1] = t;
        }
      }
      else dp[j][i + 1] = dp[j][i];
      if (out + 1)
        break;
    }
    cout << out << endl;
    // rep(i,n){rep(j,5)cout<<" "<<dp[i][j];cout<<endl;}
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return 0;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y], data[y] = x;
    return 1;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int n;
  while (cin >> n, n) {
    vvi in(n, vi(31, 0));
    UnionFind UF(n);
    rep(i, n) {
      int a;
      cin >> a;
      rep(j, a) {
        int b;
        cin >> b;
        in[i][b - 1] = true;
      }
    }
    int h = -1;
    rep(i, 30) {
      vi a;
      rep(j, n) if (in[j][i]) a.pb(j);
      rep(j, a.size() - 1) UF.unionSet(a[0], a[j + 1]);
      if (UF.size(0) == n) {
        h = i + 1;
        break;
      }
    }
    cout << h << endl;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

const int INF = 1e8;

signed main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int d[51][31] = {};
    rep(i, 0, n) {
      int m;
      cin >> m;
      rep(j, 0, m) {
        int a;
        cin >> a;
        a--;
        d[i][a] = 1;
      }
    }
    int ans = INF;
    rep(j, 0, n) {
      int vis[51] = {};
      rep(i, 0, n) vis[i] = ((int)1 << i);
      rep(i, 0, 30) {
        rep(l, 0, n) {
          rep(x, 0, n) {
            if (d[l][i] && d[x][i]) {
              vis[l] |= vis[x];
              vis[x] |= vis[l];
            }
          }
        }
        // cout << j << "  " << i << " " << vis[j] << endl;
        if (vis[j] == ((int)1 << n) - 1) {
          ans = min(ans, i + 1);
          break;
        }
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

struct UF {
  vector<int> par;
  vector<int> sz;
  UF() {}
  UF(int n) {
    par.resize(n);
    sz.resize(n, 1);
    rep(i, 0, n) par[i] = i;
  }
  int find(int x) {
    if (x == par[x])
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

signed main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int d[51][31] = {};
    rep(i, 0, n) {
      int m;
      cin >> m;
      rep(j, 0, m) {
        int a;
        cin >> a;
        a--;
        d[i][a] = 1;
      }
    }
    int ans = -1;
    rep(i, 0, 30) {
      rep(j, 0, n) {
        bool f[51] = {};
        f[j] = true;
        repb(k, i, 0) {
          rep(l, 0, n) {
            rep(x, 0, n) {
              if (f[l] || f[x]) {
                if (d[l][k] && d[x][k]) {
                  f[l] = 1;
                  f[x] = 1;
                }
              }
            }
          }
        }
        bool g = true;
        rep(k, 0, n) if (f[k] == false) g = false;
        if (g) {
          ans = i + 1;
          j = n + 1;
          i = 31;
          break;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long N;
// vector<int> A[51];
long long P[51];
vector<int> D[51];

void solve() {

  for (int n = 0; n < N; n++) {
    int F;
    cin >> F;
    for (int f = 0; f < F; f++) {
      int a;
      cin >> a;
      D[a].push_back(n);
    }
  }
  for (long long n = 0; n < N; n++) {
    P[n] = static_cast<long long>(1) << n;
  }
  for (int d = 0; d < 31; d++) {
    for (auto n2 : D[d]) {
      for (auto n : D[d]) {
        P[n2] = (P[n2] | P[n]);
      }
      if (P[n2] == (static_cast<long long>(1) << N) - 1) {
        cout << d << endl;
        return;
      }
    }
  }
  cout << "-1" << endl;
}

int main() {
  cin >> N;
  while (N) {
    for (int i = 0; i < 51; i++) {
      D[i] = {};
    }
    solve();
    cin >> N;
  }
}


Error: TLE

Fixed submission:

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

int N;
// vector<int> A[51];
long long P[51];
vector<int> D[51];

void solve() {
  for (int n = 0; n < N; n++) {
    int F;
    cin >> F;
    for (int f = 0; f < F; f++) {
      int a;
      cin >> a;
      D[a].push_back(n);
    }
  }
  for (int n = 0; n < N; n++)
    P[n] = 1 << n;
  for (int d = 0; d < 31; d++) {
    for (auto n2 : D[d]) {
      for (auto n : D[d]) {
        P[n2] = (P[n2] | P[n]);
      }
      if (P[n2] == (1 << N) - 1) {
        cout << d << endl;
        return;
      }
    }
  }
  cout << "-1" << endl;
}

int main() {
  for (int i = 0; i < 51; i++) {
    D[i] = {};
  }
  cin >> N;
  while (N) {
    solve();
    cin >> N;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

int main() {
  for (int n; scanf("%d", &n), n;) {
    bool sche[50][31] = {};
    rep(i, n) {
      int f;
      scanf("%d", &f);
      rep(j, f) {
        int d;
        scanf("%d", &d);
        sche[i][d] = true;
      }
    }

    int ans = -1;
    ll maps[50];
    rep(i, n) maps[i] = 1LL << i;
    for (int t = 1; t <= 30; t++) {
      ll gather = 0;
      rep(i, n) if (sche[i][t]) gather |= maps[i];
      rep(i, n) if (sche[i][t]) maps[i] = gather;
      if (gather == (1LL << n) - 1) {
        ans = t;
        break;
      }
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <vector>

#define pb push_back

using namespace std;

typedef vector<int> vi;

int meet[50];

int UF_root(int i) { return (meet[i] == -1) ? i : meet[i] = UF_root(meet[i]); }
void UF_union(int i, int j) { meet[UF_root(j)] = UF_root(i); }
bool UF_find(int i, int j) { return UF_root(i) == UF_root(j); }

int main() {
  for (int n; scanf("%d", &n), n;) {
    vi day[31];
    for (int i = 0; i < n; i++) {
      int f;
      scanf("%d", &f);
      for (int j = 0; j < f; j++) {
        int d;
        scanf("%d", &d);
        day[d].pb(i);
      }
      meet[i] = -1;
    }

    int t, cnt = 0;
    for (t = 1; t <= 30; t++) {
      for (int i = 0; i < day[t].size() - 1; i++) {
        if (!UF_find(day[t][i], day[t][i + 1])) {
          UF_union(day[t][i], day[t][i + 1]);
          cnt++;
        }
      }
      if (cnt == n - 1)
        break;
    }

    printf("%d\\n", t < 31 ? t : -1);
  }
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, s, t;

  while (1) {
    cin >> n;
    if (!n)
      break;

    bool sch[30][51];
    set<int> max[51];

    for (int i = 0; i < n; i++) {
      for (int h = 0; h < 30; h++)
        sch[h][i] = false;
      max[i].insert(i + 1);
    }

    for (int i = 0; i < n; i++) {
      cin >> s;
      for (int j = 0; j < s; j++) {
        cin >> t;
        sch[t - 1][i] = true;
      }
    }

    set<int> maps;

    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < n; j++) {
        if (sch[i][j]) {
          maps.insert(max[j].begin(), max[j].end());
          // for(int k=j;k<n;k++){
          // if(sch[i][k]){
          // cout<<i<<" "<<k<<endl;
          // max[i].insert(max[k].begin(),max[k].end());
          // max[k].insert(max[i].begin(),max[i].end());
          //}
          //}
        }
      }
      if (maps.size() == n) {
        cout << i + 1 << endl;
        goto found;
      }

      for (int j = 0; j < n; j++) {
        if (sch[i][j])
          max[j].insert(maps.begin(), maps.end());
      }

      if (i == 29)
        cout << -1 << endl;
      maps.clear();
    }

  found:;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, s, t;

  while (1) {
    cin >> n;
    if (!n)
      break;

    bool sch[30][n];
    set<int> max[n];
    bool meeted[n][n];

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        meeted[i][j] = false;
        if (i == j)
          meeted[i][j] == true;
      }

    for (int i = 0; i < n; i++) {
      for (int h = 0; h < 30; h++)
        sch[h][i] = false;
      max[i].insert(i + 1);
    }

    for (int i = 0; i < n; i++) {
      cin >> s;
      for (int j = 0; j < s; j++) {
        cin >> t;
        sch[t - 1][i] = true;
      }
    }

    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < n; j++) {
        if (max[j].size() == n) {
          cout << i + 1 << endl;
          goto found;
        }
        if (sch[i][j]) {
          for (int k = j; k < n; k++) {
            if (sch[i][k]) {
              max[i].insert(max[k].begin(), max[k].end());
              max[k].insert(max[i].begin(), max[i].end());
            }
          }
        }
      }
      if (i == 29)
        cout << -1 << endl;
    }

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

#include <iostream>
#define N 50
#define F 31
using namespace std;

int main() {
  int n, f, a, flag, ans;
  bool d[N][F], c[N][F];
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < F; j++)
        d[i][j] = c[i][j] = false;
    for (int i = 0; i < n; i++) {
      cin >> f;
      for (int j = 0; j < f; j++)
        cin >> a, d[i][a] = c[i][a] = true;
    }
    ans = -1;
    for (int i = 1; i < F; i++) {
      flag = 0;
      for (int j = 0; j < n; j++) {
        if (!d[j][i])
          flag = 1;
        if (c[j][i])
          for (int k = i + 1; k < F; k++)
            if (c[j][k]) {
              for (int l = 0; l < n; l++)
                d[l][k] = d[l][i] | d[l][k];
              break;
            }
      }
      if (!flag && ans == -1)
        ans = i;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define N 50
#define F 31
using namespace std;
void func(int d);
int cal();
int n, f, a, ans;
bool d[N][F], c[F], choose[F];
vector<int> v[F];

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < F; j++)
        d[i][j] = false;
    for (int i = 0; i < n; i++) {
      cin >> f;
      for (int j = 0; j < f; j++)
        cin >> a, d[i][a] = true;
    }
    for (int i = 1; i < F; i++) {
      for (int j = 1; j < F; j++)
        c[j] = false;
      for (int j = 0; j < n; j++)
        if (d[j][i])
          for (int k = i + 1; k < F; k++) {
            if (c[k])
              continue;
            if (d[j][k])
              v[i].push_back(k), c[k] = true;
          }
    }
    ans = N;
    for (int i = 1; i < F; i++) {
      for (int j = 1; j < F; j++)
        choose[j] = false;
      choose[i] = true;
      func(i);
      choose[i] = false;
      func(i);
    }
    if (ans == N)
      cout << -1 << endl;
    else
      cout << ans << endl;
    for (int i = 0; i < n; i++)
      v[i].clear();
  }
  return 0;
}

void func(int d) {
  for (int i = 0; i < (int)v[d].size(); i++) {
    choose[v[d][i]] = true;
    ans = min(ans, cal());
    func(v[d][i]);
    choose[v[d][i]] = false;
    ans = min(ans, cal());
    func(v[d][i]);
  }
}

int cal() {
  int ret = N, finished[N], cnt = 0;
  for (int i = 0; i < n; i++)
    finished[i] = false;
  for (int i = 1; i < F; i++) {
    if (!choose[i])
      continue;
    for (int j = 0; j < n; j++) {
      if (!finished[j] && d[j][i])
        cnt++, finished[j] = true;
      if (cnt == n) {
        ret = i;
        break;
      }
    }
    if (cnt == n)
      break;
  }
  return ret;
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, a, b;
  vector<vector<int>> V;
  vector<int> G;
  bool map[50][50];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    V.clear();
    V.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> a;
      for (int j = 0; j < a; j++) {
        cin >> b;
        V[i].push_back(b);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          map[i][j] = true;
        else
          map[i][j] = false;
      }
    }

    for (int k = 1; k < 31; k++) {
      G.clear();
      for (int i = 0; i < V.size(); i++) {
        for (int j = 0; j < V[i].size(); j++) {
          if (k == V[i][j]) {
            G.push_back(i);
            break;
          }
        }
      }

      for (int i = 1; i < G.size(); i++) {
        for (int j = 0; j < n; j++) {
          if (map[G[i]][j])
            map[G[0]][j] = true;
        }
      }
      for (int i = 1; i < G.size(); i++) {
        for (int j = 0; j < n; j++) {
          map[G[i]][j] = map[G[0]][j];
        }
      }
      bool flag = false;
      for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = 0; j < n; j++) {
          if (map[i][j])
            sum++;
        }
        if (sum == n) {
          cout << k << endl;
          flag = true;
          break;
        }
      }
      if (flag)
        break;
      if (k == 30)
        cout << "-1" << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int map[51][51], color[51], n, HA[51][31];
void dfs(int u) {
  color[u] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (map[i][j] != 0 && color[j] == 0) {
        dfs(i);
      }
    }
  }
  color[u] = 2;
}
int main() {
  int max;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> HA[i][0];
      color[i] = 0;
      for (int j = 1; j <= HA[i][0]; j++) {
        cin >> HA[i][j];
      }
    }
    max = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j < HA[i][0] + 1; j++) {
        for (int k = i + 1; k < n; k++) {
          for (int l = 1; l < HA[k][0] + 1; l++) {
            if (HA[i][j] == HA[k][l]) {
              map[i][k] = HA[i][j];
              map[k][i] = HA[i][j];
              if (max < HA[i][j])
                max = HA[i][j];
            }
          }
        }
      }
    }
    dfs(0);
    for (int i = 0; i < n; i++) {
      if (color[i] != 2) {
        cout << "-1" << endl;
        break;
      }
      if (i == n - 1)
        cout << max << endl;
    }
  }
  return 0;
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

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

int n;
vector<vector<int>> f;

class data {
public:
  bool map[64];
  int last;

  data() {
    for (int i = 0; i < 64; i++) {
      map[i] = false;
    }
    last = 0;
  }

  bool judge() {
    for (int i = 0; i < n; i++) {
      if (map[i] == false)
        return false;
    }

    return true;
  }
};

void init() { f.clear(); }

bool input() {
  int tmp;
  cin >> n;
  if (!n)
    return false;

  f.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    for (int j = 0; j < tmp; j++) {
      int tmp2;
      cin >> tmp2;
      f[i].push_back(tmp2);
    }
  }

  return true;
}

int solve() {
  vector<data> vd;
  vd.resize(n);

  for (int i = 0; i < n; i++)
    vd[i].map[i] = true;

  for (int date = 1; date < 31; date++) {
    vector<int> tmp;
    for (int i = 0; i < f.size(); i++) {
      for (int j = 0; j < f[i].size(); j++) {
        if (date == f[i][j])
          tmp.push_back(i);
      }
    }

    if (tmp.size() > 1) {
      for (int i = 0; i < tmp.size(); i++) {
        for (int j = 0; j < tmp.size(); j++)
          vd[tmp[i]].map[tmp[j]] = true;
        for (int j = 0; j < n; j++) {
          for (int k = 0; k < tmp.size(); k++) {
            vd[tmp[i]].map[j] = vd[tmp[i]].map[j] | vd[tmp[k]].map[j];
          }
        }
        if (vd[tmp[i]].judge())
          return date;
      }
    }
  }

  return -1;
}

int main(void) {
  while (init(), input()) {
    cout << solve() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int n;
vector<vector<int>> f;

void init() { f.clear(); }

bool input() {
  int tmp;
  cin >> n;
  if (!n)
    return false;

  f.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    for (int j = 0; j < tmp; j++) {
      int tmp2;
      cin >> tmp2;
      f[i].push_back(tmp2);
    }
  }
  return true;
}

int solve() {
  bool table[64];
  int true_num = 0;
  int ret;
  for (int i = 0; i < 64; i++) {
    table[i] = false;
  }

  int i, j, k;
  for (i = 0; i < 31; i++) {
    for (j = 0; j < f.size(); j++) {
      for (k = 0; k < f[j].size() && f[j][k] < i; k++)
        ;
      if (f[j][k] == i) {
        if (table[j] == false) {
          true_num++;
          table[j] = true;
          ret = i;
        }
      }
    }
  }
  if (true_num != n)
    return -1;

  return ret;
}

int main(void) {
  while (init(), input()) {
    cout << solve() << endl;
  }
  return 0;
}
Problem p01136 (C++) — buggy submission, verdict: Time Limit Exceeded

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

unsigned long long b[100], c[100][31];
#define bit(n) (1LL << (n))

int main() {
  int n;
  while (cin >> n, n) {
    memset(c, 0, sizeof(c));
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      b[i] = 0;
      while (m--) {
        int t;
        cin >> t;
        b[i] |= bit(t);
      }
      c[i][0] = bit(i);
    }
    unsigned res = -1u;
    for (unsigned d = 1; d <= 30; ++d) {
      for (int i = 0; i < n; ++i) {
        c[i][d] = c[i][d - 1];
        if (b[i] & bit(d))
          for (int j = 0; j < n; ++j)
            if ((b[j] & bit(d)))
              c[i][d] |= c[j][d - 1];
      }
      for (int i = 0; i < n; ++i)
        if (c[i][d] == bit(n) - 1)
          res = min(res, d);
    }
    cout << (int)res << endl;
  }
}

Error: TLE

Fixed submission:

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

unsigned n, a[100], res;
unsigned long long b[100];
#define bit(n) (1LL << (n))

void dfs(int k, unsigned d, unsigned m) {
  if (d > 30 or d > res)
    return;
  for (int i = 0; i < n; ++i)
    if (b[i] & bit(d))
      m |= bit(i);
  if (m == bit(n) - 1) {
    res = min(res, d + 1);
    return;
  }
  for (int i = d + 1; i < 30; ++i)
    if (b[k] & bit(i)) {
      for (int j = 0; j < n; ++j)
        if (j != k && (b[j] & bit(i)))
          dfs(j, i, m);
    }
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      b[i] = 0;
      while (m--) {
        int t;
        cin >> t;
        b[i] |= bit(t - 1);
      }
    }
    res = -1u;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 30; ++j)
        if (b[i] & bit(j)) {
          dfs(i, j, 0);
          break;
        }
    }
    cout << (int)res << endl;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long STATES;

int main(void) {
  STATES free_days[31], dp[51], goal_state, next;
  STATES *p;
  int i, j, n, free_day, k;
  short answer;

  while (scanf("%d", &n) == 1 && n) {
    memset(free_days, 0, sizeof(free_days));
    memset(dp, 0, sizeof(dp));

    goal_state = 0;
    for (i = 0; i < n; i++)
      goal_state |= 1LL << i;

    for (i = 0; i < n; i++) {
      scanf("%d", &k);
      for (j = 0; j < k; j++) {
        scanf("%d", &free_day);
        free_days[free_day] |= 1LL << i;
      }
    }

    answer = -1;
    for (i = 0; i < n; i++)
      dp[i] = 1LL << i;
    p = free_days + 1;
    for (i = 1; i <= 30; i++, p++) {
      next = 0;
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          next |= dp[j];
      if (next == goal_state) {
        answer = i;
        break;
      }
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          dp[j] |= next;
    }

    printf("%d\\n", answer);
  }

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long STATES;

int main(void) {
  STATES free_days[31], dp[51], goal_state, next;
  STATES *p;
  short int i, j, n, answer, free_day, k;

  while (scanf("%d", &n) == 1 && n) {
    memset(free_days, 0, sizeof(free_days));
    memset(dp, 0, sizeof(dp));

    goal_state = 0;
    for (i = 0; i < n; i++)
      goal_state |= 1LL << i;

    for (i = 0; i < n; i++) {
      scanf("%d", &k);
      for (j = 0; j < k; j++) {
        scanf("%d", &free_day);
        free_days[free_day] |= 1LL << i;
      }
    }

    answer = -1;
    for (i = 0; i < n; i++)
      dp[i] = 1LL << i;
    p = free_days + 1;
    for (i = 1; i <= 30; i++, p++) {
      next = 0;
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          next |= dp[j];
      if (next == goal_state) {
        answer = i;
        break;
      }
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          dp[j] |= next;
    }

    printf("%d\\n", answer);
  }

  return 0;
}