Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	currentVertex := &vertices[currentId-1]
	if currentVertex.isVisited() {
		return timestamp
	}
	timestamp = timestamp + 1
	currentVertex.firstVisitAt = timestamp
	for _, adjId := range currentVertex.adjs {
		timestamp = dfs(adjId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func solveDfs(vertices []vertex) {
	timestamp := 0
	for {
		vid := vertexId(-1)
		for i := 0; i < len(vertices); i++ {
			if !vertices[i].isVisited() {
				vid = vertexId(i+1)
				break
			}
		}
		if vid > 0 {
			timestamp = dfs(vid, timestamp, vertices)
		} else {
			break
		}
	}
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveDfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

C++: # include <iostream>
# include <vector>
# include <cstdio>
using namespace std;
struct info
{
	int st;
	int ed;
	bool vis = false;
};
int Time = 1;
vector<int> a[105];
info *ans;
void dfs(int i)
{
	ans[i].st = Time++;
	ans[i].vis = true;
	for(int j = 0;j < a[i].size();j++) if(!ans[a[i][j]].vis) dfs(a[i][j]);
	ans[i].ed = Time++;
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	for(int i = 1;i <= n;i++) if(!ans[i].vis) dfs(i);
	for(int i = 1;i <= n;i++) printf("%d %d %d\n",i,ans[i].st,ans[i].ed);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> d;
vector<int> f;
vector<bool> used;
vector< vector<int> > v;
int n;
int cnt = 1;

void dfs(int pos){
  d[pos] = cnt; cnt++;
  used[pos] = true;
  for(int i = 0; i < n; i++){
    if(v[pos][i] && !used[i]){
      dfs(i);
    }
  }
  f[pos] = cnt; cnt++;
}

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

  cin >> n;
  d = f = vector<int>(n, -1);
  used = vector<bool>(n, false);
  v = vector< vector<int> >(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      int a; cin >> a;
      a--;
      v[u][a] = 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++: //ノードが持つ情報→子のノードの情報
//ノードが子を持たないときはからのvectorを保持する
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>

using namespace std;

//このようにクラスやストラクトなどで一括で管理してしまえば良い
class Node{//普通の木用のデータ
//ノードはポインタでもっておこう
public:
  ///ノードの番号
  int num;
  //そのノードの親
  Node* p;
  //そのノードの子
  vector<Node*> v;
  //そのノードの発見時刻と完了時刻(これは分けて配列の方がいいかもしれない)
  //int d=0;int f=0;
};

//木じゃないのでrootがあるわけじゃない(重要)

//見つからなかったら-1
//非連結な場合もあるので
int find_next_root(vector<int> va){
  int l= int(va.size());
  for(int i=0;i<l;i++){
    if(va[i]==0){return i;}
  }
  return -1;
}


//Nは始まりのNode
//tはmainで管理しするので
void depth_search(Node& N,int& t,vector<int>& va,vector<int>& vf){
  //呼ばれた前の時点での時間が与えられる
  //子を全部見終わったら、そのノードも足し算してそこで終了する
  t++;
  va[N.num]=t;
  int l=N.v.size();
  for(int i=0;i<l;i++){
    if(va[N.v[i]->num]==0) depth_search(*(N.v[i]),t,va,vf);
  }
  t++;
  vf[N.num]=t;
}


int main(){
  int n;cin>>n;
  vector<Node> V(n);for(int i=0;i<n;i++){V[i].num=i;}
  vector<int> Va(n,0);vector<int> Vf(n,0);

  for(int i=0;i<n;i++){
    int k;cin >> k >> k;
    for(int j=0;j<k;j++){
      int l;cin >>l;l-=1;
      V[l].p=&V[i];
      V[i].v.push_back(&V[l]);
    }
  }
  //初めは一つのグラフだと思ってたけど非連結な場合もある
  int t=0;
  int x=0;
  while(x!=-1){
    depth_search(V[x],t,Va,Vf);
    x=find_next_root(Va);
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << Va[i] << " " << Vf[i] << endl;
  }
}

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

void dfs(int cur);
int df[100][2]={};
bool dc[100]={};
bool adj[100][100]={};//初期化を忘れない
int n,u,v,k;
int t = 1;

int main(){
  cin >> n;
  for (int i=0; i<n; i++){
    cin >> u >> k;
    for (int j=0; j<k; j++){
      cin >> v;
      adj[u-1][v-1] = true;
    }
  }
  
 
  dfs(0);
  
  for (int i=0; i<n; i++)
    cout << i+1 << " " << df[i][0] << " " << df[i][1] << endl;

}

void dfs(int cur){
  dc[cur] = true;
  df[cur][0] = t;
  t += 1;
  for (int i=0; i<n; i++){
    if (adj[cur][i] && !dc[i]){
      dfs(i);
    }
  }
  df[cur][1] = t; 
  t += 1;
}

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

using namespace std;

const int max_v = 101;
vector<vector<int> > adj_list;
int d[max_v] = { 0 };
int f[max_v] = { 0 };
int discovered[max_v] = { 0 };
int t = 1;

// Depth-first Search ( Stack implementation)
void DFS(int v)
{
	stack<int> S;
	S.push(v);
	discovered[v] = 1;
	d[v] = t++;
	int i = 0;
	while (!S.empty()) {
		v = S.top();
		if (i < adj_list[v - 1].size()) {
			int u = adj_list[v - 1][i++];
			if (discovered[u] == 0) {
				discovered[u] = 1;
				d[u] = t++;
				S.push(u);
				i = 0;
			}
		}
		else {
			S.pop();
			f[v] = t++;
			i = 0;
		}
	}
}

int main(int argc, char** argv)
{
	// Input
	int n;
	int u, k;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		int v;
		vector<int> adj;
		for (int j = 0; j < k; j++) {
			cin >> v;
			adj.push_back(v);
		}
		adj_list.push_back(adj);
	}

	// Depth-first Search
	for (int i = 1; i <= n; i++) {
		if(discovered[i] == 0)
			DFS(i);
	}

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

	return 0;
}

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

# define WHITE 0
# define GREY  1
# define BLACK 2

int M[100][100];
int color[100], d[100], f[100];
int n, tt = 0;

void dfs_visit(int u) {
	color[u] = GREY;
	d[u] = ++tt;
	for (int 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() {
    for (int i = 0; i < n; i++) color[i] = WHITE;
	for (int i = 0; i < n; i++) {
	    if ( color[i] == WHITE ) dfs_visit(i);
	}
}

int main() {
    int u, v, k;
	scanf("%d", &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++) {
		scanf("%d %d", &u, &k);
		u--;
		for (int j = 0; j < k; j++) {
			scanf("%d", &v);
			v--;
			M[u][v]++;
		}
	}
	dfs();
	for (u = 0; u < n; u++) {
	    printf("%d %d %d\n", u+1, d[u], f[u]);
	}
	return 0;
}