Go: package main

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

// repr state of visited node
// WHITE yet visit
// GEAY  visiting
// BLACK visited
const (
	WHITE int = iota
	GRAY
	BLACK
)

type dfsGraph struct {
	g      [][]int
	colors []int
	stime  []int // Start time of visiting node
	ftime  []int // Finish time of visiting node
}

var time int

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func (dg dfsGraph) dfsVisit(u int) {
	// 頂点uを訪問中に設定
	dg.colors[u] = GRAY
	time++
	dg.stime[u] = time

	// uの隣接nodeを訪問
	for v := 0; v < len(dg.g); v++ {
		if dg.g[u][v] == 0 {
			continue // 辺がなければスキップ
		}

		// vが未訪問なら訪問
		if dg.colors[v] == WHITE {
			dg.dfsVisit(v)
		}
	}

	// 頂点uを訪問済みに設定
	dg.colors[u] = BLACK
	time++
	dg.ftime[u] = time
}

func (dg dfsGraph) dfs() {
	for i := 0; i < len(dg.g); i++ {
		if dg.colors[i] == WHITE {
			dg.dfsVisit(i)
		}
	}
}

func (dg dfsGraph) printDfsTime() {
	for i := 0; i < len(dg.g); i++ {
		fmt.Printf("%d %d %d\n", i+1, dg.stime[i], dg.ftime[i])
	}
}

func main() {
	adj := makeAdjList()
	colors := make([]int, len(adj))
	stime := make([]int, len(adj))
	ftime := make([]int, len(adj))
	graph := dfsGraph{adj, colors, stime, ftime}
	graph.dfs()
	graph.printDfsTime()
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define MAX 100
#define WHITE 1
#define GRAY 2
#define BLACK 3

int n;
int graph[MAX][MAX];
stack<int> st;
int *d,*f,*v;
int *color;
int tm;

int next(int k){
	for(int i = v[k];i < n;i++){
		v[k]++;
		if(color[i] == WHITE && graph[k][i] == 1) return i;
	}
	return -1;
}

void dfs(int k){
	for(int i = 0;i < n;i++) v[i] = 0;

	st.push(k);
	d[k] = ++tm;
	color[k] = GRAY;

	while(!st.empty()){
		int m = next(st.top());
		if(m != -1){
		  if(color[m] == WHITE){
				st.push(m);
				color[m] = GRAY;
				d[m] = ++tm;
			}
		}else{
			f[st.top()] = ++tm;
			color[st.top()] = BLACK;
			st.pop();
		}
	}
}


int main(){
	cin >> n;
	//?????????
	color = new int[n];
	d = new int[n];
	f = new int[n];
	v = new int[n];
	for(int i = 0;i < n;i++){
		color[i] = WHITE;
		d[i] = 0;
		f[i] = 0;
		for(int j = 0;j < n;j++){
			graph[i][j] = 0;
		}
	}
	//data input
	for(int i = 0;i < n;i++){

		int t,k,p;

		cin >> t >>  k;
		for(int j = 0;j < k;j++){
			cin >> p;
			p--;
			graph[i][p] = 1;
		}
	}
	tm = 0;
	for(int i = 0;i < n;i++){if(color[i] == WHITE) dfs(i);}
	for(int i = 0;i < n;i++){
		cout << i+1 << " " << d[i] << " " << f[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
const int MAXN = 1000;
int n;
int d[MAXN];
int f[MAXN];
int t;
void solve(int current,vector<vector<int> > &g){
  if(d[current] > 0)return;
  t++;
  d[current] = t;
  for(int next:g[current]){
    solve(next,g);
  }
  t++;
  f[current] = t;
}

int main(){
  cin >> n;
  vector<vector<int> > g(n+1);
  for(int i = 1;i <= n;i++){
    int u,k;
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      int v;
      cin >> v;
      g[i].push_back(v);
    }
  }
  solve(1,g);
  for(int i = 1;i <= n;i++){
    cout << i << ' ' << d[i] << ' ' << f[i] << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

int N;
vint G[100];
int FLG[100];
int D[100];
int F[100];
const int NIL = -1;
int tt;

void dfs(int ii){

	tt++;
	D[ii] = tt;

	cout << "reach " << ii << " " << tt << endl;

	rep(i, G[ii].size()){
		int to = G[ii][i];
		if (D[to] == NIL){
			dfs(to);
		}
	}

	tt++;
	F[ii] = tt;
	cout << "back " << ii << " " << tt << endl;

}

void run_dfs(){
	rep(i, N){ D[i] = NIL; }
	int start = 0;
	tt = 0;
	rep(i, N){
		if (D[i] == NIL) dfs(start);
	}
}

int main() {

	cin >> N;
	rep(i, N){
		int id, n; cin >> id >> n; id--;
		rep(j, n){
			int id_to; cin >> id_to; id_to--;
			G[id].push_back(id_to);
		}
	}

	run_dfs();

	rep(i, N){
		cout << i << " " << D[i] << " " << F[i] << endl;
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct dfs{
    vector<int> color;
    vector<vector<int>> adj;
    int time = 0;
    int n_;
    vector<P> time_lst;
    dfs(int n):color(n,0),adj(n),time_lst(n){
        n_ = n;
    }    

    void init(){
        rep(i,n_){
            int u;cin>>u;u--;
            int k;cin>>k;
            rep(j,k){
                int tmp;cin>>tmp;tmp--;
                adj[u].push_back(tmp);
            }
        }
    }
    void visit(int u){
        //まずは0スタート
        color[u] = 1;
        time_lst[u].first = ++time;
        for(int v:adj[u]){
            if(color[v] == 0){
                visit(v);
            }
        }
        color[u] = 2;
        time_lst[u].second = ++time;
    }
    void solve(){
        init();
        rep(i,n_){
            if(color[i] == 0){
                visit(i);
            }
        }
        rep(i,n_){
            cout<<i+1<<" "<<time_lst[i].first<<" "<<time_lst[i].second<<endl;
        }
    }
};

int main(){
    int m;cin>>m;
    dfs df(m);
    df.solve();
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
// stackのパターン
// 実習資料では「srcの訪問時刻は0」となっているが、題意は1である。

#include <iostream>
#include <cstdio>
#include <stack> // 入れたものから出していく引っ越しトラック型の構造。
using namespace std;

int G[110][110];
int n;
int u, k, v;

void make_matrix() {
  fill(&G[0][0], &G[0][0]+110*110, 0);
  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--;
      G[u][v] = 1;
    }
  }
}

int timer; // timeだとエラーが出たので。
int d[110]; // 訪問時刻。ナマの数字を入れる(indexを-1処理しない)。
int f[110]; // 終了時刻。ナマの数字を入れる。
int parent[110]; // 訪問する原因となった数字を入れる。ナマの数字を入れる。
int children[110]; // Stackに追加した頂点の数。

void init() {
  timer = 1;
  fill(d, d+110, -1);
  fill(f, f+110, -1);
  fill(parent, parent+110, -1);
  fill(children, children+110, 0);
}

void dfs(int src) {
  // cerr << "dfs root is " << src << endl;
  stack<int> S; // todo訪問先
  S.push(src);
  while (! S.empty()) {
    int cur = S.top(); // 一番最近入れた数字を訪問。
    d[cur] = timer;
    timer++;
    S.pop();
    // cerr << "visiting " << cur << " at the time " << d[cur] << endl;
    for (int i=1; i<=n; i++) { // 節点i
      if (G[cur-1][i-1] == 1 && d[i] == -1) {
	S.push(i);
	parent[i] = cur;
	children[cur]++;
      }
    }
    int hantei = cur;
    while (hantei != -1 && children[hantei] == 0) {
      f[hantei] = timer;
      timer++;
      hantei = parent[hantei];
      children[hantei]--;
    }
  }
}

int main() {
  make_matrix();
  init();
  for (int i=1; i<=n; i++) { // 1から連結していない連結成分にも番号をふる。
    if (d[i] == -1) {
      dfs(i);
    }
  }
  for (int i=1; i<=n; i++) {
    printf("%d %d %d\n", i, d[i], f[i]);
  }
}