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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 305;

struct edge {
  int st;
  vi vec;
  edge(){};
  edge(int a, vi b) { st = a, vec = b; }
};

vector<edge> eda;
int memo[MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, e, t;
  cin >> n >> e >> t;
  --t;
  fill(memo, memo + n, INF);
  rep(i, n) {
    int a;
    cin >> a;
    if (a) {
      memo[i] = 1;
    }
  }
  rep(i, e) {
    int a, b;
    vi hoge;
    cin >> a >> b;
    rep(j, b) {
      int c;
      cin >> c;
      hoge.pb(c - 1);
    }
    eda.emplace_back(a - 1, hoge);
  }
  while (1) {
    bool update = false;
    rep(i, e) {
      edge &e = eda[i];
      vi hoge;
      bool ok = true;
      each(it, e.vec) {
        if (memo[it] == INF) {
          ok = false;
        }
        hoge.pb(memo[it]);
      }
      if (ok) {
        int val = len(hoge);
        sort(all(hoge));
        rep(j, len(hoge)) { cmx(val, hoge[j] + len(hoge) - 1 - j); }
        if (val < memo[e.st]) {
          memo[e.st] = val;
          update = true;
        }
      }
    }
    if (!update)
      break;
  }
  if (memo[t] == INF) {
    cout << "-1\\n";
  } else {
    cout << memo[t] << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 305;

vvi G[MAX_N];
int memo[MAX_N];

int dfs(int nw, vector<bool> &flag) {
  if (flag[nw]) {
    flag[nw] = false;
    return -1;
  }
  flag[nw] = true;
  if (memo[nw]) {
    flag[nw] = false;
    return memo[nw];
  }
  int mn = INF;
  rep(i, len(G[nw])) {
    bool ok = true;
    vi kekka;
    int sz = len(G[nw][i]);
    rep(j, sz) {
      int res = dfs(G[nw][i][j], flag);
      if (res < 0) {
        ok = false;
        break;
      }
      kekka.pb(res);
    }
    if (ok) {
      int val = sz;
      sort(all(kekka));
      rep(j, len(kekka)) { cmx(val, kekka[j] + sz - 1 - j); }
      cmn(mn, val);
    }
  }
  if (mn == INF) {
    memo[nw] = -1;
  } else {
    memo[nw] = mn;
  }
  flag[nw] = false;
  return memo[nw];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, e, t;
  cin >> n >> e >> t;
  --t;
  vi w(n);
  rep(i, n) { cin >> w[i]; }
  rep(i, e) {
    int a, b;
    vi hoge;
    cin >> a >> b;
    rep(j, b) {
      int c;
      cin >> c;
      hoge.pb(c - 1);
    }
    G[a - 1].pb(hoge);
  }
  rep(i, n) {
    if (w[i])
      memo[i] = 1;
  }
  vector<bool> flag(n, false);
  cout << dfs(t, flag) << "\\n";
  return 0;
}