Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var depth [101]int
var finish [101]int
var d [101]int
var n, time int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func visit(graph [][]int, u int) {
	depth[u] = 1
	time++
	d[u] = time
	for i := 0; i < n; i++ {
		if graph[u][i] == 1 {
			if depth[i] == 0 {
				visit(graph, i)
			}
		}
	}
	depth[u] = 2
	time++
	finish[u] = time
}
func main() {
	sc.Split(bufio.ScanWords)

	n = nextInt()
	graph := make([][]int, n)
	for i := 0; i < n; i++ {
		graph[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		s := nextInt()
		t := nextInt()
		if t == 0 {
			continue
		}
		for i := 0; i < t; i++ {
			u := nextInt()
			graph[s-1][u-1] = 1
		}
	}
	for i := 0; i < n; i++ {
		if depth[i] == 0 {
			visit(graph, i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, d[i], finish[i])
	}
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cstring>


using namespace std;

const int N_MAX = 1000000;
/*
bool update_num_to_visit(int st, int en, int* arr){
  if(st==-1) return false;
  arr[st]--;
  return arr[st]==0;
  }*/

int main(){
  int n;
  cin >> n;
  stack<pair<int, int> > edges;
  stack<pair<int, int> > paths;
  
  int** adjl = new int*[n];
  int* d = new int[n];
  int* f = new int[n];
  
  for(int i=0; i<n; i++){
    adjl[i] = new int[n];
    memset(adjl[i], -1, n * sizeof(adjl[i][0]));
  }
  
  memset(d, -1, n*sizeof(d[0]));
  memset(f, -1, n*sizeof(f[0]));

  for(int i=0; i<n; i++){
    int row_num, k;
    cin >> row_num >> k; row_num--;
    
    for(int j=0; j<k; j++){
      int node_num;
      cin >> node_num; node_num--;
      adjl[row_num][j] = node_num; // [[3, 5, -1 , ...], [1, 3, -1, ...], [1, 2], ...]
    }
  }

  for(int row_num=n-1; row_num>=0; row_num--){
      pair<int, int> p;
      p.first = -1; p.second = row_num; // visited from nowhere
      edges.push(p);
    }

  int time = 1;
  while(!edges.empty()){
    //    cout << "time: " << time << endl;
    
    pair<int, int> edge;
    edge = edges.top(); edges.pop();
    int st, en; st = edge.first; en = edge.second;
    //    cout << "visiting: " << en << " from: " << st << endl;
    
    pair<int, int> path; 
    // record path to go back
    if(st > -1){ // exclude (-1, node)
      path.first = en; path.second = st;
      // avoid inf loop: if the node is not finished, have to go back at some point
      if(f[path.second] == -1){
	paths.push(path);
	// cout << "<" << path.first << ", " << path.second << "> pushed as a path." << endl;
      }
    }    
    
    // if first time visit, mark as visited
    if(d[en] == -1) d[en] = time;
    
    // if not first time, check if it was finished.
    // if it was already finished, continue
    if(f[en] != -1){
      continue;
    }
    
    // counter for opened nodes
    int unvisited_node_num = 0;

    // open node (reverse order)
    for(int i=n-1; i>=0; i--){
      int next_node = adjl[en][i];
      // cout << "next_node: " << next_node << endl;
      // undefined dest or visited, continue
      if(next_node == -1) continue;
      if(d[next_node] != -1){
	// if num_to_visit[en] > 0;
	continue;
      }
      // if unvisited, push into stack
      unvisited_node_num++;
      pair<int, int> next_edge; next_edge.first = en; next_edge.second = next_node;
      edges.push(next_edge);
      // cout << "<" << next_edge.first << ", " << next_edge.second << "> pushed." << endl;
    }

    // if there are no unvisited linked nodes anymore, mark the node finished
    if(unvisited_node_num == 0){
      // cout << "going back..." << endl;
      f[en] = time + 1;
      // cout << "f[" << en << "]: " << f[en] << endl;
      
      // way to go back
      if(paths.empty()) break;
      path = paths.top(); paths.pop();
      edges.push(path);
      // cout << "path pushed." << endl;
    }
    time++;
  }

  // print 'i+1, d[i], f[i]'
  
  for(int i=0; i<n; i++){
    cout << i+1 << " " << d[i] << " " << f[i] << endl;
  }
  
  for(int i=0; i<n; i++){
    delete[] adjl[i];
  }
  delete[] adjl;
  delete[] d;
  delete[] f;
  
  return 0;
}

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

int mat[MAX][MAX] = {};
int n, time = 0, d[MAX] = {}, f[MAX] = {};
bool c[MAX];

void dfs_visit(int u){
    c[u] = true;
    d[u] = ++time;
    for(int i = 0; i < n; i++){
        if(mat[u][i] == 0)  continue;
        if(!c[i])   dfs_visit(i);
    }
    f[u] = ++time;
}

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

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

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        int x, k, y;
        cin >> x >> k;
        while(k-- > 0){
            cin >> y;
            mat[x-1][y-1] = 1;
        }
    }

    dfs();
    return 0;
}

C++: #include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int n;
int a[100][100] = {0};
int k[100] = {0};
int t = 1;
int d[100] = {0};
int f[100] = {0};

int kansu( int i ){
	if ( d[i] != 0 ) return 0;
	d[i] = t;
	t++;
	for ( int j = 0; j < k[i]; j++ ) {
		if ( kansu( a[i][j] ) == 1 ) t++;
	}
	f[i] = t;
	return 1;
}

int main(){
	cin >> n;
	int s;
	for ( int i = 0; i < n; i++ ) {
		cin >> s;
		cin >> k[i];
		for ( int j = 0; j < k[i]; j++ ) {
			cin >> s;
			a[i][j] = s-1;
		}
	}
	kansu (0);
	for ( int i = 0; i < n; i++ ) {
		cout << i+1 << " " << d[i] << " " << f[i] << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string.h>
#include <set>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> void printvec(const vector<T>& v) {
  for (auto x : v) { cout << x << " "; }
  cout << endl;
}
template<class T> void printtree(const vector< vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": "; printvec(tree[i]);
  }
}
template<class T, class U> void printmap(const map<T, U>& mp) {
  for (auto x : mp) { cout << x.first << "=>" << x.second << endl; }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

#define rep(i, n) for(ll i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()
#define sz(x) (ll)(x).size()
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;
typedef vector<ll> vl;
typedef vector<P> vp;

const ll INF = 1e9;
const ll MOD = 1000000007;  // 1e9 + 7

vector<vector<ll>> g;
vector<ll> d;
vector<ll> f;
ll cnt;

void dfs(ll v) {
  if (d[v] >= 0) { return; }
  d[v] = cnt++;
  for (ll u : g[v]) {
    dfs(u);
  }
  f[v] = cnt++;
}

int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  //cout << setprecision(10) << fixed;

  ll n;
  cin >> n;
  g.resize(n);
  rep(i,n) {
    ll u,k;
    cin >> u >> k;
    --u;
    rep(iter,k) {
      ll v;
      cin >> v;
      --v;
      g[u].push_back(v);
    }
  }
  d.assign(n,-1);
  f.assign(n,-1);
  // printtree(g);
  rep(i,n) {
    dfs(i);
  }
  rep(i,n) {
    cout << i+1 << " " << d[i]+1 << " " << f[i]+1 << endl;
  }
}