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>
using namespace std;
int visited[100]={0};
int V[100][100]={0};
int start[100]={0};
int finish[100]={0};
int t=1;

void dfs(int i,int n){
  start[i]=t++;
  visited[i]++;
  for(int j=0;j<n;j++){
    if(V[i][j]==1 and visited[j]==0){
      dfs(j,n);
    }
  } 
  finish[i]=t++;
}



int main () {
    int n,k;
    int p1, p2;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            V[i][j] = 0;
        }
  }
  for(int i=0;i<n;i++){
    cin>>p1>>k;
    for(int j=0;j<k;j++){
      cin>>p2;
      V[p1-1][p2-1]=1;
    }
  }
  for(int i=0;i<n;i++){
    if(visited[i]==0){
      dfs(i,n);
    }
  }
  for(int i=0;i<n;i++){
      cout << i + 1 << " ";
      cout<<start[i] << " " << finish[i] << "\n";
  }


  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define VMAX 50
#define EMAX 500
#define DMAX 20
#define TRUE 1
#define FALSE 0
#define NOTUSED 0
#define END -1
#define ROOT -1

// グラフの隣接頂点リスト表現
struct GraphAV{
    int p;  // 頂点数
    int q;  // 辺数
    // int AV[VMAX][DMAX]; 
    vector<vector<int>> AV;  // 隣接頂点リスト
};

GraphAV G;
// int d[VMAX],f[VMAX];
// bool seen[VMAX];
vector<int> d(VMAX);
vector<int> f(VMAX);
vector<bool> seen(VMAX);
int t=1;
void DFS(int v,int& t);

int main(){
    cin>>G.p;

    d.resize(G.p+1);
    f.resize(G.p+1);
    G.AV.resize(G.p+1);
    seen.resize(G.p+1);
    G.AV[0]={NOTUSED};
    d[0]=NOTUSED;
    
    for(int i=1; i<=G.p; i++){
        int u,k;
        cin>>u>>k;
        G.AV[u].resize(k);
        for(int j=0; j<k; j++){
            cin>>G.AV[u][j];
        }
    }

    for(int i=1; i<=G.p; i++){
        if(seen[i]){
            continue;
        }
        DFS(i,t);
    }

    for(int i=1; i<=G.p; i++){
        cout<<i<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}

void DFS(int v,int& t){
    d[v]=t++;

    seen[v]=true;
    // v から行ける各頂点 adj_v について
    for(int adj_v:G.AV[v]){
        if(seen[adj_v]) continue; // adj_v が探索済なら何もしない
        DFS(adj_v,t);  // 再帰的に探索
    }

    f[v]=t++;
}

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

int n, u, k, v;
int d[101], f[101];
pair<int, vector<int>> G[101];

int ti = 1;

void dfs(int id) {
	d[id] = ti ++;

	vector<int>& adj = G[id].second;
	for (int i=0, l=adj.size(); i<l; ++i) {
		if ( ! d[adj[i]]) {
			dfs(adj[i]);
		}
	}
	f[id] = ti ++;
}

int main() {
	cin >> n;
	for (int i=0; i<n; ++i) {
		cin >> u >> k;
		G[u].first = u;
		while (k --) {
			cin >> v;
			G[u].second.push_back(v);
		}
	}
	dfs(1);

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

C++: #include <cstdio>
#include <vector>
using namespace std;
int Begin;
int step=1;
int root[105]={0};
typedef struct{
	int id;
	vector<int>next;
	int num;
	int find;
	int finish;
}inf;
void input(int cases,inf table[]){
	for(int i=0;i<cases;i++){
		int id;
		scanf("%d",&id);
		table[id].id=id;
		scanf("%d",&table[id].num);
		for(int i=0;i<table[id].num;i++){
			int next;
			scanf("%d",&next);
			table[id].next.push_back(next);
			root[next]=1;
		}
		table[id].find=-1;
		table[id].finish=-1;
	}
}
void dfs(inf table[],int now){
	if(table[now].num==0){
		table[now].find=step;
		step++;
		table[now].finish=step;
		step++;
		return;
	}else{
		if(table[now].find==-1){
			table[now].find=step;
		}
		step++;
		for(int i=0;i<table[now].num;i++){
			if(table[table[now].next[i]].find==-1){
				dfs(table,table[now].next[i]);				
			}
		}
		table[now].finish=step;
		step++;
	}
}
void find_begin(int cases){
	for(int i=1;i<=cases;i++){
		if(root[i]==0){
			Begin=i;
			break;
		}
	}
}
int main(){
	int cases;
	scanf("%d",&cases);
	inf table[105];
	input(cases,table);
	find_begin(cases);
	dfs(table,Begin);
	for(int i=1;i<=cases;i++){
		printf("%d %d %d\n",i,table[i].find,table[i].finish);
	}
}

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

void DFS(vector<vector<int> >& G,
        vector<pair<int, int> >& rec, int i) {
    if (rec[i].first != -1) return;
    static int t = 1;
    rec[i].first = t++;
    for (int j = 1; j <= G[i].size(); j++) {
        if (G[i][j]) DFS(G, rec, j);
    }
    rec[i].second = t++;
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1, vector<int>(n + 1, 0));
    for (int i = 1; i <= n; i++) {
        int num, k;
        cin >> num >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[i][v] = 1;
        }
    }
    vector<pair<int, int> > record(n + 1, make_pair(-1, -1));
    int t = 1;
    for (int i = 1; i <= n; i++) {
        DFS(G, record, i);
    }

    for (int i = 1; i <= n; i++) {
        cout << i << " " << record[i].first
                  << " " << record[i].second << endl;
    }

    return 0;
}