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<bits/stdc++.h>
using namespace std;

int times;
vector<vector<int> > lists;
vector<int> stampA;
vector<int> stampB;

void dfs(int point){
    times++;
    stampA.at(point-1)=times;
    for(int i=0;i<(int)lists.at(point-1).size();i++){
        if(stampA.at(lists.at(point-1).at(i)-1)==0) dfs(lists.at(point-1).at(i));
    }
    times++;
    stampB.at(point-1)=times;
    
}

int main(){
    times=0;
    int n;
    cin >> n;
    lists.resize(n,vector<int>());
    stampA.resize(n,0);
    stampB.resize(n,0);
    for(int i=0;i<n;i++){
        int u,k;
        cin >> u >> k;
        for(int j=0;j<k;j++){
            int v;
            cin >> v;
            lists.at(u-1).push_back(v);
        }
    }
    
    for(int i=0;i<n;i++){
        if(stampA.at(i)==0) dfs(i+1);
    }
    
    for(int i=0;i<n;i++){
        cout << i+1 << " " << stampA.at(i) << " " << stampB.at(i) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int a[101][101],start[101],end[101],n,p=0,v[101];
void dfs(int u){
    if(!v[u]){v[u]++;
    start[u]=++p;
    r(i,1,n+1)if(a[u][i])dfs(i);
    end[u]=++p;}
}
int main(){
    int q,qq,qqq;
    cin>>n;
    r(i,0,n){cin>>q>>qq;
        r(i,0,qq){cin>>qqq;
            a[q][qqq]++;
        }
    }
    r(i,1,n+1)if(!v[i])dfs(i);
    r(i,1,n+1)cout<<i<<' '<<start[i]<<' '<<end[i]<<endl;
}

C++: /* ALDS1_11_B: Depth First Search */

#include <iostream>
#include <cstdio>
#include <vector>
using std::vector;

vector<int> *graph;
int elapsed = 1;

// Search the graph recursively by depth-first search.
void traverse(int id, int found[], int finished[]) {
    if (0 < found[id]) {
        return;
    }

    found[id] = elapsed++;
    vector<int> degrees = graph[id];
    for (int i = 0; i < degrees.size(); i++) {
        traverse(degrees[i], found, finished);
    }
    finished[id] = elapsed++;
}

int main() {
    int n_v;
    scanf("%d", &n_v);

    int max_v = n_v + 1;
    graph = new vector<int>[max_v];

    for (int i = 0; i < max_v; i++) {
        int id, n_degrees;
        scanf("%d %d", &id, &n_degrees);

        for (int j = 0; j < n_degrees; j++) {
            int deg_id;
            scanf("%d", &deg_id);
            graph[id].push_back(deg_id);
        }
    }

    int found[max_v];
    int finished[max_v];
    std::fill_n(found, max_v, 0);
    std::fill_n(finished, max_v, 0);

    traverse(1, found, finished);

    for (int id = 1; id < max_v; id++) {
        printf("%d %d %d\n", id, found[id], finished[id]);
    }
    return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

int N, Start[105], Goal[105];
vector <int> Graph[105];

int Time = 0;
int DFS(int node){
  for (int i = 0; i < Graph[node].size(); i++){
    int next = Graph[node][i];
    if (Start[next] == 0){
      Time++;
      Start[next] = Time;
      DFS(next);
    }
  }
  Time++;
  Goal[node] = Time;
  return 0;
}

int main(){
  cin >> N;
  for (int i = 0; i < N; i++){
    int M, K; cin >> M >> K;
    for (int j = 0; j < K; j++){
      int Node; cin >> Node;
      Graph[M].push_back(Node);
    }
  }

  for (int i = 1; i <= N; i++){
    if (Graph[i].size() > 0) sort(Graph[i].begin(), Graph[i].end());
  }
  
  for (int i = 1; i <= N; i++){
    if (Start[i] == 0){
      Time++; Start[i] = Time; DFS(i);
    }
  }
  

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

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void dfs(int i, vector<vector<int>> a, vector<int> &d, vector<int> &f) {
	int n = a.size();
	int next = -1;
	REP(j, n) {
		if (d[j] == 0 && a[i][j]) {
			if (next == -1) d[j] = d[i] + 1;
			else d[j] = f[next] + 1;
			next = j;
			dfs(j, a, d, f);
		}
	}
	if (next == -1) f[i] = d[i] + 1;
	else f[i] = f[next] + 1;
}

void dfs_all(vector<vector<int>> a, vector<int> &d, vector<int> &f) {
	int i = 0;
	int n = a.size();
	int end = -1;
	while (1) {
		if (end == -1) d[0] = 1;
		else {
			while (d[i] > 0) i++;
			if (i == n) return;
			d[i] = f[end] + 1;
		}
		end = i;
		dfs(i, a, d, f);
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n, 0));
	REP(i, n) {
		int u, k;
		cin >> u >> k;
		u--;
		REP(j, k) {
			int v;
			cin >> v;
			a[u][--v] = 1;
		}
	}
	vector<int> d(n + 1, 0);
	d[0] = 1;
	vector<int> f(n);
	dfs_all(a, d, f);
	
	REP(i, n) {
		cout << i + 1 << " " << d[i] << " " << f[i] << "\n";
	}

	return 0;
}