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

#include <iostream>
#include <stdio.h>

#define MAX 101

int main() {
  int n;
  int M[MAX][MAX];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);
    if (k == 0)
      continue;
    else {
      for (int j = 1; j <= k; j++) {
        int v;
        scanf("%d", &v);
        M[id][v] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == n)
        printf("%d\\n", M[i][j]);
      else
        printf("%d ", M[i][j]);
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

#define MAX 100

int main() {
  int n;
  int M[MAX][MAX];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);
    if (k == 0)
      continue;
    else {
      for (int j = 1; j <= k; j++) {
        int v;
        scanf("%d", &v);
        M[id][v] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == n)
        printf("%d\\n", M[i][j]);
      else
        printf("%d ", M[i][j]);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u][v - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

#define MAX 101

using namespace std;

int visited[MAX], d[MAX], f[MAX];
int tt = 0;
vector<int> G[MAX];

void dfs(int u) {
  d[u] = ++tt;
  visited[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {
    if (visited[G[u][i]] == 0)
      dfs(G[u][i]);
  }
  f[u] = ++tt;
}

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

Error: -11

Fixed submission:

#include <iostream>
#include <vector>

#define MAX 101

using namespace std;

int visited[MAX], d[MAX], f[MAX];
int tt = 0;
vector<int> G[MAX];

void dfs(int u) {
  d[u] = ++tt;
  for (int i = 0; i < G[u].size(); i++) {
    if (visited[G[u][i]] == 0)
      dfs(G[u][i]);
  }
  visited[u] = 1;
  f[u] = ++tt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    visited[i] = 0;
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int i = 0; i < k; i++) {
      int v;
      cin >> v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

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

void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}

void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;

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

  for (u = 0; u < n; u++) {
    printf("%d %d %d\\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, k, v, i, j;

  scanf("%d", &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++) {
    scanf("%d %d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

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

void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}

void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;

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

  for (u = 0; u < n; u++) {
    printf("%d %d %d\\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, k, v, i, j;

  scanf("%d", 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++) {
    scanf("%d %d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

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

#include <bits/stdc++.h>
using namespace std;
int a[101][101], f[101], e[101], nt[101], cur, i, j, n, t = 0;
int color[101];
void dfs(int u) {
  f[u] = ++t;
  color[u] = 1;
  for (int v = 0; v < n; ++v) {
    if (color[v] == 0 && a[u][v]) {
      dfs(v);
    }
  }
  e[u] = ++t;
}
int main() {
  int u, v;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d %d", &u, &cur);
    for (j = 0; j < cur; ++j) {
      scanf("%d", &v);
      a[u - 1][v - 1] = 1;
    }
  }
  for (i = 0; i < n; ++i) {
    if (!color[i]) {
      dfs(i);
    }
  }
  for (i = 0; i < n; ++i)
    printf("%d %d %d\\n", i + 1, f[i], e[i]);
  return 0;
}


Error: -11

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
#define MAX 105
#define WT 0
#define GR 1
#define BK 2

using namespace std;

int M[MAX][MAX];
int cr[MAX], d[MAX], f[MAX];
int t;
stack<int> ck;
void dfs_init(int n) {
  t = 1;

  for (int i = 1; i <= n; i++)
    cr[i] = WT;

  return;
}
void dfs(int k, int n) {
  if (cr[k] == WT) {
    cr[k] = GR;
    d[k] = t;
    t++;
    ck.push(k);

    for (int i = 1; i <= n; i++) {
      if (M[k][i] == 1 && cr[i] == WT)
        dfs(i, n);
      else if (i == n) {
        dfs(ck.top(), n);
        ck.pop();
      }
    }
  } else if (cr[k] == GR) {
    ck.pop();
    cr[k] = BK;
    f[k] = t;
    t++;

    if (!ck.empty())
      dfs(ck.top(), n);
    else
      return;
  }
}
int main() {
  int i, j, n, p, q, tmp;

  // Set the matrix: M[n][n]
  for (i = 0; i < MAX; i++)
    for (j = 0; j < MAX; j++)
      M[i][j] = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d%d", &p, &q);

    for (j = 0; j < q; j++) {
      scanf("%d", &tmp);
      M[p][tmp] = 1;
    }
  }

  // DFS:d[v] and f[v] for start and end
  dfs_init(n);
  dfs(1, n);

  for (i = 1; i <= n; i++)
    printf("%d %d %d\\n", i, d[i], f[i]);

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

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <stdio.h>
#define black 0
#define red 1
#define white -1
#define maxn 110
using namespace std;

int tot = 0;
int book[maxn] = {}, t[maxn][2] = {};
list<int> map[maxn];

void dfs(int u) {
  book[u] = red;
  t[u][0] = ++tot;
  for (list<int>::iterator it = map[u].begin(); it != map[u].end(); it++) {
    if (book[*it - 1] == black) {
      dfs(*it - 1);
    }
  }
  book[u] = white;
  t[u][1] = ++tot;
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int u, k, v;
    scanf("%d%d", &u, &k);
    u--;
    for (int i = 0; i < k; i++) {
      scanf("%d", &v);
      map[u].push_back(v);
    }
  }
  for (int i = 0; i < n; i++)
    if (book[i] == black)
      dfs(i);
  for (int i = 0; i < n; i++) {
    printf("%d %d %d\\n", i + 1, t[i][0], t[i][1]);
    ;
  }
  return 0;
}


Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <stdio.h>
#define black 0
#define red 1
#define white -1
#define maxn 110
using namespace std;

int tot = 0;
int book[maxn] = {}, t[maxn][2] = {};
list<int> map[maxn];

void dfs(int u) {
  if (book[u] == white)
    return;
  else if (book[u] == black) {
    t[u][0] = ++tot;
    for (list<int>::iterator it = map[u].begin(); it != map[u].end(); it++)
      dfs(*it - 1);
    book[u] = white;
    t[u][1] = ++tot;
  }
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int u, k, v;
    scanf("%d%d", &u, &k);
    u--;
    for (int i = 0; i < k; i++) {
      scanf("%d", &v);
      map[u].push_back(v);
    }
  }
  for (int i = 0; i < n; i++)
    if (book[i] == black)
      dfs(i);
  for (int i = 0; i < n; i++) {
    printf("%d %d %d", i + 1, t[i][0], t[i][1]);
    if (i != n - 1)
      printf("\\n");
  }
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N = 100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
// #define Kongxiangzhouye
int n;
int a[123][123];
int st[123];
int ed[123];
int now;
void dfs(int local) {
  st[local] = now++;
  for (int i = 1; i <= n; i++) {
    if (a[local][i]) {
      if (st[i] == 0) {
        dfs(i);
      }
    }
  }
  ed[local] = now++;
}
int main() {
  ios::sync_with_stdio(false);
  while (cin >> n) {
    memset(a, 0, sizeof(a));
    memset(st, 0, sizeof(st));
    memset(ed, 0, sizeof(ed));
    for (int i = 0; i < n; i++) {
      int t, m;
      cin >> t >> m;
      for (int i = 0; i < m; i++) {
        int te;
        cin >> te;
        a[t][te] = 1;
      }
      now = 1;
    }
    for (int i = 1; i <= n; i++) {
      if (!st[i])
        dfs(i);
    }
    for (int i = 1; i <= n; i++) {
      cout << i << " " << st[i] << " " << ed[i] << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define N 2000

using namespace std;
vector<vector<int>> pa;
vector<vector<int>> ch;
int first[N] = {0};
int second[N] = {0};
int timestamp = 0;
void dfs(int x) {
  if (ch[x].empty()) {
    return;
  } else {
    for (int i = 0; i < ch[x].size(); i++) {
      if (first[ch[x][i]] * second[ch[x][i]] == 0) {
        timestamp++;
        first[ch[x][i]] = timestamp;
        dfs(ch[x][i]);
        timestamp++;
        second[ch[x][i]] = timestamp;
      }
    }
    return;
  }
}
int main() {
  int n;
  cin >> n;
  int temp = n;
  pa.clear();
  pa.resize(N + 5);
  ch.clear();
  ch.resize(N + 5);
  while (temp--) {
    int u, k, v;
    cin >> u >> k;
    for (int i = 0; i < k; i++) {
      cin >> v;
      pa[v].push_back(u);
      ch[u].push_back(v);
    }
  }
  int an = 0;
  for (int i = 1; i <= n; i++) {
    if (pa[i].empty()) {
      an = i;
    } else {
      sort(pa[i].begin(), pa[i].end(), less<int>());
    }
    if (ch[i].empty()) {
      ;
    } else {
      sort(ch[i].begin(), ch[i].end());
    }
  }
  timestamp++;
  first[an] = timestamp;
  dfs(an);
  timestamp++;
  second[an] = timestamp;
  for (int i = 1; i <= n; i++) {
    cout << i << ' ' << first[i] << ' ' << second[i] << '\\n';
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
struct {
  int Top = 0;
  int s[20000];
  bool Empty() { return Top == 0; }
  void push(int x) { s[++Top] = x; }
  void pop() { Top--; }
  int top() { return s[Top]; }
} S;
const int N = 100;
const int bai = 0;
const int hui = 1;
const int hei = 2;
int n, tu[N][N];
int fx[N], js[N], sum, nt[N], jilu[N];
int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (tu[u][v])
      return v;
  }
  return -1;
}
void DFS_visit(int r) {

  S.push(r);
  jilu[r] = hui;
  fx[r] = ++sum;
  while (!S.Empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (jilu[v] == bai) {
        jilu[v] = hui;
        fx[v] = ++sum;
        S.push(v);
      }
    } else {
      S.pop();
      jilu[u] = hei;
      js[u] = ++sum;
    }
  }
}
void DFS() {
  sum = 0;
  for (int u = 0; u < n; u++) {
    if (jilu[u] == bai)
      DFS_visit(u);
  }
  for (int i = 0; i < n; i++) {
    printf("%d %d %d\\n", i + 1, fx[i], js[i]);
  }
}
int main() {
  int u, k, v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      tu[u][v] = 1;
    }
  }
  DFS();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<int> G[102];
int d[102], f[102];
bool vis[102];
int V;
int cur = 0;
/*
void dfs(int id){
    cur++;
    d[id]=cur;
    vis[id]=true;
    for(int i=0;i<G[id].size();i++){
        if(!vis[G[id][i]])
            dfs(G[id][i]);
    }
    f[id]=cur;
}
*/
stack<int> s;
void dfs_visit(int id) {
  s.push(id);
  cur++;
  d[id] = cur;
  while (!s.empty()) {
    int u = s.top();
    int nex = -1;
    for (int i = 0; i < G[u].size(); i++) {
      if (!vis[G[u][i]]) {
        nex = G[u][i];
        break;
      }
    }
    cur++;
    if (nex == -1) {
      s.pop();
      f[u] = cur;
      vis[u] = true;
    } else {
      d[nex] = cur;
      s.push(nex);
    }
  }
}

void dfs() {
  int i;
  for (i = 1; i <= V; i++) {
    if (!vis[i])
      dfs_visit(i);
  }
  for (i = 1; i <= V; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
}
int main() {
  cin >> V;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= V; i++) {
    int id, k;
    cin >> id >> k;
    while (k--) {
      int out;
      cin >> out;
      G[id].push_back(out);
    }
    sort(G[id].begin(), G[id].end());
  }
  dfs();
  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int tu[1000][1000];
int a[10000];
int d[10000];
int f[10000];
int shijian, n;
void sou(int u) {
  a[u] = 2;
  d[u] = ++shijian;
  for (int i = 0; i < n; i++) {
    if (tu[u][i] == 0)
      continue;
    if (a[i] == 1) {
      sou(i);
    }
  }
  a[u] = 3;
  f[u] = ++shijian;
}
void shensou() {
  for (int u = 0; u < n; u++) {
    a[u] = 1;
  }
  shijian = 0;
  for (int u = 0; u < n; u++) {
    if (a[u] == 1)
      sou(u);
  }
  for (int u = 0; u < n; u++) {
    printf("%d %d %d\\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int v;
  int m;
  int x;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &v, &m);
    v--;
    for (int j = 0; j < m; j++) {
      scanf("%d", &x);
      x--;
      tu[v][x] = 1;
    }
  }
  shensou();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int MAX = 110;
vector<int> G[MAX];
int dtime[MAX];
int ftime[MAX];
int n;
int tim;

void dfs(int p) {
  if (ftime[p] == -1)
    dtime[p] = ++tim;
  else
    return;
  for (int i = 0; i < G[p].size(); i++) {
    dfs(G[p][i]);
  }
  if (ftime[p] == -1)
    ftime[p] = ++tim;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    G[i].clear();
    dtime[i] = -1;
    ftime[i] = -1;
  }
  tim = 0;
  for (int i = 1; i <= n; i++) {
    int p, k;
    scanf("%d %d", &p, &k);
    while (k--) {
      int ne = 0;
      scanf("%d", &ne);
      G[p].push_back(ne);
    }
    sort(G[p].begin(), G[p].end());
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d %d\\n", i, dtime[i], ftime[i]);
  }
  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 M[N][N], color[N], d[N], f[N], nt[N];
int tt, 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] = BLACK;
  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 j = 0; j < n; j++) {
    if (color[j] == WHITE) {
      dfs_visit(j);
    }
  }

  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 i = 0; i < k; i++) {
      cin >> v;
      v--;
      M[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 M[N][N], color[N], d[N], f[N], nt[N];
int tt, n;

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

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

  stack<int> S;
  S.push(u);
  color[u] = BLACK;
  f[u] = ++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;
  }
  tt = 0;

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

  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;
    }
  }
  cin >> u >> k;
  u--;
  for (int i = 0; i < k; i++) {
    cin >> v;
    v--;
    M[u][v] = 1;
  }

  dfs();

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

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

void dfs(int cur);

int A[100][100], d[100], f[100];
;
int n, t = 0;

int main() {
  cin >> n;

  for (int i = 0; i < n; ++i) {

    d[i] = 0;
    f[i] = 0;

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

    int m, k; // k=number of adj
    cin >> m >> k;
    assert(m == i + 1);

    for (int j = 0; j < k; ++j) {
      int adj;
      cin >> adj;
      if (adj) {
        A[i][adj - 1] = 1;
      }
    }
  }

  /*
  for(int i=0;i<n;++i){
          for(int j=0;j<n;++j){
                  cout<<A[i][j]<<" ";
          }
          cout<<endl;
  }
  */

  for (int i = 0; i < n; ++i) {
    if (d[i] == 0)
      dfs(i);
  }

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

  return 0;
}

void dfs(int cur) {
  t += 1;
  d[cur] = t;
  // cout<<"visiting "<<cur+1<<" "<<t<<endl;

  for (int dst = 0; dst < n; ++dst) {
    if (A[cur][dst] == 1 && d[dst] == 0) {
      dfs(dst);
    }
  }

  t += 1;
  f[cur] = t;

  // cout<<"leaving "<<cur+1<<" "<<t<<endl;

  return;
}

Error: WA

Fixed submission:

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

void dfs(int cur);

int A[100][100], d[100], f[100];
;
int n, t = 0;

int main() {
  cin >> n;

  for (int i = 0; i < n; ++i) {

    d[i] = 0;
    f[i] = 0;

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

    int m, k; // k=number of adj
    cin >> m >> k;
    assert(m == i + 1);

    for (int j = 0; j < k; ++j) {
      int adj;
      cin >> adj;
      if (adj) {
        A[i][adj - 1] = 1;
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cout << A[i][j] << " ";
    }
    cout << endl;
  }

  dfs(0);

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

  return 0;
}

void dfs(int cur) {
  t += 1;
  d[cur] = t;

  cerr << "visiting " << cur + 1 << " " << t << endl;

  for (int dst = 0; dst < n; ++dst) {
    if (A[cur][dst] == 1 && d[dst] == 0) {
      dfs(dst);
    }
  }

  t += 1;
  f[cur] = t;

  cerr << "leaving " << cur + 1 << " " << t << endl;

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

#include <iostream>
using namespace std;

int d[101] = {0};
int f[101] = {0};
int AdjM[101][101] = {0};
int lapse = 0;

void dfs(int cur) {
  ++lapse;
  d[cur] = lapse;
  // cerr<<"visiting "<<cur<<" "<<lapse<<"\\n";
  for (int dst = 1; dst < 101; ++dst) {
    if (AdjM[cur][dst] != 0 && d[dst] == 0) {
      dfs(dst);
    }
  }
  ++lapse;
  f[cur] = lapse;
}

int main() {
  int N;
  cin >> N;
  int id, edgnum, adj;
  for (int i = 0; i < N; ++i) {
    cin >> id >> edgnum;
    for (int j = 0; j < edgnum; ++j) {
      cin >> adj;
      AdjM[id][adj] += 1;
    }
  }

  for (int i = 1; i <= N; ++i) {
    if (d[i] == 0) {
      dfs(i);
    }
  }

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

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


Fixed submission:

#include <iostream>
using namespace std;

int d[101] = {0};
int f[101] = {0};
int AdjM[101][101] = {0};
int lapse = 0;

void dfs(int cur) {
  ++lapse;
  d[cur] = lapse;
  cerr << "visiting " << cur << " " << lapse << "\\n";
  for (int dst = 1; dst < 101; ++dst) {
    if (AdjM[cur][dst] != 0 && d[dst] == 0) {
      dfs(dst);
    }
  }
  ++lapse;
  f[cur] = lapse;
}

int main() {
  int N;
  cin >> N;
  int id, edgnum, adj;
  for (int i = 0; i < N; ++i) {
    cin >> id >> edgnum;
    for (int j = 0; j < edgnum; ++j) {
      cin >> adj;
      AdjM[id][adj] += 1;
    }
  }

  dfs(1);

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