Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), 0, 0}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}

	s := Stack{}
	for i := 1; i < len(nodes)+1; i++ {
		if nodes[i].s != 0 {
			continue
		}
		walk(i, &nodes, s)
	}
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v %v\n", (*nodes[i]).id, (*nodes[i]).s, (*nodes[i]).e)
	}
}

var time int

func walk(nid int, nodes *map[int]*node, s Stack) {
	n := (*nodes)[nid]
	if n.s != 0 {
		return
	}
	time++
	n.s = time
	s.Push(n)
	for _, v := range n.nexts {
		walk(v, nodes, s)
	}
	time++
	n.e = time
}

type node struct {
	id    int
	nexts []int
	s     int
	e     int
}

// Stack is Stack
type Stack struct {
	stack []interface{}
}

// Push new value
func (stack *Stack) Push(val interface{}) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() interface{} {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

C++: #include <bits/stdc++.h>
using namespace std;
bool A[105][105],vist[105]={0};
int d[105],f[105],n,t=0;

void dfs(int v){
	d[v] = ++t;
	vist[v] = 1;
	for(int i=1;i<=n;i++)
		if(A[v][i] && !vist[i])		
			dfs(i);
	f[v] = ++t;  
}
int main()
{	
	memset(A,0,sizeof(A));
	int v,u,du;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>v>>du;
		while(du--)
			cin>>u,
			A[v][u] = 1;
	}
	for(int i=1;i<=n;i++)
		if(!vist[i]) dfs(i);
	for(int i=1;i<=n;i++)
		printf("%d %d %d\n",i,d[i],f[i]);
	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
 
#define WHITE 10000
#define BLACK 10001
#define GRAY 10002
#define TRUE 1
#define FALSE 0
#define NUL NULL
 
void dfs(void);
void visit(int);
void push(int);
void pop(void);
int isEmpty(void);
 
int v[101][101],s[101],time1,n;
int *d,*f,*color;
int top = 0;
 
int main(){
  int u,i,j,w,k;
 
  scanf("%d",&n);
 
  d = malloc(sizeof(int) * (n + 1));
  f = malloc(sizeof(int) * (n + 1));
  color = malloc(sizeof(int) * (n + 1));
 
  for(i = 1;i <= n;i++){
    scanf("%d %d",&u,&k);
    for(j = 1;j <= k;j++){
      scanf("%d",&w);
      v[i][w] = 1;
    }
  }
 
  dfs();
 
  for(i = 1;i <= n;i++){
    printf("%d %d %d\n",i,d[i],f[i]);
  }
 
  return 0;
}
 
void dfs(){
  int i = 1,j = 1;
  for(i = 1;i <= n;i++){
      color[i] = WHITE;
  }
 
  time1 = 0;
 
  for(i = 1;i <= n;i++){
    if(color[i] == WHITE){
      visit(i);
    }
  }    
 
}
 
void visit(int r){
  int u,vv=NUL,i;
 
  push(r);
  color[r] = GRAY;
  d[r] = ++time1;
 
  while(isEmpty() == FALSE){
    vv=NUL;
    u = s[top];
    for(i = 1;i <= n;i++){
    if(v[u][i] == 1 && color[i] == WHITE){
      vv = i;
      break;
    }
     
    }
    if(vv != NUL){
      if(color[vv] == WHITE){
    push(vv);
    color[vv] = GRAY;
    d[vv] = ++time1;
      }
    }
    else{
      pop();
      color[u] = BLACK;
      f[u] = ++time1;
    }
  }
}
 
void push(int x){
  top++;
  s[top] = x;
}
 
int isEmpty(){
  if(top == 0){
    return TRUE;
  }
  else{
    return FALSE;
  }
}
 
void pop(){
  top--;
}

C++: #include <stdio.h>
#define N 100
#define W 0
#define X 1
#define Z 2

int n,m[N][N];
int c[N],d[N],f[N],tt;

void dfs_visit(int u){
  int i;
  c[u] = X;
  ++tt;
  d[u] = tt;
  for(i=0;i<n;i++){
    if(m[u][i] == 0) continue;
    else if(c[i]==W){
      dfs_visit(i);
    }
  }
  c[u]=Z;
  ++tt;
  f[u]=tt;
}

void dfs(){
  int i;
  for(i=0;i<n;i++){
    c[i] = W;
  }
  tt = 0;

  for(i=0;i<n;i++){
    if(c[i]==W)dfs_visit(i);
  }
  for(i = 0 ;i<n;i++){
    printf("%d %d %d\n", i+ 1,d[i],f[i]);
  }
}

int main(){
  int u,v,k,i,j;

  scanf("%d",&n);
  for(i = 0;i<n;i++){
    for(j=0;j<n;j++)m[i][j]=0;
  }

  for(i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      m[u][v]=1;
    }
  }
  dfs();

  return 0;
}

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  time[102][2];
int ct = 1;
inline void search(int t) {
	if(!time[t][0]) time[t][0] = ct++;
	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();
		time[t][1] = ct++;
		if(!st.empty()) search(st.top());
	}
}
int main() {
	for (int i = 1; i <= n; i++) {
		time[i][0] = 0;
	}
	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 = 1; i <= n; i++) {
		cout << i << " " << time[i][0] << " " << time[i][1] << endl;
	}
	return 0;
}

C++: n=int(input())

l=[[0 for i in range(n)] for j in range(n)]##静的確保 隣接行列
d=[0 for i in range(n)]##発見
f=[0 for i in range(n)]##完了
stack=[]##スタック
check=[]

for i in range(n):
    p=[int(x) for x in input().split()]
    
    for j in range(n):
        if j+1 in p[2:]:
            l[i][j]=1
##print("---隣接行列---")
##print(l)

time=1
d[0]=1
stack+=[1]
check+=[-1]

while len(stack)!=0:
    ##print("----------while---------")
    for i in range(n):
        if l[stack[-1]-1][i]==1 and not i+1 in check:
            time+=1
            d[i]=time
            stack+=[i+1]

           ## print("stack:",stack)
           ## print("d[",i,"]:",d[i])
            break
        elif i==n-1:
##最深    
            check+=[stack[-1]]
            time+=1
            f[stack.pop()-1]=time

            ##print("f[",i,"]:",f[i])
            ##print("check:",check)
            


for i in range(n):
    print(i+1," ",d[i]," ",f[i])

C++: #include<stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1 
#define BLACK 2
 
int n, M[N][N];
int color[N], d[N], f[N], tt; 

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

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