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

#include <iostream>
#include <vector>

using namespace std;

int N, k[1000];
int v[1000][1000];
int found[1000] = {0}, ended[1000] = {0};
int keep;
int checking = 0;

void dfs(int cur);

int main() {
  cin >> N;
  for (int i = 1; i < N + 1; i++) {
    cin >> keep >> k[i];
    for (int j = 1; j < k[i] + 1; j++) {
      cin >> v[i][j];
    }
  }
  dfs(1);
  while (true) {
    // cout << "processing..." << endl;
    bool flag = false;
    int key = 1;
    for (int i = 1; i < N + 1; i++) {
      if (found[i] == 0) {
        flag = true;
        key = i;
        break;
      }
    }
    if (flag) {
      dfs(key);
    } else {
      break;
    }
  }

  for (int i = 1; i < N + 1; i++) {
    cout << i << " " << found[i] << " " << ended[i] << endl;
  }
}

void dfs(int cur) {
  // cout << "process ended" << endl;
  if (!found[cur]) {
    checking++;
    // cerr << "visiting: " << cur << " " << "time: " << checking << endl;
    found[cur] = checking;
  }
  for (int dst = 1; dst < N + 1; dst++) {
    bool flag = false;
    for (int i = 1; i < k[cur] + 1; i++) {
      if (v[cur][i] == dst)
        flag = true;
    }
    if ((flag) && (dst != cur) && (!found[dst]))
      dfs(dst);
  }
  if (!ended[cur]) {
    checking++;
    // cerr << "ended: " << cur << " " << "time: " << checking << endl;
    ended[cur] = checking;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int N, k[1000];
int v[1000][1000];
int found[1000] = {0}, ended[1000] = {0};
int keep;
int checking = 0;

void dfs(int cur);

int main() {
  cin >> N;
  for (int i = 1; i < N + 1; i++) {
    cin >> keep >> k[i];
    for (int j = 1; j < k[i] + 1; j++) {
      cin >> v[i][j];
    }
  }
  dfs(1);
  while (true) {
    // cout << "processing..." << endl;
    bool flag = false;
    int key = 1;
    for (int i = 1; i < N + 1; i++) {
      if (found[i] == 0) {
        flag = true;
        key = i;
        break;
      }
    }
    if (flag) {
      dfs(key);
    } else {
      break;
    }
  }

  for (int i = 1; i < N + 1; i++) {
    cout << i << " " << found[i] << " " << ended[i] << endl;
  }
}

void dfs(int cur) {
  bool REkirrer = false;
  for (int i = 1; i < N + 1; i++) {
    if (found[i] == 0) {
      REkirrer = true;
    }
  }
  if (!REkirrer)
    return;
  int loopcount = 0;
  // cout << "process ended" << endl;
  if (!found[cur]) {
    checking++;
    // cerr << "visiting: " << cur << " " << "time: " << checking << endl;
    found[cur] = checking;
  }
  for (int dst = 1; dst < N + 1; dst++) {
    loopcount++;
    bool flag = false;
    for (int i = 1; i < k[cur] + 1; i++) {
      if (v[cur][i] == dst)
        flag = true;
    }
    if ((flag) && (dst != cur))
      dfs(dst);
  }
  if (!ended[cur]) {
    checking++;
    // cerr << "ended: " << cur << " " << "time: " << checking << endl;
    ended[cur] = checking;
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>

const int MAX = 100;
static int time = 0;
int adj[MAX][MAX];
int discover[MAX];
int finish[MAX];

void visit(int node, int n) {
  ++time;
  discover[node] = time;

  for (int i = 0; i < n; ++i) {
    if (adj[node][i] == 1 && discover[i] == 0)
      visit(i, n);
  }

  ++time;
  finish[node] = time;
}

int main() {
  std::memset(adj, 0, sizeof(adj));
  std::memset(discover, 0, sizeof(discover));
  std::memset(finish, 0, sizeof(finish));

  int n;
  scanf("%d", &n);

  int u, k, v;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &v);
      adj[u - 1][v - 1] = 1;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (discover[i] == 0)
      visit(i, n);
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d %d\\n", i + 1, discover[i], finish[i]);

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

const int MAX = 100;
static int time = 0;
int adj[MAX][MAX];
int discover[MAX];
int finish[MAX];

void visit(int node, int n) {
  if (finish[node] != 0)
    return;

  ++time;
  discover[node] = time;

  for (int i = 0; i < n; ++i) {
    if (adj[node][i] == 1 && node != i)
      visit(i, n);
  }

  ++time;
  finish[node] = time;
}

int main() {
  std::memset(adj, 0, sizeof(adj));
  std::memset(discover, 0, sizeof(discover));
  std::memset(finish, 0, sizeof(finish));

  int n;
  scanf("%d", &n);

  int u, k, v;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &v);
      adj[u - 1][v - 1] = 1;
    }
  }

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

  for (int i = 0; i < n; ++i)
    printf("%d %d %d\\n", i + 1, discover[i], finish[i]);

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<int> l[101];
int matrix[100][100];
pair<int, int> t[101];
bool visited[101];
int tim = 0;
void dfs(int cur) {
  tim += 1;
  t[cur].first = tim;
  // cerr<<"visiting "<<cur<<' '<<tim<<endl;
  visited[cur] = true;

  for (int i = 0; i < l[cur].size(); i++) {
    if ((matrix[cur - 1][l[cur][i] - 1] == 1) &&
        (visited[l[cur][i]] == false)) {
      dfs(l[cur][i]);
    }
  }

  tim += 1;
  t[cur].second = tim;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int tmp;
      cin >> tmp;
      l[u].push_back(tmp);
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      matrix[i][j] = 0;
    }
  }

  visited[0] = false;
  for (int i = 1; i < n + 1; i++) {
    visited[i] = false;
    for (int j = 0; j < l[i].size(); j++) {
      matrix[i - 1][l[i][j] - 1] = 1;
    }
  }

  for (int i = 1; i < n + 1; i++) {
    if (visited[i] == false)
      dfs(i);
  }

  for (int i = 1; i < n + 1; i++) {
    cout << i << " " << t[i].first << " " << t[i].second << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<int> l[101];
int matrix[100][100];
pair<int, int> t[101];
bool visited[101];
int tim = 0;
void dfs(int cur) {
  tim += 1;
  t[cur].first = tim;
  // cerr<<"visiting "<<cur<<' '<<tim<<endl;
  visited[cur] = true;

  for (int i = 0; i < l[cur].size(); i++) {
    if ((matrix[cur - 1][l[cur][i] - 1] == 1) && (visited[i] == false)) {
      dfs(l[cur][i]);
    }
  }

  tim += 1;
  t[cur].second = tim;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int tmp;
      cin >> tmp;
      l[u].push_back(tmp);
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      matrix[i][j] = 0;
    }
  }
  for (int i = 1; i < n + 1; i++) {
    visited[i] = false;
    for (int j = 0; j < l[i].size(); j++) {
      matrix[i - 1][l[i][j] - 1] = 1;
    }
  }

  for (int i = 1; i < n + 1; i++) {
    if (visited[i] == false)
      dfs(i);
  }

  for (int i = 1; i < n + 1; i++) {
    cout << i << " " << t[i].first << " " << t[i].second << endl;
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

typedef struct Node {
  int id;
  int d;
  int f;
  int count;
} Node;

int count(int id, int now, Node *node, int a[101][100]) {
  if (node[id].d == 0)
    node[id].d = now;
  else
    return now - 1;
  if (node[id].f != 0)
    return now - 1;
  for (int i = 0; i < node[id].count; ++i) {
    int nextId = a[id][i];
    now = count(nextId, now + 1, node, a);
  }
  node[id].f = now + 1;
  return node[id].f;
}

int main() {
  int n;
  std::scanf("%d", &n);
  Node node[101];
  int a[101][100];
  for (int i = 1; i <= n; ++i) {
    int id;
    std::scanf("%d", &id);
    node[id].id = id;
    std::scanf("%d", &node[id].count);
    for (int j = 0; j < node[id].count; ++j) {
      std::scanf("%d", &a[id][j]);
    }
  }
  int now = 0;
  for (int i = 1; i <= n; ++i) {
    now = count(i, now + 1, node, a);
  }
  for (int i = 1; i <= n; ++i) {
    std::printf("%d %d %d\\n", node[i].id, node[i].d, node[i].f);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>

typedef struct Node {
  int id;
  int d;
  int f;
  int count;
} Node;

int count(int id, int now, Node *node, int a[101][100]) {
  if (node[id].d == 0)
    node[id].d = now;
  if (node[id].f != 0)
    return now - 1;
  for (int i = 0; i < node[id].count; ++i) {
    int nextId = a[id][i];
    if (nextId == id)
      continue;
    now = count(nextId, now + 1, node, a);
  }
  node[id].f = now + 1;
  return node[id].f;
}

int main() {
  int n;
  std::scanf("%d", &n);
  Node node[101];
  int a[101][100];
  for (int i = 1; i <= n; ++i) {
    int id;
    std::scanf("%d", &id);
    node[id].id = id;
    std::scanf("%d", &node[id].count);
    for (int j = 0; j < node[id].count; ++j) {
      std::scanf("%d", &a[id][j]);
    }
  }
  int now = 0;
  for (int i = 1; i <= n; ++i) {
    now = count(i, now + 1, node, a);
  }
  for (int i = 1; i <= n; ++i) {
    std::printf("%d %d %d\\n", node[i].id, node[i].d, node[i].f);
  }

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> graph[100];
int start[100], endt[100];
int cnt;

void dfs(int num) {
  start[num] = ++cnt;
  for (int i = 0; i < graph[num].size(); i++) {
    if (graph[num][i] != num && !start[graph[num][i]] && !endt[graph[num][i]])
      dfs(graph[num][i]);
  }
  endt[num] = ++cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int now, m;
    cin >> now >> m;
    for (int j = 0; j < m; j++) {
      int t;
      cin >> t;
      graph[now - 1].push_back(t - 1);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!endt[i])
      dfs(i);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << start[i] << " " << endt[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

vector<int> graph[100];
int start[100], endt[100];
int cnt;

void dfs(int num) {
  start[num] = ++cnt;
  for (int i = 0; i < graph[num].size(); i++) {
    if (graph[num][i] != num && !endt[graph[num][i]])
      dfs(graph[num][i]);
  }
  endt[num] = ++cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int now, m;
    cin >> now >> m;
    for (int j = 0; j < m; j++) {
      int t;
      cin >> t;
      graph[now - 1].push_back(t - 1);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!endt[i])
      dfs(i);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << start[i] << " " << endt[i] << endl;
  }
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[MAX];
int counter = 0;
int D[MAX];
int F[MAX];

void input() {
  cin >> num;
  for (int i = 0; i < MAX; i++) {
    dst[i] = -1;
    for (int j = 0; j < MAX; j++) {
      matrix[i][j] = 0;
    }
  }
  for (int i = 1; i <= num; i++) {
    int v;
    cin >> v;
    int tmp;
    cin >> tmp;
    for (int j = 0; j < tmp; j++) {
      int vi;
      cin >> vi;
      matrix[v - 1][vi - 1] = 1;
    }
  }
}

void dfs(int cur) {
  counter++;
  D[cur] = counter;
  for (int i = 0; i < num; i++) {
    if (matrix[cur][i] == 1 && D[i] == 0)
      dfs(i);
  }
  counter++;
  F[cur] = counter;
  return;
}

int check() {
  for (int i = 0; i < num; i++) {
    if (D[i] == 0)
      return i;
  }
  return -1;
}

int main() {
  input();
  for (int i = 0; i < MAX; i++) {
    D[i] = 0;
    F[i] = 0;
  }
  dfs(0);
  while (check() != -1) {
    int a = check();
    dfs(a);
  }
  for (int i = 0; i < num; i++) {
    cout << i + 1 << " " << D[i] << " " << F[i] << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <stack>

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[MAX];

void input() {
  cin >> num;
  for (int i = 0; i < MAX; i++) {
    dst[i] = -1;
    for (int j = 0; j < MAX; j++) {
      matrix[i][j] = 0;
    }
  }
  for (int i = 1; i <= num; i++) {
    int v;
    cin >> v;
    int tmp;
    cin >> tmp;
    for (int j = 0; j < tmp; j++) {
      int vi;
      cin >> vi;
      matrix[v - 1][vi - 1] = 1;
    }
  }
}

void dfs() {
  stack<int> S;
  S.push(0);
  dst[0] = 0;
  while (!S.empty()) {
    int cur = S.top();
    S.pop();
    if (dst[cur] == -1 || dst[cur] == 0) {
      for (int i = 0; i < num; i++) {
        if (matrix[cur][i] == 1) {
          S.push(i);
          dst[i] = dst[cur] + 1;
        }
      }
    } else if (dst[S.top()] + 1 < dst[cur]) {
      for (int i = 0; i < num; i++) {
        if (matrix[cur][i] == 1) {
          S.push(i);
          dst[i] = dst[cur] + 1;
        }
      }
    }
  }
  for (int i = 0; i < num; i++) {
    cout << i + 1 << " " << dst[i] << endl;
  }
}

int main() {
  input();
  dfs();
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long N = 100;
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[N + 1][N + 1];
long color[N + 1], d[N + 1], f[N + 1], tt;
long nt[N + 1];

long next(long u) {
  for (long v = nt[u]; v <= n; v++) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

void dfs_visit(long r) {
  for (long i = 1; i <= n; i++)
    nt[i] = 1;

  stack<long> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    long u = S.top();
    long v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  for (long i = 1; i <= n; i++) {
    color[i] = WHITE;
    nt[i] = 1;
  }
  tt = 0;

  for (long u = 1; u <= n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (long i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  long u, k, v;
  cin >> n;
  for (long i = 1; i <= n; i++) {
    for (long j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (long i = 1; i <= n; i++) {
    cin >> u >> k;
    for (long j = 1; j <= k; j++) {
      cin >> v;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}

Error: TLE

Fixed submission:

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

const long N = 100;
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[N + 1][N + 1];
long color[N + 1], d[N + 1], f[N + 1], tt;
long nt[N + 1];

long next(long u) {
  for (long v = nt[u]; v < n; v++) {
    nt[u] = v;
    if (M[u][v])
      return v;
  }
  return -1;
}

void dfs_visit(long r) {
  for (long i = 1; i <= n; i++)
    nt[i] = 0;

  stack<long> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    long u = S.top();
    long v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      } else {
        S.pop();
        color[u] = BLACK;
        f[u] = ++tt;
      }
    }
  }
}

void dfs() {
  for (long i = 1; i <= n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  for (long u = 1; u <= n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (long i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  long u, k, v;
  cin >> n;
  for (long i = 1; i <= n; i++) {
    for (long j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (long i = 1; i <= n; i++) {
    cin >> u >> k;
    for (long j = 1; j <= k; j++) {
      cin >> v;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;
const int MAX_N = 100;

int A[MAX_N + 1][MAX_N + 1];
pair<int, int> TIMESTAMPS[MAX_N + 1];
int N;

void solve() {
  int ts = 0;
  stack<int> S;

  for (int i = N; i > 0; i--) {
    if (TIMESTAMPS[i].first == 0)
      S.push(i);
  }

  while (!S.empty()) {
    int c = S.top();

    if (TIMESTAMPS[c].first == 0)
      TIMESTAMPS[c].first = ++ts;

    for (int i = N; i > 0; i--) {
      if (A[c][i] == 1 && TIMESTAMPS[i].first == 0)
        S.push(i);
    }

    if (S.top() == c) {
      if (TIMESTAMPS[c].second == 0)
        TIMESTAMPS[c].second = ++ts;
      S.pop();
    }
  }
}

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      A[i][j] = 0;
    }
    TIMESTAMPS[i] = make_pair(0, 0);
  }

  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      A[u][t] = 1;
    }
  }

  solve();

  for (int i = 1; i <= N; i++) {
    cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second
         << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;
const int MAX_N = 100;

int A[MAX_N + 1][MAX_N + 1];
pair<int, int> TIMESTAMPS[MAX_N + 1];
int N;

void solve() {
  int ts = 0;
  stack<int> S;

  for (int i = N; i > 0; i--) {
    if (TIMESTAMPS[i].first == 0)
      S.push(i);
  }

  while (!S.empty()) {
    int c = S.top();

    if (TIMESTAMPS[c].first == 0)
      TIMESTAMPS[c].first = ++ts;

    for (int i = N; i > 0; i--) {
      if (A[c][i] == 1 && TIMESTAMPS[i].second == 0)
        S.push(i);
    }

    if (S.top() == c) {
      if (TIMESTAMPS[c].second == 0)
        TIMESTAMPS[c].second = ++ts;
      S.pop();
    }
  }
}

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      A[i][j] = 0;
    }
    TIMESTAMPS[i] = make_pair(0, 0);
  }

  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      A[u][t] = 1;
    }
  }

  solve();

  for (int i = 1; i <= N; i++) {
    cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second
         << endl;
  }

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const int max_v = 101;
vector<vector<int>> adj_list;
int d[max_v] = {0};
int f[max_v] = {0};
int discovered[max_v] = {0};
int t = 1;

// Depth-first Search ( Stack implementation)
void DFS(int v) {
  stack<int> S;
  S.push(v);
  discovered[v] = 1;
  d[v] = t++;
  int i = 0;
  while (!S.empty()) {
    v = S.top();
    if (i < adj_list[v - 1].size()) {
      int u = adj_list[v - 1][i++];
      if (discovered[u] == 0) {
        discovered[u] = 1;
        d[u] = t++;
        S.push(u);
        i = 0;
      }
    } else {
      S.pop();
      f[v] = t++;
      i = 0;
    }
  }
}

int main(int argc, char **argv) {
  // Input
  int n;
  int u, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    int v;
    vector<int> adj;
    for (int j = 0; j < k; j++) {
      cin >> v;
      adj.push_back(v);
    }
    adj_list.push_back(adj);
  }

  // Depth-first Search
  for (int i = 1; i <= n; i++) {
    if (discovered[i] == 0)
      DFS(i);
  }

  // Output
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const int max_v = 100;
vector<vector<int>> adj_list;
int d[max_v] = {0};
int f[max_v] = {0};
int discovered[max_v] = {0};
int t = 1;

// Depth-first Search ( Stack implementation)
void DFS(int v) {
  stack<int> S;
  S.push(v);
  discovered[v] = 1;
  d[v] = t++;
  int i = 0;
  while (!S.empty()) {
    v = S.top();
    if (i < adj_list[v - 1].size()) {
      int u = adj_list[v - 1][i++];
      if (discovered[u] == 0) {
        discovered[u] = 1;
        d[u] = t++;
        S.push(u);
        i = 0;
      }
    } else {
      S.pop();
      f[v] = t++;
      i = 0;
    }
  }
}

int main(int argc, char **argv) {
  // Input
  int n;
  int u, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    int v;
    vector<int> adj;
    for (int j = 0; j < k; j++) {
      cin >> v;
      adj.push_back(v);
    }
    adj_list.push_back(adj);
  }

  // Depth-first Search
  for (int i = 1; i <= n; i++) {
    if (discovered[i] == 0)
      DFS(i);
  }

  // Output
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int visit[100], end[100], a[100][100];
int t, N, u, k, col;

void rist_to_mat(int N) {
  for (int l = 0; l < N; ++l) {
    cin >> u >> k;
    for (int i = 0; i < k; ++i) {
      cin >> col;
      a[u - 1][col - 1] = 1;
    }
  }
}

void dfs(int cur, int N) {
  t += 1;
  visit[cur] = t;
  for (int dst = 0; dst < N; ++dst) {
    if (a[cur][dst] == 1 && visit[dst] == 0) {
      dfs(dst, N);
    }
  }
  t += 1;
  end[cur] = t;
}

int main() {
  cin >> N;
  rist_to_mat(N);
  for (int i = 0; i < N; ++i) {
    if (visit[i] == 0)
      dfs(i, N);
  }
  for (int i = 0; i < N; ++i) {
    cout << i + 1 << ' ' << visit[i] << ' ' << end[i] << endl;
  }
}

Error: 0
visiting 0 1
visiting 1 2
visiting 3 3
visiting 2 4


Fixed submission:

#include <iostream>
using namespace std;

int a[100][100];
int n, p, q, r;
int T[100], F[100];
int Time = 0;
void dfs(int cur) {
  T[cur] = Time;
  Time += 1;
  cerr << "visiting " << cur << ' ' << Time << endl;
  for (int dst = 0; dst < n; ++dst) {
    if (a[cur][dst] == 1 && T[dst] == -1) {
      dfs(dst);
    }
  }
  F[cur] = Time;
  Time += 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> p >> q;
    T[i] = -1;
    for (int j = 0; j < q; ++j) {
      cin >> r;
      a[p - 1][r - 1] = 1;
    }
  }
  dfs(0);
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << T[i] + 1 << " " << F[i] + 1 << endl;
  }
}