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 <cstdio>
#include <stack>

#define N 100

using namespace std;

int d[N],f[N],m[N][N],ucolor[N],t,n,nt[N];

int next(int u2){
	int r;
	for(r=nt[u2];r<n;r++){
		nt[u2] = r+1;
		if(m[u2][r]){
			return r;
		}
	}
	return -1;
}

void dfsvisit(int uu){
	int u3=0,v,i;
	for(i=0;i<n;i++){
		nt[i] = 0;
	}
	stack<int> s;
	s.push(uu);
	ucolor[uu] = 1;
	d[uu] = ++t;
	while( !s.empty() ){
		u3 = s.top();
		v = next(u3);
		if(v != -1){
			if(ucolor[v] == 0){
				ucolor[v] = 1;
				d[v] = ++t;
				s.push(v);
			}
		}else{
			s.pop();
			ucolor[u3] = 2;
			f[u3] = ++t;
		}
	}
}

void dfs(){
	int i,j;
	for(i=0;i<n;i++){
		ucolor[i] = 0;
		nt[i] = 0;
	}
	for(j=0;j<n;j++){
		if(ucolor[j]==0){
			dfsvisit(j);
		}
	}
	for(i=0;i<n;i++){
		printf("%d %d %d\n",i+1,d[i],f[i]);
	}
}

int main(void){
	int i,j,u1,k,box;
	t = 0;// 時間の初期化
	for(i=0;i<N;i++){
		for(j=0;j<N;j++)m[i][j]=0;
	}
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&u1,&k);
		u1--; //頂点番号、その隣接する頂点の数
		for(j=0;j<k;j++){
			scanf("%d",&box);
			box--; // 隣接する頂点の番号
			m[u1][box] = 1;
		}
	}
	dfs();
	return 0;
}

C++: //
//  main.cpp
//  619
//
//  Created by 8595198919 on 14/06/16.
//  Copyright (c) 2014年 8595198919. All rights reserved.
//

#define _2
#ifdef _1
#include <iostream>
#include <cstdio>
#include <queue>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;

int dist[105];
queue<int> que;
vector<int> G[105];
int main(int argc, const char * argv[])
{
    int n;
    cin >> n;
    for(int i = 0;i <= n;i++) {
        dist[i]=-1;
    }
    for(int i = 0;i < n;i++){
        int u,k;
        int to;
        scanf("%d %d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d",&to);
            G[u].push_back(to);
        }
    }
    que.push(1);
    dist[1]=0;
    while(!que.empty()){
        int now = que.front();
        que.pop();
        int d = dist[now];
        rep(i,G[now].size()){
            if(dist[G[now][i]]==-1){
                dist[G[now][i]]=d+1;
                que.push(G[now][i]);
            }
        }
    }
    for(int i = 1;i <= n;i++) {
        cout <<i <<" " << dist[i]<<endl;
    }
}
#endif

#ifdef _2

#include <iostream>
#include <cstdio>
#include <queue>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;


vector<int> G[105];
int d[105];
int f[105];
int tm = 1;
void dfs(int p){
    d[p]=tm;
    rep(i,G[p].size()){
        if(d[G[p][i]]==-1){
            tm++;
            dfs(G[p][i]);
        }
    }
    f[p] = ++tm;
    return;
}
int main(int argc, const char * argv[])
{
    int n;
    cin >> n;
    for(int i = 0;i <= n;i++) {
        d[i]=-1;
        f[i]=-1;
    }
    for(int i = 0;i < n;i++){
        int u,k;
        int to;
        scanf("%d %d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d",&to);
            G[u].push_back(to);
        }
    }
    dfs(1);
    for(int i = 1;i <= n;i++){
        printf("%d %d %d\n",i,d[i],f[i]);
    }
}

#endif

C++: #include <stack>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
#define N 100
int n, m[N][N];
int color[N], d[N], f[N], tt;
int gcd(int a, int b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}
int mod = 1000000000 + 7;
void dfs_visit(int i){
    color[i] = 1;
    d[i] = ++tt;
    for(int j = 0; j < n; j++){
        if(m[i][j] == 0){
            continue;
        }
        if(color[j] == 0){
            dfs_visit(j);
        }
    }
    color[i] = 2;
    f[i] = ++tt;
}
void dfs(){
    for(int i = 0; i < n; i++){
        color[i] = 0;
    }
    tt = 0;
    for(int i = 0; i < n; i++){
        if(color[i] == 0){
            dfs_visit(i);
        }
    }
    for(int i = 0; i < n; i++){
        cout << i+1 << " "<<  d[i] << " " << f[i] << endl;
    }
}
int main(){
    //std::stack<int> stack;
    int u, k, v;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            m[i][j] = 0;
        }
    }
    for(int i = 0; i < n; i++){
        cin >> u >> k;
        u --;
        for(int j = 0; j < k; j++){
            cin >> v;
            v--;
            m[u][v] = 1;
        }
    }
    dfs();
    //cout << masu << endl;
    return 0;
}

C++: #include <cstdio>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[N][N];
int color[N],d[N],f[N],tt;

//??¨???????????°?????°?????±?????????????´¢
void dfs_visit(int u){
    int v;
    color[u]=GRAY;
    //???????????????
    d[u]=++tt;
    for(v=0;v<n;v++){
        if(M[u][v]==0)
            continue;
        if(color[v]==WHITE)
            dfs_visit(v);
    }
    color[u]=BLACK;
    //????????????
    f[u]=++tt;
}

void dfs(){
    int u;
    //????§????
    for(u=0;u<n;u++)
        color[u]=WHITE;
    tt=0;
    
    for(u=0;u<n;u++)
        //??\????????????u?????????????????±?????????????´¢
        if(color[u]==WHITE)
            dfs_visit(u);
    for(u=0;u<n;u++)
        printf("%d %d %d\n",u+1,d[u],f[u]);
}

int main(){
    int u,v,k;
    
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            M[i][j]=0;
    
    for(int i=0;i<n;i++){
        scanf("%d %d",&u,&k);
        u--;
        for(int j=0;j<k;j++){
            scanf("%d",&v);
            v--;
            M[u][v]=1;
        }
    }
    
    dfs();
}