Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 最初にiを訪問した時刻。発見時刻
//f[i]: iの隣接リストを訪問し終えた時刻。完了時刻
var color, d, f [MAX]int

func dfs_visit(u int) {
	color[u] = GRAY
	time++
	d[u] = time
	//隣接を調べる
	for v := 0; v < n; v++ {
		if M[u][v] == 0 {
			continue
		}
		//隣接するvが、未訪問だった場合
		if color[v] == WHITE {
			dfs_visit(v)
		}
	}
	//訪問完了処理
	color[u] = BLACK
	time++
	f[u] = time
	//訪問完了
}

func dfs() {
	//初期化
	for u := 0; u < n; u++ {
		color[u] = WHITE
	}
	time = 0

	//未訪問のuを始点として、深さ優先探索
	for u := 0; u < n; u++ {

		if color[u] == WHITE {
			dfs_visit(u)
		}
	}

	for u := 0; u < n; u++ {
		fmt.Println(u+1, d[u], f[u])
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	dfs()

}

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

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[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(){
    stack<int> S;
    S.push(0);
    dst[0] = 0;
    while(!S.empty()){
        int cur = S.top();
        S.pop();
        if(dst[cur]==-1 || dst[cur]== 0){
            for(int i=0;i<num;i++){
                if(matrix[cur][i]==1){
                    S.push(i);
                    dst[i] = dst[cur] + 1;
                }
            }
        }
        else{
            for(int i=0;i<num;i++){
                if(matrix[cur][i]==1){
                    S.push(i);
                    if(dst[cur] +1 <= dst[i])
                        dst[i] = dst[cur] + 1;
                }
            }
        }
    }
    for(int i=0;i<num;i++){
        cout << i+1 << " " <<  dst[i] << endl;
    }
}

int main(){
    input();
    dfs();
}

C++: #include <iostream>

using namespace std;

#define MAX 100

int n,Time;
int G[MAX][MAX],d[MAX],f[MAX];
bool used[MAX];

void dfs(int v){
  d[v] = Time++;
  used[v] = true;

  for(int i = 0 ; i < n ; i++){
    if(!used[i] && G[v][i]){
      dfs(i);
    }
  }

  f[v] = Time++;
}

int main(){
  cin >> n;

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

  Time = 1;
  for(int i = 0 ; i < n ; i++){
    if(!used[i]){
      dfs(i);
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

static const int N=100;
static const int white=0;
static const int gray=1;
static const int black=2;
int color[N],nt[N];
int mp[N][N];
int n,d[N],f[N],tt;
int next(int r){
    
    for(int i=nt[r];i<n;i++){
        nt[r]=i+1;
        if(mp[r][i])return i;
        
    }
    return -1;
    
}

void dfs_visit(int r){
    rep(i,n){nt[i]=0;}
    stack<int> s;
    s.push(r);
    d[r]=++tt;
    color[r]=gray;
    
    while(!s.empty()){
        int u=s.top();
        int v=next(u);
        if(v!=-1){
            if(color[v]==white){
            d[v]=++tt;
            color[v]=gray;
            s.push(v);
            }
        }else{
            f[u]=++tt;
            color[u]=black;
            s.pop();
        }
    }
}



void dfs(){
    tt=0;
    rep(i,n){
        color[i]=white;nt[i]=0;
    }
    rep(i,n){
        if(color[i]==white){
            dfs_visit(i);
            
        }
    }
    rep(i,n){
        cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}


int main(){
    cin>>n;
    rep(i,N){
        rep(j,N){
            mp[i][j]=0;
        }
    }
    rep(i,n){
        int a,b;
        cin>>a>>b;
        a--;
        rep(i,b){
            int c;
            cin>>c;
            c--;
            mp[a][c]=1;
        }
    }
    dfs();
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

using namespace std;

#define FIND 0
#define END 1

int  N;
bool Path[101][101] = {false};
int  TIME[101][2] = {0};
int time_tt;


void DFS(int i){
    if(TIME[i][FIND] != 0) return;
    time_tt++;
    TIME[i][FIND] = time_tt;
    
    for (int p = 1; p <= N; p++) {
        if (Path[i][p]) DFS(p);
    }
    
    time_tt++;
    TIME[i][END] = time_tt;
}

int main(){
    cin >> N;
    int bb;
    for (int i = 1; i <= N; i++) {
        int K;
        scanf("%d %d",&bb,&K);
        for (int k = 0; k < K; k++) {
            int p;
            scanf("%d",&p);
            
            Path[i][p] = true;
        }
    }
    
    time_tt = 0;
    
    for (int i = 1; i <= N; i++) {
        DFS(i);
    }
    for (int i = 1; i <= N; i++) {
        printf("%d %d %d\n",i,TIME[i][FIND],TIME[i][END]);
    }
    
    return 0;
}

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

int d[101]={0};
int f[101]={0};
int AdjM[101][101]={0};
int lapse=0;

void dfs(int cur){
    ++lapse;
    d[cur]=lapse;
    cerr<<"visiting "<<cur<<" "<<lapse<<"\n";
    for(int dst=1;dst<101;++dst){
        if(AdjM[cur][dst]!=0 && d[dst]==0){
            dfs(dst);
        }
    }
    ++lapse;
    f[cur]=lapse;
}

int main(){
    int N;
    cin>>N;
    int id,edgnum,adj;
    for(int i=0;i<N;++i){
        cin>>id>>edgnum;
        for(int j=0;j<edgnum;++j){
            cin>>adj;
            AdjM[id][adj]+=1;
        }
    }

    dfs(1);
    
    for(int i=1;i<=N;++i){
        cout<<i<<" "<<d[i]<<" "<<f[i]<<"\n";
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

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

const ll INF=(ll)1e19;
//const int INF=(1<<30);

int n;
int s[100], e[100];
vector<vector<int>> edge(100,vector<int>());

int dfs(int v, int t){
  if(s[v]!=0)return t-1;
  s[v] = t;
  REP(i,edge[v].size()){
    int nx = edge[v][i];
    if(s[nx] != 0)continue;
    t = dfs(nx, t+1);
  }
  e[v] = ++t;
  return t;
}

int main(){
  cin >> n;
  
  REP(i,n){
    int u;
    cin >> u;
    int k;
    cin >> k;
    REP(j,k){
      int v;
      cin >> v;
      v--;
      edge[i].emplace_back(v);
    }
  }

  int t = 0;
  REP(i,n){
    t = dfs(i,t+1);
  }

  REP(i,n){
    printf("%d %d %d\n", i+1, s[i], e[i]);
  }


}