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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
#include <iostream>
#include <cstdio>
#include <queue> // 入れた順番に出していく玉突き構造。
using namespace std;

int G[110][110];
int n;
int u, k, v;

/* void print_matrix(int N) {
  for(int i=0; i<N; i++) {
    for(int j=0; j<N; j++) {
      printf("%d", G[i][j]);
      if (j < N-1) {
	printf(" ");
      }
    }
      printf("\n");
  }
} */

void make_matrix() {
  fill(&G[0][0], &G[0][0]+110*110, 0);
  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", &v);
      v--;
      G[u][v] = 1;
    }
  }
}

int timer = 1; // timeだとエラーが出たので。
int d[110]; // 訪問時刻。ナマの数字を入れる。
int f[110]; // 終了時刻。ナマの数字を入れる。

void init() {
  fill(d, d+110, -1);
  fill(f, f+110, -1);
}

void dfs(int cur) { // curを訪問。
  d[cur] = timer;
  timer++;
  // cerr << "visiting " << cur << " at the time " << timer << endl;
  for (int i=1; i<=n; i++) { // 節点i
    if (G[cur-1][i-1] == 1 && d[i] == -1) {
      dfs(i);
    }
  }
  f[cur] = timer;
  timer++;
}

int main() {
  make_matrix();
  init();
  for (int i=1; i<=n; i++) {
    if (d[i] == -1) {
      dfs(i);
    }
  }
  for (int i=1; i<=n; i++) {
    printf("%d %d %d\n", i, d[i], f[i]);
  }
}

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

using namespace std;

int  n, M[100][100], d[100], f[100], color[100], tt, nt[100], W = 0, G = 1, B = 2;

int next (int u) {

    int v;
    for (v = nt[u]; v < n; v++) {
        nt[u] = v + 1;
        if (M[u][v] != 0) {
            return v;
        }
    }
    return -1;

}

void dfs_visit (int r) {

    int i, u, v;

    for (i = 0; i < n; i++) {
        nt[i] = 0;
    }

    stack<int> S;
    S.push(r);
    color[r] = G;
    d[r] = ++tt;

    while (!S.empty()) {
        u = S.top();
        v = next (u);
        if (v != -1) {
            if (color[v] == W) {
                color[v] = G;
                d[v] = ++tt;
                S.push(v);
            }
        } else {
            S.pop();
            color[u] = B;
            f[u] = ++tt;
        }
    }

}

void dfs () {

    int i, u;

    for (i = 0; i < n; i++) {
        color[i] = W;
        nt[i] = 0;
    }
    time = 0;

    for (u = 0; u < n; u++) {
        if (color[u] == W) {
            dfs_visit(u);
        }
    }

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

}

int main () {

    int u, k, v, i, j;

    cin >> n;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }

    for (i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (j = 0; j < k; j++) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    dfs ();

    return 0;

}

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

#define MAX 105

struct Point{
    int id;
    int d;
    int f;
    bool flag;
    std::vector<int> child;
};

int stack[MAX];
int sp = 0;
int count = 1;

Point p[MAX];

void push(int id){
    if(p[id].flag == false){
        stack[sp++] = id;
        p[id].d = count++;
        p[id].flag = true;
    }
}

void pop(){
    p[stack[--sp]].f = count++;
    //return stack[sp];
}

void dft(int id){
    //std::cout << id << "に対しdft\n";
    push(id);
    int size = p[id].child.size();
    for(int i=0;i<size;i++){
        if(p[p[id].child[i]].flag == false){
            dft(p[id].child[i]);
        }
    }
    pop();
}

int main(){
    int n;
    int size;
    std::cin >> n;
    for(int i=1;i<=n;i++){
        std::cin >> p[i].id >> size;
        for(int j=0;j<size;j++){
            int tmp;
            std::cin >> tmp;
            p[i].child.push_back(tmp);
        }
    }
    for(int i=1;i<=n;i++){
        if(p[i].flag == false){
            dft(i);
        }
    }
    
    for(int i=1;i<=n;i++){
        std::cout << p[i].id << ' ' << p[i].d << ' ' << p[i].f << '\n';
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

int N;
vint G[100];
int FLG[100];
int D[100];
int F[100];
const int NIL = -1;
int tt;

void dfs(int ii){

	tt++;
	D[ii] = tt;

	// cout << "reach " << ii << " " << tt << endl;

	rep(i, G[ii].size()){
		int to = G[ii][i];
		if (D[to] == NIL){
			dfs(to);
		}
	}

	tt++;
	F[ii] = tt;
	// cout << "back " << ii << " " << tt << endl;

}

void run_dfs(){
	rep(i, N){ D[i] = NIL; }
	int start = 0;
	tt = 0;
	rep(i, N){
		if (D[i] == NIL) dfs(i);
	}
}

int main() {

	cin >> N;
	rep(i, N){
		int id, n; cin >> id >> n; id--;
		rep(j, n){
			int id_to; cin >> id_to; id_to--;
			G[id].push_back(id_to);
		}
	}

	run_dfs();

	rep(i, N){
		cout << i << " " << D[i] << " " << F[i] << endl;
	}
}