Go: package main

/*
深さ優先探索
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		ints := eGetInts(scanner)
		num, vcount := ints[0], ints[1]
		g[i] = make([]int, vcount)
		for j := 0; j < vcount; j++ {
			g[num][j] = ints[j+2]
		}
	}
	hist := make([]struct{ d, f int }, n+1)
	t := 0
	var visit func(int)
	visit = func(num int) {
		if hist[num].d == 0 {
			stack := []int{}
			t++
			hist[num].d = t
			stack = append(stack, num)
			for len(stack) > 0 {
				// トップを訪問する
				top := stack[len(stack)-1]
				for i := 0; i < len(g[top]); i++ {
					if hist[g[top][i]].d == 0 {
						t++
						hist[g[top][i]].d = t
						stack = append(stack, g[top][i])
						goto endloop
					}
				}
				// 訪問場所がなくなったら
				// pop
				stack = stack[0 : len(stack)-1]
				t++
				hist[top].f = t
			endloop:
			}
		}
	}
	for i := 1; i < n+1; i++ {
		visit(i)
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, hist[i].d, hist[i].f)
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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

using namespace std;

int g[102][102];
int d[102], f[102];
int t = 0, n;

int dfs(int x){
    t++;
    //cout<<x<<" s: "<<t<<endl;
    for(int i=1; i<=n; i++){
        if(d[x] == 0) d[x] = t;
       
        if(g[x][i] == 1 && f[i] == 0){
            g[x][i] = 0;
            if(d[i] == 0){
                dfs(i);
                t++;
            }   
        }
        
        if(i == n && f[x] == 0){
            //   cout<<x<<" e: "<<t+1<<endl;
            f[x] = t+1;
        }
    }
    return 0;        
  
}

int main(){
    cin >> n;

    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            g[i][j] = 0;
        }
        d[i] = 0;
        f[i] = 0;
    }
    
    int u,k;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            int v;
            cin>>v;
            g[u][v] = 1;
        }
    }

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

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

C++: #include<iostream>
using namespace std;


int uma[101];
int n=1;
int sore;
int check;
int i;
int shika;
int ima = 1;
int deru=0;
int yagi=1;
int ushi[101];
int kaba[101];
int miti[101][101];
int eda[101][101];
int haji[1001];
int owari[1001];
int main(){
	cin >> shika;
	for (int i = 1; i < shika + 1; i++){
		cin >> uma[i];
		cin >> ushi[uma[i]];
		for (int j = 1; j < ushi[uma[i]] + 1; j++){
			cin >> miti[uma[i]][j];
			eda[uma[i]][miti[uma[i]][j]] = 1;
			eda[miti[uma[i]][j]][uma[i]] = 1;
		}
	}
	//cout << "??\?????????";
	//for (int test = 1; test < shika + 1; test++){ cout << uma[test]<<endl;}//test

	//???????????????????????????
	ima = 1;
	while (true){
		check = 0;
		if (deru == 3){ break; }
		if (deru == 1||deru==2){ deru = 0; }
		yagi = yagi + 1;
	//	cout << yagi << "yagi" << endl;
		if (haji[uma[ima]] == 0){
			haji[uma[ima]] = yagi;
		}
		while (true){
			if (deru > 0){ break; }
			if (eda[uma[ima]][miti[uma[ima]][n]] == 1&&haji[miti[uma[ima]][n]]==0&&owari[miti[uma[ima]][n]]==0){
				eda[uma[ima]][miti[uma[ima]][n]] = 2; 
				//cout << "idou" << ima << "to" << miti[uma[ima]][n] << endl;
				eda[miti[uma[ima]][n]][uma[ima]] = 2;
				ima = miti[uma[ima]][n];
				n = 1;
				break;
			}
			if (eda[uma[ima]][miti[uma[ima]][n]] == 0){
			//	cout << "ikidomari" << endl;
						owari[uma[ima]] = yagi;		
				//cout << "owari" << uma[ima] << yagi << owari[uma[ima]] << endl;
				n = 1;
			//	cout << eda[3][4] <<"shika" << shika <<endl;
				while (true){
					//cout << "edan" << uma[ima] << n << miti[n][uma[ima]] << eda[uma[ima]][miti[n][uma[ima]]] << endl;
					if (deru == 2||deru == 3){ break; }
					for (int sore = 1; sore < shika + 1; sore = sore + 1){
						if (eda[uma[ima]][miti[uma[ima]][sore]] == 1&&owari[miti[uma[ima]][sore]]==0){
							deru = 2;
							n = 1;
							//cout << ima << "move to" << miti[uma[ima]][sore] << endl;
							eda[uma[ima]][miti[uma[ima]][sore]] = 2;
							eda[miti[uma[ima]][sore]][uma[ima]] = 2;
							ima = miti[uma[ima]][sore];
							check = 1;
						}
					}for (int sore = 1; sore < shika + 1; sore = sore + 1){
						if (check==0&&eda[uma[ima]][sore] == 2){
							eda[uma[ima]][sore] = 3;
							eda[sore][uma[ima]] = 3;
							ima = sore;
							deru = 2;
							n = 1;
							//cout << "modoru" << endl;
							break;
						}
					}
					if (n>shika+1){
						for (int i = 1; i < shika + 1; i++){
							if (haji[uma[i]] == 0){
								ima = i;
								//cout << "warp to " << ima << endl;
								deru = 2;
								n = 1;
								break;
							}
							else if (i == shika){
								//cout << "shika" << shika << "i" << i << "shuryo" << endl;
								deru = 3;
								break;
							}
						}
					}
					n = n + 1;
				}
			}
			n = n + 1;
		}
	}
	for (int i = 1; i < shika + 1; i++){
		cout << uma[i] << " " << haji[uma[i]] << " " << owari[uma[i]] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 105

int Mytime = 1;
int d[MAX], f[MAX];
struct Vertex {
	int degree;
	vector<int> adj;
	Vertex(int x = 0) {
		degree = x;
	}
};
vector<Vertex> v;

void DFS(int now) {
	if (d[now] != 0) // 已经搜索过了
		return;
	d[now] = Mytime;
	Mytime++;
	int count;
	for (count = 0; count < v[now].degree; count++) {
		DFS(v[now].adj[count]);
	}
	f[now] = Mytime;
	Mytime++;
}

int main(void) {
	int n, i, j;
	Vertex temp;
	v.push_back(temp);
	cin >> n;
	int u, k, ve;
	for (i = 1; i <= n; i++) {
		d[i] = f[i] = 0;
		cin >> u >> k;
		v.push_back(temp);
		v[i].degree = k;
		while (k--) {
			cin >> ve;
			v[u].adj.push_back(ve);
		}
	}
	for (i = 1; i <= n; i++) {
		DFS(i);
	}
	for (i = 1; i <= n; i++) {
		cout << i << " " << d[i] << " " << f[i] << endl;
	}

}