Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 1
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	g := graph{}
	g.ee = make([]Edges, n)
	g.visited = make([]bool, n)
	g.found = make([]int, n)
	g.leave = make([]int, n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner) - 1
		k := getNextInt(scanner)
		for j := 0; j < k; j++ {
			v := getNextInt(scanner) - 1
			g.ee[u] = append(g.ee[u], Edge{
				to: v,
				id: u,
			})
		}
	}
	for i := 0; i < n; i++ {
		sort.Sort(g.ee[i])
	}
	for i := 0; i < n; i++ {
		if g.visited[i] {
			continue
		}
		g.dfs(i)
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d %d", i+1, g.found[i], g.leave[i]))
	}
}

// Edge ...
type Edge struct {
	to, id int
}

type Edges []Edge

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

type graph struct {
	ee           []Edges
	t            int
	n            int
	visited      []bool
	found, leave []int
}

func (g *graph) dfs(i int) {
	g.visited[i] = true
	g.t++
	g.found[i] = g.t
	for _, e := range g.ee[i] {
		if g.visited[e.to] {
			continue
		}
		g.dfs(e.to)
	}
	g.t++
	g.leave[i] = g.t
}

C++: #include<iostream>
#include<stack>
#include<queue>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

string col[3] = {"WHITE", "GRAY", "BLACK"};
struct Node{
	int color, d, f;
	queue<int> Q;
};

int main(){
	int n;
	cin >> n;
	Node node[n + 1];
	stack<int> move;
	for(int i = 1; i <= n; i++){
		int u, k, v;
		cin >> u >> k;
		node[u].color = WHITE;
		while(k--){
			cin >> v;
			node[u].Q.push(v);
 		}
	}

	int start = 1;
	int tt = 1, p, next;

	do{
		move.push(start);
		node[start].color = GRAY;
		node[start].d = tt;
		tt++;

		while(!move.empty()){
			p = move.top();
			//cout << "Is at " << p << endl;
			if(node[p].Q.empty()){
				//cout << "Cant move from here!" << endl;
				node[p].color = BLACK;
				node[p].f = tt;
				move.pop();
				tt++;
			} else {
				next = node[p].Q.front();
				if(node[next].color == WHITE){
					//cout << "Moving to " << next << endl;
					move.push(next);
					node[next].color = GRAY;
					node[next].d = tt;
					tt++;
				} else {
					//cout << "Cant move to " << next << endl;
					node[p].Q.pop();
				}
			}
			/*
			for(int i = 1; i <= n; i++)
				cout << i << "'s color is " << col[node[i].color] << endl;
			*/
		}

		start = -1;
		for(int i = 1; i <= n; i++){
			if(node[i].color == WHITE){
				start = i;
				break;
			}
		}
		//cout << "start = " << start << endl;
	} while (start > 0);
	

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

C++: #include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define SORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
using ll = long long;
using namespace std;

#define WHITE 1
#define GRAY 2
#define BLACK 3

int now, n, u, k, v, tree[102][102], timeTable[102][2], color[102], timeNow = 1;
stack<int> st;

void dfs(){
    while(1){
        if(st.empty()){
            FOR(l, 1, n + 1){
                cout << l << " " << timeTable[l][0] << " " << timeTable[l][1] << endl;
            }
            break;
        }else{
            now = st.top();
          	if(color[now] == WHITE){
            	timeTable[now][0] = timeNow;
            }
            color[now] = GRAY;
            int cnt = 1;
            while(1){
                if(cnt == n + 1){
                    st.pop();
                    color[now] = BLACK;
                    timeTable[now][1] = timeNow + 1;
                    break;
                }else if(tree[now][cnt] && color[cnt] == WHITE){
                    st.push(cnt);
                    break;
                }
                cnt++;
            }
            timeNow++;
        }
    }
}

int main(){
    cin >> n;
  	REP(i, 102){
    	REP(j, 102){
        	tree[i][j] = 0;
        }
    }
    REP(i, n){
        cin >> u >> k;
        color[i + 1] = WHITE;
        REP(j, k){
            cin >> v;
            tree[u][v] = 1;
        }
    }
    st.push(1);
    dfs();
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<vector<int>> G;
int N;
vector<int> d;
vector<int> f;
int de = 0;
void dfs(int v,int p){
    d[v] = ++de;
    for(auto nv:G[v]){
        if(d[nv]!=-1) continue;
        dfs(nv,v);
    }
    f[v] = ++de;
}
signed main(){
    stack<int> st;
    cin >> N;
    G.resize(N);
    d.assign(N,-1);
    f.assign(N,-1);
    for(int i=0;i<N;i++){
        int id;cin >> id;
        int k;cin >> k;
        for(int j=0;j<k;j++){
            int a;cin >> a;a--;
            G[i].push_back(a);
        }
    }
    /*st.push(0);
    int dep=0;
    while(!st.empty()){
        int now = st.top();
        dep++;
        d[now] = dep;
        st.pop();
        for(int nv:G[now]){
            if(d[nv]!=-1) continue;
            st.push(nv);
        }
    }*/
    for(int i=0;i<N;i++){
        if(d[i]==-1) dfs(i,-1);
    }
    for(int i=0;i<N;i++){
        cout << i+1 << " " << d[i] << " " << f[i] << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
int t = 0;

void visit(int u, int *color, int *d, int *f, vector<vector<int>> Adj){
    int zero = 0;
    color[u] = GRAY;
    d[u] = ++t;
    for (int i = 1; i <= Adj[u].size(); i++){
        if (Adj[u][i] == 0){
            break;
        }
        if (color[Adj[u][i]] == WHITE){
            visit(Adj[u][i], color, d, f, Adj);
        }
    }
    color[u] = BLACK;
    f[u] = ++t;
}

int main(int argc, char* argv[]) {
    int n, u, k, v;
    cin >> n;
    n++;
    int color[n], d[n], f[n];
    vector<vector<int>> Adj(n, vector<int>(n, 0));

    for (int i = 1; i <= n; i++) color[i] = WHITE;

    for (int i = 1; i < n; i++){
        cin >> u >> k;
        for (int j = 1; j <= k; j++){
            cin >> v;
            Adj[i][j] = v;
        }
    }

    for (int i = 1; i < n; i++)
        if (color[i] == WHITE)
            visit(i, color, d, f, Adj);

    for (int i = 1; i < n; i++){
        printf("%d %d %d\n", i, d[i], f[i]);
    }
    return 0;
}