Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	f         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []*DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]*DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes] = &DirectedGraphNode{}
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].f = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

var time = 0

func fillDistancesByDepthFirstSearch(g *DirectedGraph) {
	for _, u := range g.nodes[1:] {
		if !u.visited {
			dfsVisit(g, u)
		}
	}
}

func dfsVisit(g *DirectedGraph, u *DirectedGraphNode) {
	time++
	u.d = time

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	fillDistancesByDepthFirstSearch(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		fmt.Println(node.key, node.d, node.f)
	}
}

C++: /* ALDS1_11_B: Depth First Search */

#include <iostream>
#include <cstdio>
#include <vector>
using std::vector;

vector<int> *graph;
int elapsed = 1;

// Search the graph recursively by depth-first search.
void traverse(int id, int found[], int finished[]) {
    if (0 < found[id]) {
        return;
    }

    found[id] = elapsed++;
    vector<int> degrees = graph[id];
    for (int i = 0; i < degrees.size(); i++) {
        traverse(degrees[i], found, finished);
    }
    finished[id] = elapsed++;
}

int main() {
    int n_v;
    scanf("%d", &n_v);

    int max_v = n_v + 1;
    graph = new vector<int>[max_v];

    for (int i = 0; i < max_v; i++) {
        int id, n_degrees;
        scanf("%d %d", &id, &n_degrees);

        for (int j = 0; j < n_degrees; j++) {
            int deg_id;
            scanf("%d", &deg_id);
            graph[id].push_back(deg_id);
        }
    }

    int found[max_v];
    int finished[max_v];
    std::fill_n(found, max_v, 0);
    std::fill_n(finished, max_v, 0);

    traverse(1, found, finished);

    for (int id = 1; id < max_v; id++) {
        printf("%d %d %d\n", id, found[id], finished[id]);
    }
    return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

int N, Start[105], Goal[105];
vector <int> Graph[105];

int Time = 0;
int DFS(int node){
  for (int i = 0; i < Graph[node].size(); i++){
    int next = Graph[node][i];
    if (Start[next] == 0){
      Time++;
      Start[next] = Time;
      DFS(next);
    }
  }
  Time++;
  Goal[node] = Time;
  return 0;
}

int main(){
  cin >> N;
  for (int i = 0; i < N; i++){
    int M, K; cin >> M >> K;
    for (int j = 0; j < K; j++){
      int Node; cin >> Node;
      Graph[M].push_back(Node);
    }
  }

  for (int i = 1; i <= N; i++){
    if (Graph[i].size() > 0) sort(Graph[i].begin(), Graph[i].end());
  }
  
  for (int i = 1; i <= N; i++){
    if (Start[i] == 0){
      Time++; Start[i] = Time; DFS(i);
    }
  }
  

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

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void dfs(int i, vector<vector<int>> a, vector<int> &d, vector<int> &f) {
	int n = a.size();
	int next = -1;
	REP(j, n) {
		if (d[j] == 0 && a[i][j]) {
			if (next == -1) d[j] = d[i] + 1;
			else d[j] = f[next] + 1;
			next = j;
			dfs(j, a, d, f);
		}
	}
	if (next == -1) f[i] = d[i] + 1;
	else f[i] = f[next] + 1;
}

void dfs_all(vector<vector<int>> a, vector<int> &d, vector<int> &f) {
	int i = 0;
	int n = a.size();
	int end = -1;
	while (1) {
		if (end == -1) d[0] = 1;
		else {
			while (d[i] > 0) i++;
			if (i == n) return;
			d[i] = f[end] + 1;
		}
		end = i;
		dfs(i, a, d, f);
	}
}

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

	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n, 0));
	REP(i, n) {
		int u, k;
		cin >> u >> k;
		u--;
		REP(j, k) {
			int v;
			cin >> v;
			a[u][--v] = 1;
		}
	}
	vector<int> d(n + 1, 0);
	d[0] = 1;
	vector<int> f(n);
	dfs_all(a, d, f);
	
	REP(i, n) {
		cout << i + 1 << " " << d[i] << " " << f[i] << "\n";
	}

	return 0;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX 105
vector<int> V[MAX];

int d[MAX], f[MAX];

int t;

void search(int v) {
	if (d[v] != 0)return;
	d[v] = t++;

	for (int i = 0; i < V[v].size(); i++) {
		search(V[v][i]);
	}
	f[v] = t++;
}

int main() {
	t = 1;
	int n;
	cin >> n;
	for (int i = 1; i < n + 1; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int s;
			cin >> s;
			V[u].push_back(s);
		}
	}
	for (int i = 1; i < n + 1; i++) {
		if (d[i] == 0)search(i);
	}
	for (int i = 1; i < n + 1; i++) {
		cout << i << ' ' << d[i] << ' ' << f[i] << endl;
	}

	return 0;
}

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

int m[101][101];
int visit[101];
int d[101];
int f[101];
int n, t;

void dfs(int u){
  visit[u] = 1;
  d[u] = t;
  t++;
  
  for(int i = 0; i < n; ++i){
    if(m[u][i] == 1 && visit[i] == 0){
      dfs(i);
    }
  }

  f[u] = t;
  t++;
}

main()
{
  int u, k, v;
  
  cin >> n;
  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;
    }
  }

  for(int i = 0; i < n; ++i){
    if(visit[i]==0){
      t=1;
      dfs(i);
    }
  }

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

  return (0);
}