Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	ans := make([][2]int, N)
	time := 0
	found := make(map[int]bool)
	var dfs func(cur int)
	dfs = func(cur int) {
		time++
		ans[cur][0] = time
		found[cur] = true
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if !found[next] {
				dfs(G[cur][i])
			}
		}
		time++
		ans[cur][1] = time
	}
	for i := 0; i < N; i++ {
		if !found[i] {
			dfs(i)
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprint(wtr, i+1)
		for j := 0; j < 2; j++ {
			fmt.Fprint(wtr, " ")
			fmt.Fprint(wtr, ans[i][j])
		}
		fmt.Fprintln(wtr)
	}
	wtr.Flush()
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int** gmtrx;
int* d, *f;
int t, n;

class Stack{
public:
	Stack(int num):head(0){
		s = new int [num];
		for(int i=0; i<num; ++i){
			s[i] = 0;
		}
	}
	~Stack(){ delete[] s; }
	void push_back(int vtx){
		s[head++] = vtx;
	}
	int pop_back(){
		--head;
		int tmp = s[head];
		s[head] = 0;
		return tmp;
	}
	int back(){
		if(head<=0) return -1;
		return s[head-1];
	}
private:
	int head;
	int* s;
};


int main()
{
	scanf("%d",&n);

	gmtrx = new int*[n];
	d = new int [n]; //discovery
	f = new int [n]; //finish
	Stack st(n);

	for(int i=0; i<n; ++i){
		gmtrx[i] = new int[n];
		for(int j=0; j<n; ++j){
			gmtrx[i][j] = 0;
		}
		d[i] = 0;
		f[i] = 0;
	}

	for(int i=0; i<n; ++i){
		int v, num, a;
		scanf("%d %d",&v,&num);
		--v;
		for(int j=0; j<num; ++j){
			scanf("%d",&a);
			--a;
			gmtrx[v][a] = 1;
		}
	}

	for(int i=0; i<n; ++i){
		int vtmp=i;
		if( d[vtmp]!= 0) continue;
		st.push_back(vtmp);
		vtmp=0;
		while(st.back()!=-1){
			if(d[st.back()]==0){
				++t;
				d[st.back()] = t;
			}
			else if(f[st.back()]!=0){
				vtmp = st.pop_back();
				++vtmp;
				continue;
			}
			for(int j=vtmp; j<=n; ++j){
				if(j==n){
					++t;
					f[st.back()] = t;
					vtmp = st.pop_back();
					++vtmp;
					break;
				}
				if(gmtrx[st.back()][j]){
					if(d[j]==0) st.push_back(j);
					else continue;
					break;
				}
			}
		}
	}

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

	for(int i=0; i<n; ++i){
		delete[] gmtrx[i];
	}
	delete[] gmtrx;
	delete[] d;
	delete[] f;

	return 0;
}

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

int main()
{
    int n;
    cin >> n;
    int Adj[n][n] = {};
    int time[n][2] = {};
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            Adj[u - 1][v - 1] = 1;
        }
    }

    stack<int> s;
    int current_time = 1;
    for (int i = 0; i < n; i++)
    {
        if (time[i][0] == 0)
        {
            int src = i;
            s.push(src);
            while (!s.empty())
            {
                int cur = s.top();
                s.pop();
                //cout << cur << " " << current_time << endl;
                if (time[cur][0] == 0)
                {
                    time[cur][0] = current_time;
                    s.push(cur);
                    for (int dst = n - 1; dst >= 0; dst--)
                    {
                        //cout << cur << " " << dst << " " << Adj[cur][dst] << endl;
                        if (Adj[cur][dst] && time[dst][0] == 0)
                        {
                            s.push(dst);
                        }
                    }
                    current_time++;
                }
                else if (time[cur][0] != 0 && time[cur][1] == 0)
                {
                    time[cur][1] = current_time;
                    current_time++;
                }
            }
        }
    }

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

    return 0;
}

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

using namespace std;

int main(){
    stack<int> st;
    int n;
    cin >> n;
    int d[n] = {};
    int f[n] = {};
    int vnum[n][2] = {};
    bool vflag[n] = {};
    int tmp2;
    int num;
    vector<vector<int>> vertex;
    // vector<vector<bool>> explored;
    vector<int> tmp;
    // vector<bool> flag;
    for(int i=0;i<n;i++){
        cin >> num;
        cin >> num;
        vnum[i][0] = num;
        for(int j=0;j<num;j++){
            cin >> tmp2;
            tmp.push_back(tmp2);
            // flag.push_back(false);
        }
        // if(num==0){
        //     flag.push_back(false);
        // }
        vertex.push_back(tmp);
        // explored.push_back(flag);
        tmp.clear();
        // flag.clear();
    }
    int cnt = 1;
    bool check;
    int s;
    st.push(0);
    do{
        s = st.top();

        if((vnum[s][0]==0) && d[s]==0){
            vflag[s] = true;
            d[s] = cnt++;
            f[s] = cnt++;
            st.pop();
        }else if(vnum[s][0]==vnum[s][1]){
            vflag[s] = true;
            f[s] = cnt++;
            st.pop();
        }else if(vflag[vertex[s][vnum[s][1]]-1] && f[vertex[s][vnum[s][1]]-1]!=0 && d[s]==0){
            vflag[s] = true;
            d[s] = cnt++;
            f[s] = cnt++;
            st.pop();
        }else if(vflag[vertex[s][vnum[s][1]]-1]){
            vflag[s] = true;
            f[s] = cnt++;
            st.pop();
        }else if(vflag[s]){
            st.push(vertex[s][vnum[s][1]++] - 1);
        }else{
            if(d[s]==0){
                d[s] = cnt++;
            }
            st.push(vertex[s][vnum[s][1]++] - 1);
        }
        check = !st.empty();
        if(!check){
            for(int i=0;i<n;i++){
                if(vflag[i]==false){
                    check = true;
                    st.push(i);
                    break;
                }
            }
        }
    }while(check);

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