Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	ans := make([][2]int, N)
	time := 0
	found := make(map[int]bool)
	var dfs func(cur int)
	dfs = func(cur int) {
		time++
		ans[cur][0] = time
		found[cur] = true
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if !found[next] {
				dfs(G[cur][i])
			}
		}
		time++
		ans[cur][1] = time
	}
	for i := 0; i < N; i++ {
		if !found[i] {
			dfs(i)
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprint(wtr, i+1)
		for j := 0; j < 2; j++ {
			fmt.Fprint(wtr, " ")
			fmt.Fprint(wtr, ans[i][j])
		}
		fmt.Fprintln(wtr)
	}
	wtr.Flush()
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cstring>


using namespace std;

const int N_MAX = 1000000;
/*
bool update_num_to_visit(int st, int en, int* arr){
  if(st==-1) return false;
  arr[st]--;
  return arr[st]==0;
  }*/

int main(){
  int n;
  cin >> n;
  stack<pair<int, int> > edges;
  stack<pair<int, int> > paths;
  
  int** adjl = new int*[n];
  int* d = new int[n];
  int* f = new int[n];
  
  for(int i=0; i<n; i++){
    adjl[i] = new int[n];
    memset(adjl[i], -1, n * sizeof(adjl[i][0]));
  }
  
  memset(d, -1, n*sizeof(d[0]));
  memset(f, -1, n*sizeof(f[0]));

  for(int i=0; i<n; i++){
    int row_num, k;
    cin >> row_num >> k; row_num--;
    
    for(int j=0; j<k; j++){
      int node_num;
      cin >> node_num; node_num--;
      adjl[row_num][j] = node_num; // [[3, 5, -1 , ...], [1, 3, -1, ...], [1, 2], ...]
    }
  }

  for(int row_num=n-1; row_num>=0; row_num--){
      pair<int, int> p;
      p.first = -1; p.second = row_num; // visited from nowhere
      edges.push(p);
    }

  int time = 1;
  while(!edges.empty()){
    //    cout << "time: " << time << endl;
    
    pair<int, int> edge;
    edge = edges.top(); edges.pop();
    int st, en; st = edge.first; en = edge.second;
    //    cout << "visiting: " << en << " from: " << st << endl;
    
    pair<int, int> path; 
    // record path to go back
    if(st > -1){ // exclude (-1, node)
      path.first = en; path.second = st;
      // avoid inf loop: if the node is not finished, have to go back at some point
      if(f[path.second] == -1){
	paths.push(path);
	// cout << "<" << path.first << ", " << path.second << "> pushed as a path." << endl;
      }
    }    
    
    // if first time visit, mark as visited
    if(d[en] == -1) d[en] = time;
    
    // if not first time, check if it was finished.
    // if it was already finished, continue
    if(f[en] != -1){
      continue;
    }
    
    // counter for opened nodes
    int unvisited_node_num = 0;

    // open node (reverse order)
    for(int i=n-1; i>=0; i--){
      int next_node = adjl[en][i];
      // cout << "next_node: " << next_node << endl;
      // undefined dest or visited, continue
      if(next_node == -1) continue;
      if(d[next_node] != -1){
	// if num_to_visit[en] > 0;
	continue;
      }
      // if unvisited, push into stack
      unvisited_node_num++;
      pair<int, int> next_edge; next_edge.first = en; next_edge.second = next_node;
      edges.push(next_edge);
      // cout << "<" << next_edge.first << ", " << next_edge.second << "> pushed." << endl;
    }

    // if there are no unvisited linked nodes anymore, mark the node finished
    if(unvisited_node_num == 0){
      // cout << "going back..." << endl;
      f[en] = time + 1;
      // cout << "f[" << en << "]: " << f[en] << endl;
      
      // way to go back
      if(paths.empty()) break;
      path = paths.top(); paths.pop();
      edges.push(path);
      // cout << "path pushed." << endl;
    }
    time++;
  }

  // print 'i+1, d[i], f[i]'
  
  for(int i=0; i<n; i++){
    cout << i+1 << " " << d[i] << " " << f[i] << endl;
  }
  
  for(int i=0; i<n; i++){
    delete[] adjl[i];
  }
  delete[] adjl;
  delete[] d;
  delete[] f;
  
  return 0;
}

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

int mat[MAX][MAX] = {};
int n, time = 0, d[MAX] = {}, f[MAX] = {};
bool c[MAX];

void dfs_visit(int u){
    c[u] = true;
    d[u] = ++time;
    for(int i = 0; i < n; i++){
        if(mat[u][i] == 0)  continue;
        if(!c[i])   dfs_visit(i);
    }
    f[u] = ++time;
}

void dfs(){
    for(int i = 0; i < n; i++){
        if(!c[i])   dfs_visit(i);
    }

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

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    dfs();
    return 0;
}

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

int n;
int a[100][100] = {0};
int k[100] = {0};
int t = 1;
int d[100] = {0};
int f[100] = {0};

int kansu( int i ){
	if ( d[i] != 0 ) return 0;
	d[i] = t;
	t++;
	for ( int j = 0; j < k[i]; j++ ) {
		if ( kansu( a[i][j] ) == 1 ) t++;
	}
	f[i] = t;
	return 1;
}

int main(){
	cin >> n;
	int s;
	for ( int i = 0; i < n; i++ ) {
		cin >> s;
		cin >> k[i];
		for ( int j = 0; j < k[i]; j++ ) {
			cin >> s;
			a[i][j] = s-1;
		}
	}
	kansu (0);
	for ( int i = 0; i < n; i++ ) {
		cout << i+1 << " " << d[i] << " " << f[i] << endl;
	}
	return 0;
}