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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define INF (1 << 26)
#define MAX 100
struct state {
  int a, b;
};

int n, a, b;
map<int, bool> f[MAX];
vector<int> G[MAX];
int t[MAX][MAX];
bool visited[MAX][MAX];

void solve(int a, int b) {
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      visited[i][j] = false;

  queue<state> Q;
  Q.push((state){a, b});
  visited[a][b] = true;

  while (!Q.empty()) {
    state s = Q.front();
    Q.pop();
    t[s.a][s.b]++;

    if (s.a + 1 < MAX && !visited[s.a + 1][s.b]) {
      visited[s.a + 1][s.b] = true;
      Q.push((state){s.a + 1, s.b});
    }

    if (!f[s.a][s.b])
      continue;
    for (int i = 0; i < (int)G[s.a].size(); i++) {
      state to = s;
      to.b = G[s.a][i];
      if (!visited[to.a][to.b]) {
        visited[to.a][to.b] = true;
        Q.push(to);
      }
    }
  }
}

void init() {
  for (int i = 0; i < MAX; i++) {
    G[i].clear();
    f[i].clear();
    for (int j = 0; j < MAX; j++) {
      t[i][j] = 0;
    }
  }
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();
    for (int i = 0; i < n; i++) {
      cin >> a;
      for (int j = 0; j < a; j++) {
        cin >> b;
        f[b][i] = true;
        G[b].push_back(i);
      }
    }

    for (int i = 0; i < n; i++)
      solve(0, i);

    int ans = INF;
    for (int i = 1; i <= 30; i++)
      for (int j = 0; j < n; j++)
        if (t[i][j] == n)
          ans = min(ans, i);
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define INF (1 << 26)
#define MAX 6
struct state {
  int a, b;
};

int n, a, b;
map<int, bool> f[MAX];
vector<int> G[MAX];
int t[MAX][MAX];
bool visited[MAX][MAX];

void solve(int a, int b) {
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      visited[i][j] = false;

  queue<state> Q;
  Q.push((state){a, b});
  visited[a][b] = true;

  while (!Q.empty()) {
    state s = Q.front();
    Q.pop();
    t[s.a][s.b]++;
    if (s.a + 1 < MAX && !visited[s.a + 1][s.b]) {
      visited[s.a + 1][s.b] = true;
      Q.push((state){s.a + 1, s.b});
    }

    if (!f[s.a][s.b])
      continue;
    for (int i = 0; i < (int)G[s.a].size(); i++) {
      state to = s;
      to.b = G[s.a][i];
      if (!visited[to.a][to.b]) {
        visited[to.a][to.b] = true;
        Q.push(to);
      }
    }
  }
}

void init() {
  for (int i = 0; i < MAX; i++) {
    G[i].clear();
    f[i].clear();
    for (int j = 0; j < MAX; j++) {
      t[i][j] = 0;
    }
  }
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();
    for (int i = 0; i < n; i++) {
      cin >> a;
      for (int j = 0; j < a; j++) {
        cin >> b;
        f[b][i] = true;
        G[b].push_back(i);
      }
    }

    for (int i = 0; i < n; i++)
      solve(1, i);

    int ans = INF;
    for (int i = 1; i <= 30; i++)
      for (int j = 0; j < n; j++)
        if (t[i][j] == n)
          ans = min(ans, i);
    cout << ans << endl;
  }
  return 0;
}