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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

int main() {
  int n;
  cin >> n;
  vector<vector<int>> A(n); //????????????????????????-1?????\\???????????????
  //??\\???
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    A[i] = vector<int>(k, 0);
    REP(j, k) {
      int l;
      cin >> l;
      A[i][j] = l - 1;
    }
  }
  //??¢?´¢
  vector<int> s;         //??????????????????????????\\???
  vector<int> dis(n, 0); //??????????????????????????????
  vector<int> fin(n, 0); //???????????????????????????
  int time = 0;
  while (find(dis.begin(), dis.end(), 0) != dis.end()) {
    int saisyo = find(dis.begin(), dis.end(), 0) - dis.begin();
    // cout<<saisyo<<endl;
    s.push_back(saisyo); //?????????0???????????????
    while (!s.empty()) {
      // REP(ss,3){
      int k = s.back(); //??????????????????????????????
      if (dis[k] == 0) {
        time++;
        dis[k] = time;
        // cout<<"dis "<<k<<" / time "<<time<<endl;
      }
      bool isfinish = true;
      for (int i : A[k]) {
        // cout<<"?????????"<<i<<endl;
        bool nai = find(s.begin(), s.end(), i) == s.end();
        if (fin[i] == 0 && i != k && nai) {
          s.push_back(i);
          isfinish = false;
          break;
        }
      }
      if (isfinish) {
        time++;
        fin[k] = time;
        s.pop_back();
        // cout<<"fin "<<k<<" / time "<<time<<endl;
      }
    }
  }
  REP(i, n) { cout << i + 1 << " " << dis[i] << " " << fin[i] << endl; }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

int main() {
  int n;
  cin >> n;
  vector<vector<int>> A(n); //????????????????????????-1?????\\???????????????
  //??\\???
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    A[i] = vector<int>(k, 0);
    REP(j, k) {
      int l;
      cin >> l;
      A[i][j] = l - 1;
    }
  }
  //??¢?´¢
  stack<int> s;          //??????????????????????????\\???
  vector<int> dis(n, 0); //??????????????????????????????
  vector<int> fin(n, 0); //???????????????????????????
  int time = 0;
  while (find(dis.begin(), dis.end(), 0) != dis.end()) {
    int saisyo = find(dis.begin(), dis.end(), 0) - dis.begin();
    // cout<<saisyo<<endl;
    s.push(saisyo); //?????????0???????????????
    while (!s.empty()) {
      // REP(ss,3){
      int k = s.top(); //??????????????????????????????
      if (dis[k] == 0) {
        time++;
        dis[k] = time;
        // cout<<"dis "<<k<<" / time "<<time<<endl;
      }
      bool isfinish = true;
      for (int i : A[k]) {
        // cout<<"?????????"<<i<<endl;
        if (fin[i] == 0 && i != k) {
          s.push(i);
          isfinish = false;
          break;
        }
      }
      if (isfinish) {
        time++;
        fin[k] = time;
        s.pop();
        // cout<<"fin "<<k<<" / time "<<time<<endl;
      }
    }
  }
  REP(i, n) { cout << i + 1 << " " << dis[i] << " " << fin[i] << endl; }
  return 0;
}