Go: package main

import "fmt"

type node struct {
	id, d, v int
	adj      []node
}

var n int
var seen []bool
var graph []node

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

func dfs(v, t int) int {
	// fmt.Printf("dfs(v %d, t %d) en:\n", v, t)
	seen[v] = true
	graph[v].v = t
	tt := t
	for _, vv := range graph[v].adj {
		if !seen[vv.id] {
			tt = max(tt, dfs(vv.id, tt+1))
		}
	}
	graph[v].d = tt + 1
	// fmt.Printf("dfs(v %d, t %d) ex: d %d\n", v, t, graph[v].d)
	return graph[v].d
}

func main() {
	fmt.Scan(&n)
	graph = make([]node, n+1)
	for i := 1; i <= n; i++ {
		var u, k, v int
		fmt.Scan(&u, &k)
		graph[u].id = u
		// 頂点uはk個の隣接頂点を持っている
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			var t node
			t.id = v
			graph[u].adj = append(graph[u].adj, t)
		}
	}
	// fmt.Println("n", n, "graph", graph)
	seen = make([]bool, n+1)
	// t := dfs(1, 1)
	v, t := 1, 1
	done := false
	for !done {
		tt := dfs(v, t)
		done = true
		for i := range seen {
			if !seen[i] {
				done = false
				v = graph[i].id
				t = tt
				break
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, graph[i].v, graph[i].d)
	}
}

C++: #include <cstdio>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

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

//DFS with recursive function
void dfs_visit(int u){
    int v;
    color[u] = GRAY;
    d[u] = ++tt; //first visit 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] = ++tt; //done visit time
}

void dfs(){
    int u;
    //initialization
    for (u=0; u<n; u++){
        color[u] = WHITE;
    }
    tt = 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(){
    int u, v, k, i, j;

    scanf("%d", &n);
    for (i=0; i<n; i++){
        for (j=0; j<n; j++){
            M[i][j] = 0;
        }
    }

    for (i=0; i<n; i++){
        scanf("%d %d", &u, &k);
        u--;
        for (j=0; j<k; j++){
            scanf("%d", &v);
            v--;
            M[u][v] = 1;
        }
    }

    dfs();

    return 0;

}

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

int t;
vector<int> d, f;
int n;
vector<vector<int> > G;

void dfs(int v) {
  if ( d[v] ) return;
  d[v] = ++t;  

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

  f[v] = ++t;  
}

int main() {
  cin >> n;

  d = vector<int>(n, 0);
  f = vector<int>(n, 0);

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

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

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

C++: /*depth first search
not lie in stack
*/
#include <iostream>
using namespace std;
static const int N=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

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

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

void dfs(){
	int u;
	for(int i=0;i<n;i++){
		color[i]=WHITE;
	}
	tt=0;
	
	for(u=0;u<n;u++){
		if(color[u]==WHITE)
			dfs_visit(u);
	}
	
	for(int i=0;i<n;i++)
		cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
}

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

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

//#include<bits/stdc++.h>



using namespace std;




int main() {
	
	int n;
	cin>>n;
	vector<vector<int>>t(n,vector<int>(n));
	for(int i=0;i<n;i++){
		int x,r;cin>>r>>x;
		for(int j=0;j<x;j++){
			int w;cin>>w;w--;t[i].push_back(w);
		}
		sort(t[i].begin(),t[i].end());
	}
	stack<int>S;
	bool F[105]={};
	S.push(0);
	int p=1;
	F[0]=1;
	vector<int>G(n),H(n);
	int g=1;
	G[0]=1;
	while(S.size()){
		g++;
		int v=S.top();
		int a=-1;
		for(int i=0;i<t[v].size();i++){
			if(!F[t[v][i]]){
				a=t[v][i];break;
			}
		}
		if(a==-1){
			H[S.top()]=g;
			S.pop();
		}
		else{
			F[a]=1;
			G[a]=g;
			S.push(a);
		}
	}
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<G[i]<<" "<<H[i]<<endl;
	}
	

	
	return 0;
}

C++: #include<stdio.h>
#include<stack>
using namespace std;

enum Color{
    WHITE,
    GRAY,
    BLACK
};

int n;
bool M[100][100];   //隣接行列
int nt[100];    //next用
stack<int> S;           //深さ優先探索のスタック
Color color[100];   //各頂点の訪問状態

int d[100]; //この問題
int f[100]; //この問題
int time = 1;

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(int i){
    S.push(i);
    color[i] = GRAY;
    d[i] = time++;

    while(!S.empty()){
        int u=S.top();
        int v=next(u);  //隣接している頂点を順番に取得
        if(v!=-1){
            if(color[v]==WHITE){
                color[v] = GRAY;
                d[v] = time++;
                S.push(v);
            }
        }else{
            S.pop();
            color[u] = BLACK;
            f[u] = time++;
        }
    }

}

void dfs_init(){
    for(int i=0; i<n; i++){
        color[i] = WHITE;
        nt[i] = 0;
    }
    for(int i=0; i<n; i++){
    if(color[i]==WHITE) dfs(i); //スタート地点
    }

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

int main(){

    scanf("%d", &n);

    for(int i=0; i<n; i++){
        int u, k;
        scanf("%d %d", &u, &k);
        u--;
        for(int j=0; j<k; j++){
            int v;
            scanf("%d", &v);
            v--;
            M[u][v] = true;    //辺を引く
        }
    }

    dfs_init();

    return 0;
}

C++: #include <iostream>

using namespace std;

int graph[101][101]={};
int d[101]={};
int f[101]={};
int n,v,k,time1 = 0,sw[101] = {};


void dfs1(int u){

  sw[u] = 1;
  d[u] = ++time1;

  for(int i = 1; i <= n; i++){
    if( graph[u][i] == 1 && sw[i] == 0 ){
     dfs1(i);
    }

  }
    f[u] = ++time1;
}


int main(){
  int u;
  cin >> n ;
  //グラフをつくる
  for(int i = 0; i < n; i++){
    cin >> u >> k ;
    for(int j = 0; j < k ; j++){
      cin >> v;
      graph[u][v] = 1;
    }
  }

  dfs1(1);

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

  }
}

C++: #include <stdio.h>

int A[101][101];
int ans[101][3];
int an = 1;

int serch(int a[][], int  ni, int n){
  int  j;
  
  for(j=0; j<n; j++){
    if(A[ni][j] == 1){
      judge(j, 1);
      serch(A, j, n);
    }
  }
  judge(ni, 2);
}

int judge(int i, int j){
  if(ans[i][j] == 0){
    ans[i][j] = an;
    an++;
  }
}

int  main(){
  int i, j, k, n, x, y;
  
  scanf("%d", &n);
  
  for(i=0; i<n; i++){
    for(j=0; j<n; j++)
      A[i][j] = 0;
    ans[i][0] = i+1;
  }
  
  for(i=0; i<n; i++){
    scanf("%d %d", &x, &y);
    for(j=0; j<y; j++){
      scanf("%d", &k);
      A[i][k-1] = 1;
    }
  }
  
  judge(0, 1);
  
  for(i=0; i<n; i++){
    if(A[0][i] == 1){
      judge(i, 1);
      serch(A, i, n);
    }
  }
  judge(0, 2);
  
  for(i=0; i<n; i++){
    for(j=0; j<3; j++){
      if(j>0)
        printf(" ");
      printf("%d", ans[i][j]);
    }
    printf("\n");
  }
  
  return 0;
}