Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}
	}

	d := make([]int, n+1)
	f := make([]int, n+1)
	t := 1
	for i := 1; i <= n; i++ {
		t = dfs(&g, &d, &f, i, t)
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, d[i], f[i])
	}
}

func dfs(g *[][]int, d *[]int, f *[]int, id int, t int) int {
	if (*d)[id] > 0 {
		return t
	}
	(*d)[id] = t
	t += 1
	for i := 0; i < len((*g)[id]); i++ {
		t = dfs(g, d, f, (*g)[id][i], t)
	}
	(*f)[id] = t
	return t+1
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
stack<int> st;
int  v[102][101], n;
int  tim[102][2];
int ct = 1;
int go[102];
inline void search(int t) {
	if(!tim[t][0]) tim[t][0] = ct++;
	go[t] = 1;
	int tf = 0;
	for (int a = 1; a <= n; a++) {
		if (v[t][a]) {
			st.push(a);
			for (int i = 1; i <= n; i++) {
				v[i][a] = 0;
			}
			search(st.top());
			tf = 1;
		}
	}
		
	if(!tf) {
		st.pop();
		tim[t][1] = ct++;
		if(!st.empty()) search(st.top());
	}
}
int main() {

	st.push(1);
	int u, k, s;
	cin >> n;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			v[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> s;
			v[u][s] = 1;
		}
	}
	search(1);
	for (int i = 2; i <= n; i++) {
		if (!go[i]) {
			st.push(i);
			search(i);
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << tim[i][0] << " " << tim[i][1] << endl;
	}
	return 0;
}

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

int G[101][102];
int found[101];
int comp[101];   // complete.
int t;  // time.

typedef struct box{ int val; box* next; } box_t;  // stack.

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    return head;
}

bool isEmpty(box_t* head){ return (head->next == NULL ? true : false); }

box_t* push(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    head->next = box;
    return box;
}

int pop(box_t* head)
{
    if(isEmpty(head)){ return -1; }
    box_t* box = head->next;
    int val;
    head->next = box->next;
    val = box->val;
    delete box;
    return val;
}

int front(box_t* head)
{
    int val;
    if(isEmpty(head)){ return -1; }
    val = head->next->val;
    return val;
}

box_t* stack = init();

void search_loop(int i)
{
    int j, k, nex;
    while(1){
        if(found[i] == 0){
            t++; found[i] = t;
            push(stack, i);
        }
        //for(k = G[i][0]; G[i][k] != 0 && found[G[i][k]] != 0; k++);
		k = G[i][0];
		while(1){
			nex =G[i][k];
			if(nex == 0) break;
			if(found[nex] == 0) break;
			k++;
		};



        if(G[i][k] == 0){
            if(isEmpty(stack)){ return; }
            else{
                j = pop(stack);
                t++; comp[j] = t;
                i = front(stack);
            }
        }else{
            G[i][0] = k + 1;
            i = G[i][k];
        }
    };
    return;
}

int main()
{
    int n, i, j, a, b, e;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        G[i][0] = 1;
        for(j = 1; j <= n + 1; j++){ G[i][j] = 0; } }

    for(i = 1; i <= n; i++){
        scanf("%d %d", &a, &e);
        for(j = 1; j <= e; j++){
            scanf("%d", &b); G[a][j] = b; } }  // adjacent representation.

    for(i = 1; i <= n; i++){ found[i] = 0; comp[i] = 0; }
    t = 0;
    while(1){
        for(i = 1; i <= n; i++){
            if(found[i] == 0) break;
        }
        if(i > n) break;
        search_loop(i);
    };

    for(i = 1; i <= n; i++){
        printf("%d %d %d\n", i, found[i], comp[i]);
    }
    return 0;
}

C++: #include <iostream>
#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 n;
//?????\??????
int M[N][N];
//??°?????¶???????¢????
int color[N];
//??\??¶????????°??¶???
int d[N];
//??\??¶???????????¶???
int f[N];
//??¶???
int tt;
//???????????????????????????????????????
//?????¨?????????????????¨????????\??????????????????
int nt[N];

//????????????????????????u?????????v
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&&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++)
        nt[u]=v+1,color[i]=WHITE;
    tt=0;

    //??\????????????u?????????????????±?????????????´¢
    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();
}

C++: #include<iostream>
#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 n, tt;
int color[N], nt[N], M[N][N], d[N], f[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 i = 0; i < n; i++) {
   	   if(color[i] == WHITE) {
   	   	   dfs_visit(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;
		u--;
		for(int j = 0; j < k; j--) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	
    dfs();
    
    return 0;
	
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int G[101][101];
bool used[101];
int f[101],s[101];
int cnt=0;
void dfs(int now){
	cnt++;
	if(s[now]==0)s[now]=cnt;
	used[now]=1;
	for(int i=1;i<=100;i++){
	if(G[now][i]&&used[i]==0)dfs(i);
	}
	cnt++;
	f[now]=cnt;
}

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