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++: /*d[n]で最初に発見した時刻を出していき、
f[n]で戻りながら完了時刻をいれていく書き方*/
#include <iostream>
using namespace std;

int r[101][101];
int d[101], f[101], num, visit[101], p=0;

void dfs(int n){
    if(!visit[n]){
        visit[n]++;
        d[n] = ++p;
        for(int i=1; i<num+1; i++)
            if(r[n][i]) dfs(i);
        f[n] = ++p;
    }
}

int main(void){
    int u, k, v;
    cin >> num;
    for(int i=0; i<num; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> v;
            r[u][v]++;
        }
    }
    for(int i=1; i<num+1; i++){
        if(!visit[i]) dfs(i);
    }
    for(int i=1; i<num+1; i++)
        cout << i << " " << d[i] << " " << f[i] << endl;
}

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

using namespace std;

int N;
vector<vector<int>> tree;
vector<pair<int,int>> times;

void stamp(int ind, int &time){
	//cout << ind << "," << time << endl;
	if(times[ind].first == 0) times[ind].first = time++;
	else return;
	if(tree[ind].size() == 0){
		times[ind].second = time++;
		//cout<<"break! " << ind << endl;
		return;
	}
	for(int i=0;i<tree[ind].size();i++){
		//cout << tree[ind][i] << endl;
		stamp(tree[ind][i],time);
	}
	//cout << ind << ":" << time << endl;
	times[ind].second = time++;
	if(ind == 1){
		for(int i=2;i<=N;i++){
			stamp(i,time);
		}
	}
}

int main(){
	cin >> N;
	tree = vector<vector<int>>(N+1);
	times = vector<pair<int,int>>(N+1,make_pair(0,0));

	for(int i=1;i<N+1;i++){
		int ind, branch;
		cin >> ind >> branch;
		vector<int> tmp(branch);
		for(int j=0;j<branch;j++){
			int in;
			cin >> in;
			tmp[j] =  in;
		}
		tree[ind] = tmp;
	}

	int time = 1;
	stamp(1,time);

	int i = 1;
	for(auto st:times){
		if(i<=N)
			cout << i << " " << times[i].first << " " << times[i].second << endl;
		i++;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GLAY 1
#define BLACK 2

// 頂点の個数
int n;
// 隣接行列
vector<vector<int> > M(100, vector<int>(100, 0));
// 頂点の状態を格納する配列
vector<int> color(100, WHITE);
// 発見時刻を格納する配列
vector<int> d(100);
// 完了時刻を格納する配列
vector<int> f(100);
// 時刻
int tt = 0;

// 再帰的にu(WHITEと仮定)をスタートして探索
void dfs_visit(int u)
{
  // uの状態を訪問済みにする
  color.at(u) = GLAY;
  // uの発見時刻を記録
  d.at(u) = ++tt;
  // uの隣接頂点(小さい順に)探索
  for (int v = 0; v < n; v++)
  {
    if (M.at(u).at(v) == 0)
    {
      // vがuの隣接頂点でないとき
      // このループをスキップ
      continue;
    }
    if (color.at(v) == WHITE)
    {
      // vがuの隣接頂点であり、かつ未発見のとき
      // vをスタートとして探索
      dfs_visit(v);
    }
  }
  // uの隣接頂点の状態が全て完了になっているのでuの状態も完了にする
  color.at(u) = BLACK;
  // uの完了時刻を記録
  f.at(u) = ++tt;
}

// 全ての頂点を探索
void dfs()
{
  for (int i = 0; i < n; i++)
  {
    if (color.at(i) == WHITE)
    {
      dfs_visit(i);
    }
  }
}

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

  dfs();

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

C++: #include <stdio.h>

#define N 101

int color[N];
int n, G[N][N],f[N],d[N],time;

void visit(int);

int main(){
  int i,j,v,vnext,k;
 
  scanf("%d",&n);

  for(i=0;i<n;i++){
    color[i] = 0;
    for(j=0;j<n;j++) {
      G[i][j] = 0;
    }
  }

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

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

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

void visit(int u){

  int i; 
  color[u]=1;
  d[u]= ++time;
  for(i=0; i<n; i++){
    if(G[u][i] == 1){
      if(color[i] == 0)visit(i);
    }
  }

  color[u]= 2;
  f[u]=++time;
}

C++: #include<stdio.h>
 
#define MAX 101
#define WHITE -1
#define GRAY 0
#define BLACK 1
 
int n,t;
int color[MAX],d[MAX],f[MAX],Adj[MAX][MAX];
 
dfs(){
  int i;
 
  for(i=0;i<n;i++) color[i]=WHITE;
  t=0;
  for(i=0;i<n;i++){
    if(color[i]==WHITE) visit(i);
  }
}
 
visit(int r){
  int i;
 
  color[r]=GRAY;
  d[r]=++t;
 
  for(i=0;i<n;i++){
    if(Adj[r][i]==1 && color[i]==WHITE) visit(i);
  }
 
  color[r]=BLACK;
  f[r]=++t;
}
 
int main(){
  int i,u,k,j,v;
 
  for(i=0;i<MAX;i++){
    for(j=0;j<MAX;j++){ 
      Adj[i][j]=0;
    }
  }
 
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      scanf("%d",&v);
      Adj[u-1][v-1]=1;
    }
  }
 
  dfs();
 
  for(i=0;i<n;i++){
    printf("%d %d %d\n",i+1,d[i],f[i]);
  }
 
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define EXIST 1
#define TODO 1
#define DONE 0
int graph[200][200];
int used[200];
int disc[200];
int fin[200];
int n;
int elapsed;

void dfs(int pos) {
  disc[pos] = ++elapsed;
  used[pos] = DONE;
  for (int i = 0; i < n; i++) {
    if (graph[pos][i] == EXIST && used[i] == TODO) {
      dfs(i);
    }
  }
  fin[pos] = ++elapsed;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k, v; cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      graph[u-1][v-1] = EXIST;
    }
    used[i] = TODO;
  }
  for (int i = 0; i < n; i++) {
    if (used[i] == TODO) {
      dfs(i);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i+1 << " " << disc[i] << " " << fin[i] << endl; 
  }
}