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 <vector>

using namespace std;

struct Edge
{
	int to;
};

vector<vector<int>> DFS(vector<vector<Edge>>& G)
{
	int timecounter = 0;
	vector<vector<int>> result;
	result = vector<vector<int>>(2, vector<int>(G.size(), 0));
	vector<int> prev(G.size(),0);
	
	for(int i = 0; i < G.size(); i++){
		int next = i + 1;
		while(result[1][i] == 0){
			int tmp;
			if(result[0][next - 1] == 0){
				timecounter += 1;	
				result[0][next - 1] = timecounter;
			}

			if(G[next - 1].empty() || result[0][G[next - 1].begin()->to - 1]){
				if(result[1][next - 1] == 0){
					timecounter += 1;
					result[1][next - 1] = timecounter;
				}
				next = prev[next - 1];
			}else{
				tmp = G[next - 1].begin()->to;
				G[next - 1].erase(G[next - 1].begin());
				if(tmp != next)
					prev[tmp - 1] = next;
				next = tmp;
			}
			//cout << next << endl;
		}
	}
	return result;
}

int main(void)
{
	int n;
	cin >> n;

	vector<vector<Edge>> AdjList(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;
			AdjList[i].push_back(Edge{v});
		}
	}

	vector<vector<int>> timestump = DFS(AdjList);
	
	for(int i = 0; i < AdjList.size(); i++){
		cout << i + 1 << " " << timestump[0][i] << " " << timestump[1][i] << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
int g[101][101], d[101], f[101], vis[101];
int time = 0, n;

void dfs(int u)
{
  if (vis[u] != 0) return;
  vis[u] = 1;
  d[u] = ++time;
  for (int i = 0; i < n; i++)
  {
    if (g[u][i] == 1)
    {
      dfs(i);
    }
  }
  vis[u] = 2;
  f[u] = ++time;
}

int main()
{
  memset(g, 0, sizeof(g));
  memset(vis, 0, sizeof(vis));
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    int u, adj_num;
    scanf("%d%d", &u, &adj_num);
    while (adj_num--)
    {
      int v;
      scanf("%d", &v);
      g[u - 1][v - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++)
  {
    if (!vis[i]) dfs(i);
  }
  for (int i = 0; i < n; i++)
  {
    printf("%d %d %d\n", i + 1, d[i], f[i]);
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int d[105] = { -1 }, f[105] = { -1 };
int n = -1;
int mat[105][105] = { { 0 } };
int visited[105] = { 0 };

void dfs(int v, int& now)
{
  d[v] = now;
  visited[v] = 1;
  ++now;
  for (int u = 1; u <= n; ++u) {
    if (mat[v][u] && !visited[u]) {
      dfs(u, now);
    }
  }
  f[v] = now;
  ++now;
}

int main()
{
  fscanf(stdin, "%d", &n);
  for (int i = 0; i < n; ++i) {
    int u = -1, k = -1, v = -1;
    fscanf(stdin, "%d %d", &u, &k);
    for (int j = 0; j < k; ++j) {
      fscanf(stdin, "%d", &v);
      mat[u][v] = 1;
    }
  }
  int now = 1;
  for (int v = 1; v <= n; ++v) {
    if (!visited[v]) {
      dfs(v, now);
    }
  }
  for (int i = 0; i < n; ++i) {
    fprintf(stdout, "%d %d %d\n", i + 1, d[i + 1], f[i + 1]);
  }
  return 0;
}

C++: #include<iostream>
#include<stack>
//#include "stdio.h"

using namespace std;

#define N 100

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1

int n, M[N][N];
int color[N],d[N],f[N];
int world_time;

int next(int node){
  for(int v=0;v<n;v++){
    if(M[node][v] == 1 && color[v] == WHITE){
      return v;
    } 
  }
  return NIL;
}

void dfs_visit(int i){
  
  world_time = world_time + 1;
  stack<int> S;
  S.push(i);
  color[i] = GRAY;
  d[i] = world_time;

  int present,next_node;
  while( !S.empty() ){
    world_time = world_time + 1;
    present = S.top();
    next_node = next(present);

    if(next_node != NIL){
      S.push(next_node);
      d[next_node] = world_time;
      color[next_node] = GRAY;
    }else{
      color[present] = BLACK;
      S.pop();
      f[present] = world_time;
    }
  }
}

void dfs(){
  world_time = 0;

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

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

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


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

  int u,v,k;
  for(int i=0;i<n;i++){
    cin >> u >> k;

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

  dfs();

  return 0;
}