Go: package main

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

/***************************************************************
stackによる深さ優先探索を実装する。
***************************************************************/

const (
	N     = 100
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

var (
	n            int
	Adj          [N][N]int
	statusColor  [N]int
	discoverTime [N]int
	finishTime   [N]int
	clock        int
	nt           [N]int
)

/***************************************************************
Stackの定義:
- field
	container: ノードの格納されたスライス
	sp: スタックポインタ
- method
	size(): スタックに積まれているノードの個数を返す
	top(): スタックの一番上に積まれているノードを返す
	isEmpty(): スタックが空かどうかを返す
	pop(): スタックの要素をポップし削除する
	push(): スタックに要素を積む
***************************************************************/

type Stack struct {
	container []int
	sp        int
}

func (s *Stack) size() int {
	return len(s.container)
}

func (s *Stack) top() int {
	return s.container[s.sp]
}

func (s *Stack) isEmpty() bool {
	return s.sp == 0
}

func (s *Stack) pop() int {
	_pop := func() {
		s.container = s.container[:len(s.container)-1]
	}
	defer _pop()
	if s.isEmpty() {
		panic("There is no elements in stack!")
	}
	s.sp--
	return s.container[s.sp]
}

func (s *Stack) push(n int) {
	s.sp++
	s.container[s.sp] = n
}

func next(u int) int {
	for v := nt[u]; v < n; v++ {
		nt[u] = v + 1
		if Adj[u][v] == 1 {
			return v
		}
	}
	return -1
}

func _DFS(target int) {
	for i := 0; i < n; i++ {
		nt[i] = 0
	}
	stack := new(Stack)
	stack.container = make([]int, N+1)
	stack.push(target)
	statusColor[target] = GRAY
	clock++
	discoverTime[target] = clock
	for !stack.isEmpty() {
		u := stack.top()
		v := next(u)
		if v != -1 {
			if statusColor[v] == WHITE {
				statusColor[v] = GRAY
				clock++
				discoverTime[v] = clock
				stack.push(v)
			}
		} else {
			stack.pop()
			statusColor[u] = BLACK
			clock++
			finishTime[u] = clock
		}
	}
}

func DFS() {
	for i := 0; i < n; i++ {
		statusColor[i] = WHITE
		nt[i] = 0
	}
	clock = 0
	for i := 0; i < n; i++ {
		if statusColor[i] == WHITE {
			_DFS(i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, discoverTime[i], finishTime[i])
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		u--
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ := strconv.Atoi(scanner.Text())
			v--
			Adj[u][v] = 1
		}
	}

	DFS()
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
int n;
int a[123][123];
int st[123];
int ed[123];
int now;
void dfs(int local){
	st[local]=now++;
	for(int i=1;i<=n;i++){
		if(a[local][i]){
			if(st[i]==0){
				dfs(i);
			}
		}
	}
	ed[local]=now++;
}
int main(){
	ios::sync_with_stdio(false);
	while(cin>>n){
		memset(a,0,sizeof(a));
		memset(st,0,sizeof(st));
		memset(ed,0,sizeof(ed));
		for(int i=0;i<n;i++){
			int t,m;
			cin>>t>>m;
			for(int i=0;i<m;i++){
				int te;
				cin>>te;
				a[t][te]=1;
			}
			now=1;
		}
		for(int i=1;i<=n;i++){
			if(!st[i])
				dfs(i);
		}
		for(int i=1;i<=n;i++){
			cout<<i<<" "<<st[i]<<" "<<ed[i]<<endl;
		}
	}
	return 0;
}


//

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define Graph vector<vector<int>>
using namespace std;

void DFS(const Graph &G,int v,vector<bool> &seen,vector<int> &first_order,vector<int> &last_order,int &ptr){//深さ優先探索。
//引数は順にグラフ、始点(注目点)、探索済みかどうか、行きがけでの順番配列、帰りがけでの順番配列、順番カウンタ
//タイムスタンプ型のため、firstとlastでカウンタを共有
  first_order[v]=ptr++;//行きがけのカウント

  seen[v]=true;

  for(auto next_v : G[v]){
    if(seen[next_v]) continue;
    DFS(G,next_v,seen,first_order,last_order,ptr);
  }
  
  last_order[v]=ptr++;//帰りがけのカウント

  return ;

}

 
int main() {

  int n;  cin >> n ;

  Graph G(n+1);//頂点はn個。今回は数値にそのまま対応させるために+1した

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k==0) continue;

    rep(j,k){
    int v;
    cin >> v;
    G[u].push_back(v);
    }
  }//入力終了

  vector<bool> seen(n+1,false);
  vector<int> first_order(n+1),last_order(n+1);
  int ptr=1;//今回はカウント1スタート
  for(int i=1;i<=n;i++){
  if(seen[i]) continue;
  DFS(G,i,seen,first_order,last_order,ptr);
  }

  for(int i=1;i<=n;i++){
    cout << i <<' '<< first_order[i] << ' ' << last_order[i] << endl;
  }

  return 0;
}

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

int n, G[101][101], S[101], T[101];

int dfs(int p, int s) {
    if (S[p] != 0) return 0;
    S[p] = s;
    s++;
    for (int i = 0; i < n; i++) {
        if (G[p][i] == 1) {
            s = max(s, dfs(i, s) + 1);
        }
    }
    T[p] = s;
    return s;
}


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

    dfs(0, 1);

    for (int i = 0; i < n; i++)
        printf("%d %d %d\n", i + 1, S[i], T[i]);

    return 0;
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>; 
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;

vector<bool> seen;
vl d, f;

void dfs(ll now, ll &time, vll &G){
    seen[now] = true;
    d[now] = time++;
    for(ll next : G[now]){
        if(seen[next]) continue;
        dfs(next, time, G);
    }
    f[now] = time++;
}

int main() {
    ll N;
    cin >> N;
    vll G(N);
    rep(i, N){
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k){
            ll t;
            cin >> t;
            t--;
            G[u].push_back(t);
        }
    }
    seen.assign(N, false);
    d.resize(N);
    f.resize(N);
    ll time = 1;
    rep(i, N){
        if(!seen[i]){
            dfs(i, time, G);
        }
    }
    rep(i, N){
        ll idx = i+1;
        cout << idx << " " << d[i] << " " << f[i] << endl;
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int G[101][101];
bool Memo[101];
int d[101];
int f[101];
int count1;

void dfs(int i, int n) {
  if (Memo[i]) {
    count1++;
    d[i] = count1;
    Memo[i] = false;
    for (int j = 0; j < n; j++) {
      if (G[i][j] == 1) {
        dfs(j,n);
      }
    }
    count1++;
    f[i] = count1;
  }
}

int main() {
  int n;
  scanf("%d",&n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int row,col;
    scanf("%d %d",&row,&col);
    for (int j = 0; j < col; j++) {
      int d;
      scanf("%d",&d);
      G[row - 1][d - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++) Memo[i] = true;
  count1 = 0;
  dfs(0,n);
  for (int i = 0; i < n; i++) printf("%d %d %d\n",i+1,d[i],f[i]);
  return 0;
}