Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, n)
	fs := make([]int, n)
	founds := make([]bool, n)
	t := 0
	for i := range founds {
		if !founds[i] {
			t = dfs(vecs, ds, fs, founds, i, t)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(w, i+1, ds[i], fs[i])
	}

	w.Flush()
}

func dfs(vecs [][]int, ds, fs []int, founds []bool, i, t int) (time int) {
	t++
	ds[i] = t
	founds[i] = true
	for _, v := range vecs[i] {
		if !founds[v] {
			t = dfs(vecs, ds, fs, founds, v, t)
		}
	}
	t++
	fs[i] = t

	return t
}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

struct Timestamp{
	int discovered;
	int finished;
};

int main(){
	int n;
	cin >> n;

	vector<int> A[n+1];
	bool reached[n+1]{};
	int v1, v2, deg;
	r(i, n){
		cin >> v1 >> deg;

		r(j, deg){
			cin >> v2;
			A[v1].push_back(v2);
		}
	}

	Timestamp ts[n+1];
	for(int i = 1; i <= n; i++){
		ts[i].discovered = 0;
		ts[i].finished = 0;
	}
	ts[1].discovered = 1;

	stack<int> s;
	s.push(1);
	int time = 1;
	while(!s.empty()){
		v1 = s.top();

		bool hasNext = false;
		for(int v2 : A[v1]){
			if(!reached[v2]){
				reached[v2] = true;
				s.push(v2);
				hasNext = true;
				break;
			}
		}

		if(!ts[v1].discovered){
			time++;
			ts[v1].discovered = time;
		}

		if(!hasNext){
			time++;
			ts[v1].finished = time;
			s.pop();
		}
	}

	for(int i = 1; i <= n; i++){
		printf("%d %d %d\n", i, ts[i].discovered, ts[i].finished);
	}

	return 0;
}

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

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int adj_matrix[N_MAX+1][N_MAX+1];

int d[N_MAX+1];
int f[N_MAX+1];

int t;
int n;

void dfs_visit(int id) {
    d[id] = ++t;
    FOR(i,1,n+1) {
        if (adj_matrix[id][i] && !d[i]) {
            dfs_visit(i);
        }
    }
    f[id] = ++t;
}

void dfs() {
    FOR(i,1,n+1) {
        if (!d[i]) {
            dfs_visit(i);
        }
    }
}

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

    cin >> n;

    int id, k, v;
    FOR(i,0,n) {
        cin >> id;
        cin >> k;
        FOR(j,0,k) {
            cin >> v;
            adj_matrix[id][v] = 1;
        }
    }

    dfs();

    FOR(i,1,n+1) {
        cout << i << " " << d[i] << " " << f[i] << endl;
    }

    return 0;
}

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

static const int N=101;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int M[N][N],n; 
int d[N],f[N],color[N];
int tm=0;
int nx[N];

int next(int u){
  for(int v=nx[u];v<n;v++){
    nx[u]=v+1;
    if(M[u][v]) return v;
  }
  return -1;
}

void dfs(int u){
  stack<int> S;
  for(int i=0;i<n;i++) nx[i]=0;

  S.push(u);
  color[u]=GRAY;
  d[u]=++tm;
  
  while(!S.empty()){
    int r=S.top();
    int v=next(r);
    if(v!=-1){
      if(color[v]==WHITE){
	color[v]=GRAY;
	d[v]=++tm;
	S.push(v);
      }
    }else{
      S.pop();
      color[r]=BLACK;
      f[r]=++tm;
    }
  }
}

void solve(){
  for(int i=0;i<n;i++){
    color[i]=WHITE;
    nx[i]=0;
  }
  tm=0;

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

int main(){
  int u,k,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;
    for(int j=0;j<k;j++){
      cin>>v;
      M[u-1][v-1]=1;
    }
  }

  solve();
  
  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <string>
#include <typeinfo>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
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, color[N], d[N], f[N], tt, nt[N];
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_visit(int r){
    for(int i = 0; i < n; i++)nt[i] = 0;
    
    stack<int> S;
    S.push(r);
    color[r] = GRAY;
    d[r] = ++tt;
    while(!S.empty()){
        int u = S.top();
        int v = next(u);
        if(v != -1){
            if(color[v] == WHITE){
                color[v] = GRAY;
                d[v] = ++tt;
                S.push(v);
            }
        }else{
            S.pop();
            color[u] = BLACK;
            f[u] = ++tt;
        }
    }
}

void dfs(){
    for(int i = 0; i < n; i++){
        color[i] = WHITE; 
        nt[i] = 0;
    }
    tt = 0;
    for(int 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 u,k,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++: #include <stdio.h>

int time, n;
int d[100], f[100], color[100];
int M[100][100];

void dfs(int u) {
    color[u] = 1;
    d[u] = ++time;
    for (int i=0; i<n; i++) {
        if(M[u][i] && color[i] == 0) {
            dfs(i);
        }
    }
    color[u] = 2;
    f[u] = ++time;
}

void dfs_init(){
    for (int i=0; i<100; i++) color[i] = 0;
    dfs(0);
}

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

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstdio>
#include <queue>
#include <list>
using namespace std;

int n;

struct Node{
public:
  int id, d, f, flag;
  vector<int> c;
  Node(){d = 1000000007;f = flag = 0;}
};

vector<Node> v(101);

void rec(int id, int &t){
  if(v[id].flag)return;
  else          v[id].flag = 1;
  v[id].d = min(v[id].d, ++t);
  for(int i=0;i<v[id].c.size();i++) {
    rec(v[id].c[i], t);
  }
  v[id].f = max(v[id].f, ++t);
}


bool asc(const Node &right, const Node &left){return right.id < left.id;}

int main(){
  cin >> n;
  
  v[0].id = 0;  
  for(int i=1;i<=n;i++){
    int t, a;  cin >> a >> t;
    vector<int> b(t);
    for(int j=0;j<t;j++){
      int val;cin >> val;
      b[j] = val;
    }
    v[a].id = a;
    v[a].c = b;
  }
  sort(v.begin(), v.end(), asc);

  int ti = 0;
  for(int i=1;i<=n;i++){
    rec(i, ti);
  }

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