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 <string>
#include <vector>
#include <algorithm>
#include <queue>
#define WHITE 0//まだ訪問してない
#define GRAY 1//訪問中
#define BLACK 2//訪問した
#define N 1000
using namespace std;

int n;
int M[N][N] = {};
int color[N];
int d[N] = {}, f[N] = {};
int t = 0;

//頂点uからの深さ優先
void dfs(int u) {
	color[u] = GRAY;
	t++;
	d[u] = t;
	//cout << "a" << endl;
	for (int i = 1; i <= n; i++) {
		if (M[u][i] == 0)continue;
		if (color[i] != WHITE)continue;
		dfs(i);
	}
	color[u] = BLACK;
	t++;
	f[u] = t;
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		color[i] = WHITE;
	}
	for (int i = 0; i < n; i++) {
		int v;
		int s;
		int k;
		cin >> s;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			M[s][v] = 1;
		}
	}
	//cout << "動いてる" << endl;

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

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

static const int MAX_NUM = 101;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void DSF(int* matrix, int num)
{
    int i, j;
    int ttime = 0;
    int stime[MAX_NUM+1] = {0};
    int etime[MAX_NUM+1] = {0};
    int state[MAX_NUM+1] = {0};
    stack<int> S;
    int target, next;
    int *tmp;

    for (j = 1; j <= num; j++) {
        if (state[j] != NO_VISIT) {
            continue;
        }
        S.push(j);
        ++ttime;
        stime[j] = ttime;
        state[j] = NOW_VISIT;

        while (S.empty() == false) {
            target = S.top();
            for (i = 1; i <= num; i++) {
                tmp = matrix + MAX_NUM * target + i;
                if ((*tmp == 1) && (state[i] == NO_VISIT)) {
                    *tmp = 2;
                    S.push(i);
                    ++ttime;
                    stime[i] = ttime;
                    state[i] = NOW_VISIT;
                    break;
                }
            }

            if (i == num + 1) {
                S.pop();
                ++ttime;
                etime[target] = ttime;
                state[target] = END_VISIT;
            }
        }
    }

    for (i = 1; i <= num; i++) {
        printf("%d %d %d\n", i, stime[i], etime[i]);
    }

    return;
}

int main(void)
{
    int i, j;
    int num;
    int elem;
    int nume;
    int tmp;
    int matrix[MAX_NUM][MAX_NUM] = {0};

    scanf("%d", &num);

    for (i = 0; i < num; i++) {
        scanf("%d %d", &elem, &nume);
        for (j = 0; j < nume; j++) {
            scanf("%d", &tmp);
            matrix[elem][tmp] = 1;
        }
    }

    DSF(&matrix[0][0], num);

    return 0;
}

C++: #include <iostream>
#include <utility>
#define N 	100

using namespace std;

int T[N+1][N+1];
pair<int , int> M[N+1];
int t = 0;

void init(){
	for (int i = 0; i < 100; ++i)
	{
		M[i].first = 0;
		M[i].second = 0;
		for (int j = 0; j < 100; ++j)
		{
			T[i][j] = 0;
		}
	}
}

void DispTable(int n){
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cout << T[i][j];
			if(j!=n) cout << " ";
		}
		cout << endl;
	}
}

void DispM(int n){
	for (int i = 1; i<=n; i++)
	{
		cout << i << " " << M[i].first << " " << M[i].second << endl;
	}
}

void dfs(int cur,int n){
	t++;
	M[cur].first = t;
	// cerr << "visiting " << cur << " " << t << endl;

	for (int i = 1; i <= n; ++i)
	{
		if(T[cur][i] == 1 && M[i].first == 0) dfs(i,n);
	}
	t++;
	M[cur].second = t;
}

int main(){
	int n;
	cin >> n;
	init();
	for (int i = 0; i < n; ++i)
	{
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; ++j)
		{
			int v;
			cin >> v;
			T[u][v] = 1;	
		}
	}
	// DispTable(n);
	dfs(1,n);
	DispM(n);
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

struct Node {
	int k, d, f;
	vector<int> v;
};

#define MAX_N 100

Node node[MAX_N + 1];
int stamp = 1;

void dfs(int u) {
	if (node[u].d == 0) {
		node[u].d = stamp++;
		for (int i = 0; i < node[u].k; ++i)
			if (node[u].v[i] != u)
				dfs(node[u].v[i]);
		if (node[u].f == 0) node[u].f = stamp++;
	}
}

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

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


int d[200] = {}, f[200] = {};

vector<int> G[200];

int func(int pos, int time){
  if(d[pos] > 0){
    return time;
  }
  d[pos] = time;
  time += 1;
  for(int to : G[pos]){
    time = func(to, time);
  }
  f[pos] = time;
  time += 1;
  return time;
}


int main(){
	// input

	int n;
	cin >> n;

	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].push_back(v);
		}
	}

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

	}
	return 0;
}