Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) doesAllAdjsVisited(vertices []vertex) bool {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return false
		}
	}
	return true
}

func (v *vertex) isFirstVisit() bool {
	return v.firstVisitAt == 0
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func (v *vertex) nextVertex(vertices []vertex) (vertexId, error) {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return vId, nil
		}
	}
	return vertexId(-1), fmt.Errorf("illegal")
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	timestamp = timestamp + 1
	currentVertex := &vertices[currentId-1]
	if currentVertex.isFirstVisit() {
		currentVertex.firstVisitAt = timestamp
	}
	for !currentVertex.doesAllAdjsVisited(vertices) {
		nextVertexId, _ := currentVertex.nextVertex(vertices)
		timestamp = dfs(nextVertexId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	dfs(vertexId(1), 0, vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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