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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
#include <iostream>
#include <cstdio>
#include <queue> // 入れた順番に出していく玉突き構造。
using namespace std;

int G[110][110];
int n;
int u, k, v;

/* void print_matrix(int N) {
  for(int i=0; i<N; i++) {
    for(int j=0; j<N; j++) {
      printf("%d", G[i][j]);
      if (j < N-1) {
	printf(" ");
      }
    }
      printf("\n");
  }
} */

void make_matrix() {
  fill(&G[0][0], &G[0][0]+110*110, 0);
  scanf("%d", &n);
  for (int i=0; i<n; i++) {
    scanf("%d%d", &u, &k);
    u--;
    for (int j=0; j<k; j++) {
      scanf("%d", &v);
      v--;
      G[u][v] = 1;
    }
  }
}

int timer = 1; // timeだとエラーが出たので。
int d[110]; // 訪問時刻。ナマの数字を入れる。
int f[110]; // 終了時刻。ナマの数字を入れる。

void init() {
  fill(d, d+110, -1);
  fill(f, f+110, -1);
}

void dfs(int cur) { // curを訪問。
  d[cur] = timer;
  timer++;
  // cerr << "visiting " << cur << " at the time " << timer << endl;
  for (int i=1; i<=n; i++) { // 節点i
    if (G[cur-1][i-1] == 1 && d[i] == -1) {
      dfs(i);
    }
  }
  f[cur] = timer;
  timer++;
}

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

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

int	dfs(const Graph &graph,
		vector<int> &d_vec, vector<int> &f_vec, int u, int cur_time)
{
	d_vec.at(u) = ++cur_time;
	for (int v : graph.at(u)) {
		if (d_vec.at(v) != -1)
			continue;
		cur_time = dfs(graph, d_vec, f_vec, v, cur_time);
	}
	f_vec.at(u) = ++cur_time;
	return cur_time;
}	
		
int	main()
{
	int	n;
	cin >> n;
	Graph graph(n);
	vector<int>	d_vec(n, -1);
	vector<int>	f_vec(n, -1);
	int	k;
	int	u, v;
	for (int i = 0; i < n; ++i) {
		cin >> u >> k;
		--u;
		for (int j = 0; j < k; ++j) {
			cin >> v;
			--v;
			graph.at(u).push_back(v);
		}
	}
	int cur_time = 0;
	for (int i = 0; i < n; ++i) {
		if (d_vec.at(i) != -1)
			continue;
		cur_time = dfs(graph, d_vec, f_vec, i, cur_time);
	}
	for (int i = 0; i < n; ++i) {
		cout << i + 1 << " " << d_vec.at(i) << " " << f_vec.at(i) << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
const int MAX=100;
const int WHITE=0;
const int GRAY=1;
const int BLACK=2;
using namespace std;
int M[MAX][MAX],d[MAX],f[MAX],tt,n,color[MAX];

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





void dfs(){
    int i;
    for(i=0;i<n;i++){
        color[i]=0;
    }
    tt=0;
    for(i=0;i<n;i++){
        if( color[i]==WHITE)dfs_visit(i);
    }
    for(i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}
int main(){
  //  cout<<"A"<<endl;
    cin>>n;
    int u,v,k;
    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++: #include <iostream>
#include <vector>

using namespace std;

const int MAX = 101;

vector<int> G[MAX];
int d[MAX], f[MAX];
int *T;

void dfs(int u) {

    if (!d[u]) d[u] = ++(*T);

    for (int v : G[u]) {
        if (!d[v]) d[v] = ++(*T);
        dfs(v);
    }

    if (!f[u]) f[u] = ++(*T);
}

int main() {

    int n;
    cin >> n;

    int t = 0;
    T = &t;
    fill(d, d + MAX, 0);
    fill(f, f + MAX, 0);

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

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


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

C++: #include <iostream>
#include <map>
#include <vector>

using namespace std;
struct node {
    node() {};
    node(int i, int n, int* xs) : id {i}, ns {vector<int>(xs, xs+n)} {
        delete xs;
    };
    int id;
    vector<int> ns;
    int in_time;
    int out_time;
};

int dfs(map<int, node*> graph, int time, int id){
    node* n = graph[id];
    if(n->in_time > 0){
        return time;
    }
    n->in_time = ++time;
    vector<int> ns = n->ns;
    for(auto it = ns.begin(); it != ns.end(); ++it){
        time = dfs(graph, time, *it);
    }
    n->out_time = ++time;
    return time;
}

int main(){
    int n;
    cin >> n;
    map<int, node*> graph {};
    int st = 1000000000;
    for(int i = 0; i < n; ++i){
        int id;
        cin >> id;
        st = min(st, id);
        int k;
        cin >> k;
        int* ns = new int[k];
        for(int j = 0; j < k; ++j){
            cin >> ns[j];
        }
        graph[id] = new node(id, k, ns);
    }
    int time = 0;
    for(auto it = graph.begin(); it != graph.end(); ++it){
        pair<int, node*> p = *it;
        if(p.second->in_time){
            continue;
        }
        time = dfs(graph, time, p.first);
    }
    for(auto it = graph.begin(); it != graph.end(); ++it){
        pair<int, node*> p = *it;
        cout << p.first << ' ' << p.second->in_time << ' ' << p.second->out_time << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define WHITE 0
#define GRAY 1
#define BLACK 2
vector<vector<int>> M(100, vector<int>(100));
vector<int> C(100);
int n, tt;
vector<int> ST(100), PT(100);

int next(int u) {
  rep(i, n) {
    if (M.at(u).at(i) == 1 && C.at(i) == WHITE) {
      return i;
    }
  }
  return -1;
}

void dfs_visit(int u) {
  stack<int> S;
  S.push(u);
  ST.at(u) = ++tt;
  C.at(u) = GRAY;
  while (S.size() > 0) {
    int t = S.top();
    int v = next(t);
    if (v!=-1) {
      if (C.at(v) == WHITE) {
        S.push(v);
        ST.at(v) = ++tt;
        C.at(v) = GRAY;
      }
    } else {
      S.pop();
      PT.at(t) = ++tt;
      C.at(t) = BLACK;
    }
  }
}

void dfs() {
  tt = 0;
  rep(u, n) if (C.at(u) == WHITE) dfs_visit(u);
}

int main() {
  cin >> n;
  rep(i, n) {
    int id, k; cin >> id >> k;
    rep(j, k) {
      int num; cin >> num;
      M.at(id-1).at(num-1) = 1;
    }
  }
  dfs();

  rep(i,n) cout << i+1 << " " << ST.at(i) << " " << PT.at(i) << endl;
  return 0;
}