Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 100

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph struct {
	M               [N][N]int
	color, d, f, nt [N]int
	tt              int
}

func (g *Graph) next(u, n int) int {
	for v := g.nt[u]; v < n; v++ {
		g.nt[u] = v + 1
		if g.M[u][v] != 0 {
			return v
		}
	}
	return -1
}

func (g *Graph) dfsVisit(r, n int) {
	var S []int
	S = append([]int{r}, S...)
	g.color[r] = GRAY
	g.tt++
	g.d[r] = g.tt

	for len(S) != 0 {
		u := S[0]
		v := g.next(u, n)
		if v == -1 {
			S = S[1:]
			g.color[u] = BLACK
			g.tt++
			g.f[u] = g.tt
			continue
		}

		if g.color[v] != WHITE {
			continue
		}

		g.color[v] = GRAY
		g.tt++
		g.d[v] = g.tt
		S = append([]int{v}, S...)
	}
}

func (g *Graph) dfs(n int) string {
	for i := 0; i < n; i++ {
		g.color[i] = WHITE
	}
	g.tt = 0

	for u := 0; u < n; u++ {
		if g.color[u] == WHITE {
			g.dfsVisit(u, n)
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d %d %d\n", i+1, g.d[i], g.f[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.dfs(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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;
  vector<int> c;
  Node(){d = 1000000007;f = 0;}
};

vector<Node> v(101);

void rec(int id, int &t){
  if(v[id].f)return;
  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;
  rec(1, ti);

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

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

using namespace std;
typedef long long ll;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const ll INF = 1LL<<60;

vector<vector<ll> >V;
ll c=0;
vector<vector<ll> >ans;
void dfs(ll x){
  if(ans[x].size()!=0)return;
  c++;
  ans[x].push_back(c);

  rep(i,0,V[x].size()){
    dfs(V[x][i]);
  }
  c++;
  ans[x].push_back(c);
}

int main(){

  ll n;cin>>n;
  V.resize(n);
  ans.resize(n);
  rep(i,0,n){
    ll u;cin>>u;
    --u;
    ll k;cin>>k;
    rep(j,0,k){
      ll a;cin>>a;
      --a;
      V[u].push_back(a);

    }
  }
  rep(i,0,n){
    dfs(i);
  }

  rep(i,0,n)out(i+1<<" "<<ans[i][0]<<" "<<ans[i][1]);






  return 0;
}

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

using namespace std;

const int white=0;
const int gray=1;
const int black=2;
const int n=100;

int cnt,m[n][n],color[n],d[n],f[n],tt,nt[n];

int next(int u)
{
    for(int v=nt[u];v<cnt;v++){
        nt[u]=v+1;
        if(m[u][v]){
            return v;
        }
    }
    return -1;
}

void dfs_visit(int r)
{
    for(int i=0;i<cnt;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<cnt;i++){
        color[i]=white;
        nt[i]=0;
    }
    tt=0;
    for(int u=0;u<cnt;u++){
        if(color[u]==white){
            dfs_visit(u);
        }
    }
    for(int i=0;i<cnt;i++){
        cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}

int main()
{
    int u,k,v;
    cin>>cnt;
    for(int i=0;i<cnt;i++){
        for(int j=0;j<cnt;j++){
            m[i][j]=0;
        }
    }
    for(int i=0;i<cnt;i++){
        cin>>u>>k;
        u--;
        for(int j=0;j<k;j++){
            cin>>v;
            v--;
            m[u][v]=1;
        }
    }
//    for(int i=0;i<cnt;i++){
//        for(int j=0;j<cnt;j++){
//            cout<<m[i][j]<<" ";
//        }
//        cout<<endl;
//    }
    dfs();
    return 0;
}
/*
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0
*/

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int MAX=105;
int M[MAX][MAX];
bool vis[MAX];
int n,t=1,d[MAX],f[MAX],nt[MAX];
stack<int> S;
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(int cur){
	memset(nt,0,sizeof(nt));
	vis[cur]=1;
	d[cur]=t++;
	S.push(cur);
	while(!S.empty()){
		int u=S.top();
		int i=next(u);
		if(i!=-1){
			if(M[u][i]&&!vis[i]){
				vis[i]=1;
				d[i]=t++;
				S.push(i);
			}
		}
		else{
			S.pop();
			f[u]=t++;
		}	
	}

}
int main(){
	cin>>n;
	memset(M,0,sizeof(M));
	memset(vis,0,sizeof(vis));
	for(int i=0;i<n;i++){
		int u,k;
		cin>>u>>k;
		int v;
		for(int j=0;j<k;j++){
			cin>>v;
			M[u][v]=1;
		}
	}
	for(int i=1;i<=n;i++){
		if(!vis[i])dfs(i);
	}
	for(int i=1;i<=n;i++){
		printf("%d %d %d\n",i,d[i],f[i]);
	}
	return 0;
}

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

using namespace std;

static const int N = 100;
static const int NEW = 0;
static const int VISITING = 1;
static const int OLD = 2;

int n, m[N][N] = {0};
int color[N], t1[N], t2[N];

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

int nxt(int idx)
{
	for (int i = 0; i < n; ++i)
	{
		if (m[idx][i] && color[i] == NEW)
			return i;
	}
	return -1;
}

void dfs_visit(int idx)
{
	int t = 1;

	stack<int> s;
	s.push(idx);
	color[idx] = VISITING;
	t1[idx] = t++;

	while (!s.empty())
	{
		int u = s.top();
		int v = nxt(u);
		if (-1 != v)
		{
			color[v] = VISITING;
			t1[v] = t++;
			s.push(v);
		}
		else
		{
			s.pop();
			color[u] = OLD;
			t2[u] = t++;
		}
	}
}

void dfs()
{
	for (int i = 0; i < n; ++i)
		color[i] = NEW;

	for (int i = 0; i < n; ++i)
	{
		if (color[i] == NEW)
			dfs_visit(i);
	}

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

int main()
{
	getMatrix();
	dfs();
}