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<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

vector<pii> visit;
int order=1;

void dfs(vector<vi> &G, int v){
    visit[v].first=order;
    order++;
    for(auto nv: G[v]){
        if(visit[nv].first != -1) continue;
        dfs(G, nv);
    }
    visit[v].second=order;
    order++;
}

int main(){
    int n; cin >> n;
    vector<vi> G(n+1);
    visit.assign(n+1, {-1,-1});
    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);
        }
    }
    for(int i=1; i<=n; i++){
        if(visit[i].first != -1) continue;
        dfs(G, i);
    }
    for(int i=1; i<=n; i++){
        cout << i << " " << visit[i].first << " " << visit[i].second << endl;
    }
    return 0;
}

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;
}