Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	currentVertex := &vertices[currentId-1]
	if currentVertex.isVisited() {
		return timestamp
	}
	timestamp = timestamp + 1
	currentVertex.firstVisitAt = timestamp
	for _, adjId := range currentVertex.adjs {
		timestamp = dfs(adjId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func solveDfs(vertices []vertex) {
	timestamp := 0
	for {
		vid := vertexId(-1)
		for i := 0; i < len(vertices); i++ {
			if !vertices[i].isVisited() {
				vid = vertexId(i+1)
				break
			}
		}
		if vid > 0 {
			timestamp = dfs(vid, timestamp, vertices)
		} else {
			break
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveDfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <iostream>
#include <stack>

using namespace std;

static const int N = 100;
static const int YET = 0;
static const int ALREADY = 1;

int state[N];
int timeTable[N][2];
int timeCount = 0;
int currentV;
stack<int> st;
int M[N][N];

void travel() {
  while(!st.empty()) {
    currentV = st.top();
    st.pop();

    if(state[currentV] == YET) {
      timeCount++;
      timeTable[currentV][0] = timeCount;
      state[currentV] = ALREADY;
      st.push(currentV);

      for(int i = N; i > -1; i--) {
        if(M[currentV][i] == 1 && state[i] == YET) {
          st.push(i);
        }
      }
    }
    else if(timeTable[currentV][1] == 0){
      timeCount++;
      timeTable[currentV][1] = timeCount;
    }
  }
}
int main(void) {


  int n, u, k, v;

  cin >> n;

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

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

  st.push(0);
  travel();
  for(int i = 0; i < N; i++) {
    if(state[i] == YET) {
      st.push(i);
      travel();
    }
  }


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

C++: #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;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int ms[100][100],n,tt=0;
int color[101],nt[101];
int d[100],f[100];
int next(int v){
    for(int i=nt[v];i<n;i++){
        nt[v]=i+1;
        if(ms[v][i]==1)return i;
    }
    return -1;
}

void visit_dfs(int v){
    stack<int> s;
    s.push(v);
    d[v]=++tt;
    color[v]=1;
    while(!s.empty()){
        int u=s.top();
        int uu=next(u);
        if(uu!=-1){
            if(color[uu]==0){
            s.push(uu);
            d[uu]=++tt;
            color[uu]=1;
            }
        }else{
            s.pop();
            f[u]=++tt;
            color[u]=2;
        }
    }
    
    
}
void dfs(){
    for(int i=0;i<n;i++){
        color[i]=0;
        nt[i]=0;
    }
    for(int i=0;i<n;i++){
        if(color[i]==0){
            visit_dfs(i);
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}

int main(){
    
    cin>>n;
    rep(i,n){
        int a,b,c;
        cin>>a>>b;
        a--;
        rep(j,b){
            cin>>c;
            c--;
            ms[a][c]=1;
        }
    }
    
    dfs();
    return 0;

    
}

C++: #include <iostream>
#include <stack>
#define N 100
#define WHITE 0
#define GRAY  1
#define BLACK 2
#define NIL -1

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

// uの隣接点を番号順に取得する
// nt[N]には番号順での探索開始位置を保存する
// 隣接点がなければ-1を返す
int next(int u)
{
	for (int v = nt[u]; v < n; v++)
	{
		nt[u]++;
		if (M[u][v]) return v;
	}
	return NIL;
}

// rを開始地点として深さ優先探索を行う
void dfs_visit(int r)
{
	std::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 != NIL)
		{
			if (color[v] == WHITE)
			{
				S.push(v);
				color[v] = GRAY;
				d[v] = tt++;
			}
		}
		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 = 1;

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

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

int main()
{
	std::cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			M[i][j] = 0;
		}
	}

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

	dfs();
}