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<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int N,u,k,x,r=0;
vector<int>V[105];
bool f[105]={0};
P c[105];

void dfs(int p)
{
	if(f[p])return;
	r++;
	f[p]=1;
	c[p].first=r;
	for(int i=0;i<V[p].size();i++)
	{
		dfs(V[p][i]);
	}
	r++;
	c[p].second=r;
}

int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		scanf("%d%d",&u,&k);u--;
		for(int j=0;j<k;j++)
		{
			scanf("%d",&x);x--;
			V[u].push_back(x);
		}
		sort(V[u].begin(),V[u].end());
	}
	dfs(0);
	for(int i=0;i<N;i++)
		printf("%d %d %d\n",i+1,c[i].first,c[i].second);
	return 0;
}

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

void search(int i,int *table, int n, int *d, int *f, int *cnt){
	
	++(*cnt);
	d[i] = *cnt;
	
	for(int j=0;j<n;++j){
		if(table[i*n+j]){
			for(int k=0;k<n;++k){
				table[k*n+j] = 0;
			}
			search(j,table,n,d,f,cnt);
		}
	}
	
	++(*cnt);
	f[i] = *cnt;
}

int main() {
	int n,*table,*d,*f, cnt;
	
	cin>>n;
	table = new int[n*n];
	d = new int[n];
	f = new int[n];
	
	for(int i=0;i<n;++i){
		int u,k,v;
		cin>>u>>k;
		for(int j=0;j<k;++j){
			cin>>v;
			table[(u-1)*n+v-1]=1;
		}
	}
	cnt = 0;
	
	while(true){
		int rest = 0;
		for(int i=0;i<n;++i){
			for(int j=0;j<n;++j){
				if(table[i*n+j]){
					rest = 1;
					break;
				}
			}
			if(rest){
				search(i,table,n,d,f,&cnt);
				break;
			}
		}
		if(!rest){
			break;
		}
	}
	
	for(int i=0;i<n;++i){
		cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
	}
	
	return 0;
}

C++: #include <cstdio>
using namespace std;
int tim = 0;
bool vis[107];
int G[107][107], d[107], f[107];
void dfs(int cur) {
	vis[cur] = true;
	d[cur] = ++tim;
	for (int i = 0; G[cur][i]; i++) {
		if (!vis[G[cur][i]]) dfs(G[cur][i]);
	}
	f[cur] = ++tim;
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
    	int u, k;
    	scanf("%d%d", &u, &k);
    	for (int j = 0; j < k; j++) {
    		scanf("%d", &G[u][j]);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (!vis[i]) dfs(i);
	}
	for (int i = 1; i <= n; i++) {
		printf("%d %d %d\n", i, d[i], f[i]);
	}
    return 0;
}

C++: #include<stdio.h>
#define N 100
#define WHITE 0
#define GREY 1
#define BLACK 2

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

void dfs_visit(int u)
{
	int v;
	color[u] = GREY;
	d[u] = ++time;
	for(v = 0; v < n; v++)
	{
		if(M[u][v] == 0)
			continue;
		if(color[v] == WHITE)
			dfs_visit(v);
	}
	color[u] = BLACK;
	f[u] = ++time;
}

void dfs()
{
	int u;
	for(u = 0; u < n; u++)
		color[u] = WHITE;
	time = 0;
	
	for(u = 0; u < n; 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()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			M[i][j] = 0;
	int u, k, v;
	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();
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#define N 101

using namespace std;

int visited[N], nt[N], discovery[N], finish[N], tt, n;
vector<int> G[N];

int next(int u) {
    for (int v = nt[u]; v < G[u].size(); v++) {
        nt[u] = v + 1;
        return G[u][v];
    }
    return -1;
}

void dfs(int r) {
    memset(nt, 0, sizeof(nt));

    stack<int> S;
    S.push(r);

    visited[r] = 1;
    discovery[r] = ++tt;

    while (!S.empty()) {
        int u = S.top();
        int v = next(u);

        if (v != -1) {
            if (visited[v] == 0) {
                visited[v] = 1;
                discovery[v] = ++tt;
                S.push(v);
            }
        }
        else {
            S.pop();
            visited[u] = 1;
            finish[u] = ++tt;
        }
    }
}

int main() {
    int u, k, v;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    memset(visited, 0, sizeof(visited));
    tt = 0;

    for (int i = 0; i < n; i++) {
        if (visited[i] == 0) {
            dfs(i);
        }
    }

    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << discovery[i] << " " << finish[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <stack>

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[MAX];
int counter = 0;
int D[MAX];
int F[MAX];

void input(){
    cin >> num;
    for(int i=0;i<MAX;i++){
        dst[i] = -1;
        for(int j=0;j<MAX;j++){
            matrix[i][j] = 0;
        }
    }
    for(int i=1;i<=num;i++){
        int v; cin >> v;
        int tmp; cin >>tmp;
        for(int j=0;j<tmp;j++){
            int vi; cin >> vi;
            matrix[v-1][vi-1] = 1;
        }
    }
}

void dfs(int cur){
    counter++;
    D[cur] = counter;
    for(int i=0; i< num;i++){
        if(matrix[cur][i] == 1 && D[i]== 0)
            dfs(i);
    }
    counter++;
    F[cur] = counter;
    return;
}

int main(){
    input();
    for(int i=0;i<MAX;i++){
        D[i] = 0;
        F[i] = 0;
    }
    dfs(0);
    for(int i=0;i<num;i++){
        cout << i+1 << " " << D[i] << " " << F[i] << endl;
    }
}