Go: package main

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

type TimeRecord struct {
	discoverTime int
	finishTime   int
}

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

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

	timeRecordMap := make([]TimeRecord, n)
	childrenMap := make([][]int, n)
	for i := 1; i <= n; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		timeRecordMap[node-1] = TimeRecord{-1, -1}

		childNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		childrenMap[node-1] = make([]int, childNum)
		for j := 0; j < childNum; j++ {
			child, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			childrenMap[node-1][j] = child
		}
	}

	timer := 1
	for {
		startNode := findStartNode(childrenMap, timeRecordMap)
		if startNode > 0 {
			visit(startNode, &timer, childrenMap, timeRecordMap)
		} else {
			break
		}
	}
	for k, v := range timeRecordMap {
		fmt.Printf("%d %d %d\n", k+1, v.discoverTime, v.finishTime)
	}
}

func findStartNode(childrenMap [][]int, timeRecordMap []TimeRecord) int {
	x := -1
	for node, timeRecord := range timeRecordMap {
		if timeRecord.discoverTime < 0 {
			x = node + 1
			break
		}
	}
	if x < 0 {
		return -1
	}

	found := false
	i := 0
	for i < len(childrenMap) {
		children := childrenMap[i]
		for j := 0; j < len(children); j++ {
			if children[j] == x {
				x = j + 1
				found = true
				break
			}
		}
		if found {
			i = 0
		} else {
			i++
		}
	}
	return x
}

func visit(currentNode int, timer *int, childrenMap [][]int, timeRecordMap []TimeRecord) {
	if timeRecordMap[currentNode-1].discoverTime > 0 {
		return
	}
	children := childrenMap[currentNode-1]
	timeRecordMap[currentNode-1] = TimeRecord{*timer, timeRecordMap[currentNode-1].finishTime}
	(*timer)++
	for i := 0; i < len(children); i++ {
		visit(children[i], timer, childrenMap, timeRecordMap)
	}
	timeRecordMap[currentNode-1] = TimeRecord{timeRecordMap[currentNode-1].discoverTime, *timer}
	(*timer)++
}

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

C++: #include <bits/stdc++.h>
#define REP0(i,n) for(int i=0;i<n;i++)
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
int n,M[N][N];
int color[N],D[N],F[N],tt;
int nt[N];
int next(int u){
  for(int v=nt[u];v<n;v++){
    nt[u]=v+1;
    if(M[u][v]) return v;
  }
  return -1;
}
void DFS_visit(int r){
  REP0(i,n) nt[i]=0;
  stack<int> S;
  S.push(r);
  color[r]=GRAY;
  D[r]=++tt;
  while(!S.empty()){
    int u=S.top();
    int v=next(u);
    if(v!=-1){
      if(color[v]==WHITE){
        color[v]=GRAY;
        D[v]=++tt;
        S.push(v);
      }
    } else{
      S.pop();
      color[u]=BLACK;
      F[u]=++tt;
    }
  }
}
void DFS(){
  REP0(i,n){
    color[i]=WHITE;
    nt[i]=0;
  }
  tt=0;
  REP0(u,n) if(color[u]==WHITE) DFS_visit(u);
  REP0(i,n) printf("%d %d %d\n",i+1,D[i],F[i]);
}
int main(){
  int u,k,v,j;
  scanf("%d",&n);
  REP0(i,n){
    REP0(j,n) M[i][j]=0;
  }
  REP0(i,n){
    scanf("%d %d",&u,&k);
    REP0(j,k){
      scanf("%d",&v);
      M[--u][--v]=1;
    }
  }
  DFS();
  return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

#define MAXN 100
int n, timecount = 1;
int ans[MAXN][MAXN];
int d[MAXN], f[MAXN]; // d[i]はdを最初に発見した時刻,f[i]は調べ終わった時刻
bool is_reach[MAXN];

int dfs(int p);

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        d[i] = -1;
        f[i] = -1;
        is_reach[i] = false;
    }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            ans[i][j] = 0;
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            ans[u-1][tmp-1] = 1;
        }
    }
//    for (int i = 0; i < n; i++) {
//        for (int j = 0; j < n; j++) {
//            if (j == n-1) cout << ans[i][j];
//            else cout << ans[i][j] << " ";
//        }
//        cout << endl;
//    }
    int start = 0;
    while (1) {
        dfs(start);
        int i;
//        printf("%d search end\n", start);
        for (i = 0; i < n; i++) {
            if (f[i] == -1) {
                start = i;
                break;
            }
        }
        if (i == n) break;
//        printf("next: %d\n", start);
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d %d\n", i+1, d[i], f[i]);
    }
    return 0;
}

int dfs(int p)
{
    int ret = 0, count = 1;
    is_reach[p] = true;
    if (d[p] == -1) d[p] = timecount++;
    for (int i = 0; i < n; i++) {
        if (ans[p][i] == 1 && is_reach[i] == false && p != i) {
            dfs(i);
        }
    }
    if (f[p] == -1) f[p] = timecount;
    timecount++;
    return 0;
}

C++: /*
　　こんにちは。あたしはｶｳｶﾞｰﾙ。
　　redcoderになるためAtCoderを巡る旅をしてます。

　　　 　 ＿_
　　　 ヽ|＿_|ノ　　　　ﾓｫ
　　　　||‘‐‘||ﾚ　　_)_, ―‐ 、
　　　　/(Ｙ (ヽ＿ /・ ヽ　　 ￣ヽ
　　　　∠ ＿ ゝ　 ｀^ヽ ﾉ.::::_(ノヽ
　　　　 _/ヽ　 　　  /ヽ￣￣/ヽ
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define rep(i, n) REP(i, 0, n)                  // 0, 1, ..., n-1
#define REP(i, x, n) for(int i = x; i < n; i++) // x, x + 1, ..., n-1
#define FOREACH(x, a) for(auto &(x) : (a))

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

#define COUT(x) cout << (x) << endl

template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

using Graph = vector<vector<int>>;
vector<bool> seen;
vector<int> first_order;
vector<int> second_order;

void dfs(const Graph &G, int v, int &ptr) {
    first_order[v] = ptr++;
    seen[v]        = true;

    for(const auto &next_v : G[v]) {
        if(seen[next_v]) continue;
        dfs(G, next_v, ptr);
    }

    second_order[v] = ptr++;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    Graph G(n + 1); // 1-indexed
    rep(i, n) {
        int v, k;
        cin >> v >> k;
        rep(i, k) {
            int to_v;
            cin >> to_v;
            G[v].push_back(to_v);
        }
    }
    // 頂点1スタート
    seen.assign(n + 1, false);
    first_order.resize(n + 1);
    second_order.resize(n + 1);
    int ptr = 1;
    REP(i, 1, n + 1) {
        if(!seen[i]) dfs(G, i, ptr);
    }

    REP(i, 1, n + 1) {
        cout << i << " " << first_order[i] << " " << second_order[i] << endl;
    }
}

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 k=0;k<n;++k){
		table[k*n+i] = 0;
	}
	for(int j=0;j<n;++j){
		if(table[i*n+j]){
			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;
}