Go: package main

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

// repr state of visited node
// WHITE yet visit
// GEAY  visiting
// BLACK visited
const (
	WHITE int = iota
	GRAY
	BLACK
)

type dfsGraph struct {
	g      [][]int
	colors []int
	stime  []int // Start time of visiting node
	ftime  []int // Finish time of visiting node
}

var time int

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func (dg dfsGraph) dfsVisit(u int) {
	// 頂点uを訪問中に設定
	dg.colors[u] = GRAY
	time++
	dg.stime[u] = time

	// uの隣接nodeを訪問
	for v := 0; v < len(dg.g); v++ {
		if dg.g[u][v] == 0 {
			continue // 辺がなければスキップ
		}

		// vが未訪問なら訪問
		if dg.colors[v] == WHITE {
			dg.dfsVisit(v)
		}
	}

	// 頂点uを訪問済みに設定
	dg.colors[u] = BLACK
	time++
	dg.ftime[u] = time
}

func (dg dfsGraph) dfs() {
	for i := 0; i < len(dg.g); i++ {
		if dg.colors[i] == WHITE {
			dg.dfsVisit(i)
		}
	}
}

func (dg dfsGraph) printDfsTime() {
	for i := 0; i < len(dg.g); i++ {
		fmt.Printf("%d %d %d\n", i+1, dg.stime[i], dg.ftime[i])
	}
}

func main() {
	adj := makeAdjList()
	colors := make([]int, len(adj))
	stime := make([]int, len(adj))
	ftime := make([]int, len(adj))
	graph := dfsGraph{adj, colors, stime, ftime}
	graph.dfs()
	graph.printDfsTime()
}

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

void InputSet(std::vector<int> & a) {
	int n;
	cin >> n;
	a.resize(n,0);
	for(int i=0;i<n;++i) {
		cin >> a.at(i);
		a.at(i)--;
	}
}
void DFS(vector<vector<int> > & Ad,
		 vector<int> & D,
		 vector<int> & F,
		 int & time,
		 int idx)
{
	if (F[idx] < 0) {
		for(int i=0;i<(int)Ad[idx].size();++i) {
			int next = Ad[idx][i];
			if (D[next] < 0) {
				D[next] = time++;
				DFS(Ad,D,F,time,next);
			}
		}
		F[idx]= time++;
	}
}
int main(){
	int n;
	cin >> n;
	vector<vector<int> > Ad(n);
	vector<int> D(n,-1),F(n,-1);
	for(int k=0;k<n;++k) {
		int u;
		cin >> u;
		--u;
		InputSet(Ad[u]);
	}
	int time=1;
	D[0] = time++;
	DFS(Ad,D,F,time,0);
	for(int i=0;i<n;++i){
		cout << i + 1 << " " << D[i] << " " << F[i] << endl;
	}
}

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

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int g[101][101],n,d[101],f[101];

int main() {
    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]++;
        }
    }
    stack<int> dfs;
    int time = 2;
    dfs.push(1);
    d[1] = 1;
    bool flag = true;
    while(flag) {
        while (!dfs.empty()) {
            int u = dfs.top();
            bool fl = false;
            for (int i = 1; i <= n; i++) {
                if (g[u][i] && d[i] == 0 && u != i) {
                    dfs.push(i);
                    d[i] = time++;
                    fl = true;
                    break;
                }
            }
            if (!fl) {
                dfs.pop();
                f[u] = time++;
            }
        }
        flag = false;
        for(int i =1;i <= n;i++) if(d[i] == 0){flag = true;dfs.push(i);d[i] = time++;break;}
    }
    for(int i = 1;i <=n;i++) {
        cout << i << " " << d[i] << " " << f[i] <<endl;
    }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;

int t;

void dfs(int pos, vector<int>& d, vector<int>& f, vector<vector<int>>& a){
	rep(i,a.size()){
		if(a[pos][i] == 1 and d[i] == -1){
			t++;
			d[i] = t;
			dfs(i, d, f, a);
		}
	}
	t++;
	f[pos] = t;
}

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

	vector<vector<int>> a(n, vector<int>(n,0));
	rep(i,n){
		int from;
		int m;
		cin >> from >> m;
		from--;
		rep(j,m){
			int to;
			cin >> to;
			to--;
			a[from][to] = 1;
		}
	}

	vector<int> d(n,-1), f(n);
	t = 1;
	d[0] = t;
	dfs(0, d, f, a);

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

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

ll n, t = 0;
vector<vector<ll>> g;
vector<ll> d, f;

void dfs(ll v, ll p = -1) {
    if (d[v] != -1) return;
    t++;
    d[v] = t;
    rep(i, sz(g[v])) {
        if (g[v][i] == p) continue;
        dfs(g[v][i], v);
    }
    f[v] = t;
    t++;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    cin >> n;
    g.resize(n);
    rep(i, n) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    rep(i, n) sort(all(g[i]));
    d.resize(n, -1);
    f.resize(n, -1);
    rep(i, n) {
        if (d[i] == -1) dfs(i);
    }
    rep(i, n) {
        printf("%lld %lld %lld\n", i + 1, d[i], f[i] + 1);
    }
    return 0;
}

C++: #if 0
????????£?????????????´??????????
?????°?????¨??????dfs??????????????????????°??????§?????????
#endif

#include <iostream>
#include <vector>
using namespace std;
vector<int> E[101];
bool visited[101];
int in[101];
int out[101];
int count=0;
int nv(int n)
{
    int i=1;
    for(i=1;i<=n;i++)
    {
        if ( visited[i]==false )
            return i;
    }
    if (i>n) return 0;
}

void dfs(int n)
{
    visited[n]=true;
    in[n]=++count;
    for(vector<int>::iterator it=E[n].begin();it!=E[n].end();++it)
    {
        int now=*it;
        if(visited[now]==false)
            dfs(now);
    }
    out[n]=++count;
}

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int e,n0;
        cin>>e>>n0;
        for(int j=0;j<n0;j++)
        {
            int t;
            cin>>t;
            E[e].push_back(t);
        }
        visited[i]=false;
        in[i]=out[i]=0;
    }

    int now=nv(n);
    while(now!=0)
    {
        dfs(now);
        now=nv(n);
    }


/*  for(int i=1;i<=n;i++)
    {
        for(vector<int>::iterator it=E[i].begin();it!=E[i].end();++it)
            cout<<*it<<" ";
        cout<<endl;
    }
*/
    for(int i=1;i<=n;i++)
        cout<<i<<" "<<in[i]<<" "<<out[i]<<endl;
    return 0;



}

C++: #include <iostream>

using namespace std;

	int TIME = 0;
	int d[101];
	int f[101];
	int num;
	int graph[101][101] = {0};

void dfs(int x){
	TIME++;
	d[x] = TIME;

	for(int i=1;i<=num;i++){
		if(graph[x][i] == 1 && d[i]==0){
			dfs(i);
		}
	}
	f[x] = ++TIME;
}

int main(){
	int hon;
	int eda;
	int ver;

	cin>>num;
	for(int i=0;i<num;i++){
		cin>>eda;
		cin>>hon;
		for(int j=0;j<hon;j++){
			cin>>ver;
			graph[eda][ver]++;
		}
	}

	dfs(1);

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

	return 0;
}