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

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

static const int MAX_NUM = 100;

void dsf_cal(int matrix[][MAX_NUM + 1], int state[], int time[][2], int index,
             int num) {
  static int t = 1;
  stack<int> S;

  S.push(index);
  state[index] = 1;
  time[index][0] = t;
  ++t;

  while (S.empty() != true) {
    int i;
    int target = S.top();
    for (i = 1; i < num + 1; i++) {
      if (matrix[target][i] == 1) {
        if (state[i] == 0) {
          S.push(i);
          state[i] = 1;
          time[i][0] = t;
          ++t;
          break;
        }
      }
    }
    if (i == num + 1) {
      time[target][1] = t;
      ++t;
      S.pop();
    }
  }

  return;
}

void DSF(int matrix[][MAX_NUM + 1], int num) {
  int state[MAX_NUM + 1] = {0};
  int time[MAX_NUM + 1][2] = {0};

  for (int i = 1; i < num + 1; i++) {
    if (state[i] == 0) {
      dsf_cal(matrix, state, time, i, num);
    }
  }

  for (int i = 1; i < num + 1; i++) {
    printf("%d %d %d\\n", i, time[i][0], time[i][1]);
  }

  return;
}

int main(void) {
  int num;
  int index, elemNum, tmp;
  int matrix[MAX_NUM + 1][MAX_NUM + 1] = {0};

  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d %d", &index, &elemNum);
    for (int j = 0; j < elemNum; j++) {
      scanf("%d", &tmp);
      matrix[index][tmp] = 1;
    }
  }

  DSF(matrix, num);

  return 0;
}

Error: -11

Fixed submission:

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

static const int MAX_NUM = 100;

void dsf_cal(int matrix[][MAX_NUM + 1], int state[], int time[][2], int index,
             int num) {
  static int t = 0;
  stack<int> S;

  S.push(index);
  state[index] = 1;
  time[index][0] = t;
  ++t;

  while (S.empty() != true) {
    int i;
    int target = S.top();
    for (i = 0; i < num; i++) {
      if (state[i] == 0) {
        S.push(i);
        state[i] = 1;
        time[i][0] = t;
        ++t;
        break;
      }
    }
    if (i == num) {
      time[target][1] = t;
      ++t;
      S.pop();
    }
  }

  return;
}

void DSF(int matrix[][MAX_NUM + 1], int num) {
  int state[MAX_NUM] = {0};
  int time[MAX_NUM][2] = {0};

  for (int i = 0; i < num; i++) {
    if (state[i] == 0) {
      dsf_cal(matrix, state, time, i, num);
    }
  }

  for (int i = 0; i < num; i++) {
    printf("%d %d %d\\n", i, time[i][0], time[i][1]);
  }

  return;
}

int main(void) {
  int num;
  int index, elemNum;
  int matrix[MAX_NUM + 1][MAX_NUM + 1] = {0};

  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d %d", &index, &elemNum);
    for (int j = 0; j < elemNum; j++) {
      scanf("%d", matrix[i + 1][j]);
    }
  }

  DSF(matrix, num);

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

#include <iostream>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2

int G[101][101];
int color[101];
int n;
int d[101], f[101], t;

void search_loop(int i) {
  int next;
  d[i] = ++t;
  color[i] = GRAY;

  for (next = 1; next <= n; next++) {
    if (i == next && G[i][i] > 0)
      break;
    if (G[i][next] > 0 && color[next] == WHITE) {
      search_loop(next);
    }
  }
  f[i] = ++t;
  color[i] = BLACK;

  return;
}

void dfs() {
  int i;
  for (i = 1; i <= n; i++)
    color[i] = WHITE; // initialize.

  t = 0;
  for (i = 1; i <= n; i++) {
    if (color[i] == WHITE)
      search_loop(i);
  }
  for (i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
  return;
}

int main() {
  int i, j, k, e;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      G[i][j] = 0;
    }
  }
  do {
    cin >> i >> e;
    for (j = 1; j <= e; j++) {
      cin >> k;
      G[i][k] = 1;
    }
  } while (i < n);

  dfs();

  cin >> n;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2

int G[101][101];
int color[101];
int n;
int d[101], f[101], t;

void search_loop(int i) {
  int next;
  d[i] = ++t;
  color[i] = GRAY;

  for (next = 1; next <= n; next++) {
    if (i == next && G[i][i] > 0)
      break;
    if (G[i][next] > 0 && color[next] != BLACK) {
      search_loop(next);
    }
  }
  f[i] = ++t;
  color[i] = BLACK;

  return;
}

void dfs() {
  int i;
  for (i = 1; i <= n; i++)
    color[i] = WHITE; // initialize.

  t = 0;
  for (i = 1; i <= n; i++) {
    if (color[i] == WHITE)
      search_loop(i);
  }
  for (i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
  return;
}

int main() {
  int i, j, k, e;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      G[i][j] = 0;
    }
  }
  do {
    cin >> i >> e;
    for (j = 1; j <= e; j++) {
      cin >> k;
      G[i][k] = 1;
    }
  } while (i < n);

  dfs();

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct Node {
  int k, d, f;
  vector<int> v;
};

#define MAX_N 100

Node node[MAX_N + 1];
int stamp = 1;

void dfs(int u) {
  if (node[u].d == 0) {
    node[u].d = stamp++;
    for (int i = 0; i < node[u].k; ++i)
      if (node[u].v[i] != u)
        dfs(node[u].v[i]);
    if (node[u].f == 0)
      node[u].f = stamp++;
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u;
    cin >> u;
    cin >> node[u].k;
    for (int j = 0; j < node[u].k; ++j) {
      int v;
      cin >> v;
      node[u].v.push_back(v);
    }
    node[u].d = node[u].f = 0;
  }
  for (int i = 1; i <= n; ++i)
    dfs(i);
  for (int i = 1; i <= n; ++i)
    cout << i << " " << node[i].d << " " << node[i].f << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct Node {
  int k, d, f;
  vector<int> v;
};

#define MAX_N 100

Node node[MAX_N + 1];
int stamp = 1;
;

void dfs(int u) {
  if (node[u].d == 0)
    node[u].d = stamp++;
  for (int i = 0; i < node[u].k; ++i)
    if (node[u].v[i] != u)
      dfs(node[u].v[i]);
  if (node[u].f == 0)
    node[u].f = stamp++;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u;
    cin >> u;
    cin >> node[u].k;
    for (int j = 0; j < node[u].k; ++j) {
      int v;
      cin >> v;
      node[u].v.push_back(v);
    }
    node[u].d = node[u].f = 0;
  }
  for (int i = 1; i <= n; ++i)
    dfs(i);
  for (int i = 1; i <= n; ++i)
    cout << i << " " << node[i].d << " " << node[i].f << endl;
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// u?????£??\\??????v?????????????????????
int next(int u) {
  for (int v = nt[u]; v < n; ++v) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

//?????????????????¨????????±???????????¢?´¢
void dfs_visit(int r) {
  for (int i = 0; i < n; ++i) {
    nt[i] = 0;
  }
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  //????¨???????u????§??????¨????????±???????????¢?´¢
  for (int u = 0; u < n; ++u) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

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

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// u?????£??\\??????v?????????????????????
int next(int u) {
  for (int v = 0; v < n; ++v) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

//?????????????????¨????????±???????????¢?´¢
void dfs_visit(int r) {
  for (int i = 0; i < n; ++i) {
    nt[i] = 0;
  }
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  //????¨???????u????§??????¨????????±???????????¢?´¢
  for (int u = 0; u < n; ++u) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      M[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; ++j) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
int cnt = 1;

struct Edge {
  int to;
};

void DFS_visit(int top, vector<vector<Edge>> &G,
               vector<vector<int>> &timestump) {
  int next;
  timestump[top][0] = cnt++;
Label:
  if (G[top].empty()) {
    timestump[top][1] = cnt++;
  } else {
    next = G[top].begin()->to - 1;
    G[top].erase(G[top].begin());
    if (timestump[next][0] == 0) {
      DFS_visit(next, G, timestump);
      goto Label;
    } else {
      goto Label;
    }
  }
}

void DFS(vector<vector<Edge>> &G) {
  vector<vector<int>> timestump;
  timestump = vector<vector<int>>(G.size(), vector<int>(2, 0));

  for (int i = 0; i < G.size(); i++) {
    if (timestump[i][0] == 0) {
      DFS_visit(i, G, timestump);
    }
  }

  for (int i = 0; i < G.size(); i++) {
    cout << i + 1 << " " << timestump[i][0] << " " << timestump[i][1] << endl;
  }
}

int main(void) {
  int num;
  cin >> num;

  vector<vector<Edge>> AdjList(num);
  for (int i = 0; i < num; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      AdjList[i].push_back(Edge{v});
    }
  }

  DFS(AdjList);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct Edge {
  int to;
};

vector<vector<int>> DFS(vector<vector<Edge>> &G) {
  int timecounter = 0;
  vector<vector<int>> result;
  result = vector<vector<int>>(2, vector<int>(G.size(), 0));
  vector<int> prev(G.size(), 0);

  for (int i = 0; i < G.size(); i++) {
    int next = i + 1;
    while (result[1][i] == 0) {
      int tmp;
      if (result[0][next - 1] == 0) {
        timecounter += 1;
        result[0][next - 1] = timecounter;
      }

      if (G[next - 1].empty()) {
        if (result[1][next - 1] == 0) {
          timecounter += 1;
          result[1][next - 1] = timecounter;
        }
        next = prev[next - 1];
      } else {
        tmp = G[next - 1].begin()->to;
        G[next - 1].erase(G[next - 1].begin());
        prev[tmp - 1] = next;
        next = tmp;
      }
    }
  }
  return result;
}

int main(void) {
  int n;
  cin >> n;

  vector<vector<Edge>> AdjList(n);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      AdjList[i].push_back(Edge{v});
    }
  }

  vector<vector<int>> timestump = DFS(AdjList);

  for (int i = 0; i < AdjList.size(); i++) {
    cout << i + 1 << " " << timestump[0][i] << " " << timestump[1][i] << endl;
  }
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, tt;
int color[N], nt[N], M[N][N], d[N], f[N];

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

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

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

  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      dfs_visit(i);
    }
  }

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

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

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

  dfs();

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::__ios_failure'
  what():  basic_ios::clear: iostream error


Fixed submission:

#include <iostream>
#include <stack>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, tt;
int color[N], nt[N], M[N][N], d[N], f[N];

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

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

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

  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      dfs_visit(i);
    }
  }

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

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

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

  dfs();

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n;
  scanf("%d", &n);
  int a[n + 1][n + 1];
  int i, j;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      a[i][j] = 0;
  for (i = 1; i <= n; i++) {
    int q, k;
    scanf("%d %d", &q, &k);
    for (j = 1; j <= k; j++) {
      int p;
      scanf("%d", &p);
      a[q][p] = 1;
    }
  }
  int time = 0;
  std::stack<int> S;
  int ans[n + 1][3];

  S.push(1);
  time++;
  ans[1][1] = time;

  int color[n + 1];
  color[1] = 1;
  for (i = 2; i <= n; i++)
    color[i] = 0; // 0??????????¨????
  int lastcheck = 0;

  while (lastcheck == 0) {
    while (S.size() >= 1) {
      int flag = 0; // flag=0??????pop
      for (i = 1; i <= n; i++) {
        if (i != S.top() && a[S.top()][i] == 1 && color[i] == 0) {
          S.push(i);
          color[i] = 1;
          time++;
          ans[i][1] = time;
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        time++;
        ans[S.top()][2] = time;
        S.pop();
      }
    }
    lastcheck = 1;
    for (i = 1; i <= n; i++)
      if (color[i] == 0) {
        lastcheck = 0;
        break;
      }
    if (lastcheck == 0) {
      S.push(i);
      time++;
      ans[i][1] = time;
      color[i] = 1;
    }
  }

  for (i = 1; i <= n; i++) {
    printf("%d %d %d\\n", i, ans[i][1], ans[i][2]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n;
  scanf("%d", &n);
  int a[n + 1][n + 1];
  int i, j;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      a[i][j] = 0;
  for (i = 1; i <= n; i++) {
    int q, k;
    scanf("%d %d", &q, &k);
    for (j = 1; j <= k; j++) {
      int p;
      scanf("%d", &p);
      a[q][p] = 1;
    }
  }
  int time = 0;
  std::stack<int> S;
  int ans[n + 1][3];

  S.push(1);
  time++;
  ans[1][1] = time;

  int color[n + 1];
  color[1] = 1;
  for (i = 2; i <= n; i++)
    color[i] = 0; // 0??????????¨????
  int lastcheck = 0;

  while (lastcheck == 0) {
    while (S.size() >= 1) {
      int flag = 0; // flag=0??????pop
      for (i = 1; i <= n; i++) {
        if (i != S.top() && a[S.top()][i] == 1 && color[i] == 0) {
          S.push(i);
          color[i] = 1;
          time++;
          ans[i][1] = time;
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        time++;
        ans[S.top()][2] = time;
        S.pop();
      }
    }
    lastcheck = 1;
    for (i = 1; i <= n; i++)
      if (color[i] == 0) {
        lastcheck = 0;
        break;
      }
    S.push(i);
    time++;
    ans[i][1] = time;
  }
  for (i = 1; i <= n; i++) {
    printf("%d %d %d\\n", i, ans[i][1], ans[i][2]);
  }
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

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

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, Adj[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (Adj[u][v])
      return v;
  }
  return -1;
}
void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

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

  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}
int main() {

  int u, k, v;
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      Adj[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      Adj[u][v] = 1;
    }
  }

  dfs();
  return 0;
}

Error: TLE

Fixed submission:

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

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, Adj[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (Adj[u][v])
      return v;
  }
  return -1;
}
void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

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

  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << "" << d[i] << "" << f[i] << endl;
  }
}
int main() {

  int u, k, v;
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      Adj[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      Adj[u][v] = 1;
    }
  }

  dfs();
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

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

void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

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

int main() {
  int u, k, v;
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

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

void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int 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 (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

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

int main() {
  int u, k, v;
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}