Go: package main

/*
深さ優先探索
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		ints := eGetInts(scanner)
		num, vcount := ints[0], ints[1]
		g[i] = make([]int, vcount)
		for j := 0; j < vcount; j++ {
			g[num][j] = ints[j+2]
		}
	}
	hist := make([]struct{ d, f int }, n+1)
	t := 0
	var visit func(int)
	visit = func(num int) {
		if hist[num].d == 0 {
			stack := []int{}
			t++
			hist[num].d = t
			stack = append(stack, num)
			for len(stack) > 0 {
				// トップを訪問する
				top := stack[len(stack)-1]
				for i := 0; i < len(g[top]); i++ {
					if hist[g[top][i]].d == 0 {
						t++
						hist[g[top][i]].d = t
						stack = append(stack, g[top][i])
						goto endloop
					}
				}
				// 訪問場所がなくなったら
				// pop
				stack = stack[0 : len(stack)-1]
				t++
				hist[top].f = t
			endloop:
			}
		}
	}
	for i := 1; i < n+1; i++ {
		visit(i)
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, hist[i].d, hist[i].f)
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
const ll INF = 1LL<<60;
const int INT_INF = 1e9;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::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; }
using namespace std;

enum State {
    unseen, // 未訪問
    seen,   // 発見済み
    end     // 探索終了
};

vector<vector<int>> adj; // 隣接行列
int t = 0; // 時刻
int d[110];   // 頂点vを最初に発見した発見時刻　
int f[110];   // 頂点vの隣接リストを調べ終えた完了時刻
State s[110];

void dfs(int u) {
    ++t;
    d[u] = t;
    s[u] = State::seen;
    // uの隣接点を探索
    for (auto &v: adj[u]) {
        if (s[v] == State::unseen) {
            dfs(v);
        }
    }
    // uは探索済み
    s[u] = State::end;
    ++t;
    f[u] = t;
}

int main() {
    int n; cin >> n;
    adj.resize(n);
    rep(i, n) {
        int u, k; cin >> u >> k;
        --u;
        rep(j, k) {
            int v; cin >> v;
            --v;
            adj[i].emplace_back(v);
        }
    }

    // 初期化
    rep(i, n) {
        d[i] = INT_INF;
        f[i] = 0;
        s[i] = State::unseen;
    }

    // DFS
    rep(i, n) {
        if (s[i] == State::unseen) {
            dfs(i);
        }
    }

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

C++: #include <iostream>

using namespace std;

typedef pair<int, int> p;

int n,u,k,t;
int** v;
p* T;

void bfs(int pos) {
  // if visited
  if (T[pos].first) {
    return;
  }
  // check visit
  T[pos].first = ++t;

  int leng = v[pos][0];
  for (int i = 1; i <= leng; i++) {
    bfs(v[pos][i]);
  }
  T[pos].second = ++t;
}

int main() {
  cin >> n;

  v = new int*[n+1];
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    v[u] = new int[k+1];
    v[u][0] = k;
    for (int j = 1; j <= k; j++) {
       cin >> v[u][j];
    }
  }

  T = new p[n+1];
  for (int i = 1; i <= n; i++) {
    T[i] = p(0, 0);
  }

  t = 0;
  bfs(1);

  for (int i = 1; i <= n; i++) {
    cout << i << " " << T[i].first << " " << T[i].second << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 100;
int d[MAXN], f[MAXN], tt;
int n,color[MAXN], mat[MAXN][MAXN];
//0表示未访问，1表示正在访问，2表示访问完毕
void dfs_visit(int node)
{
	int i;
	color[node] = 1;
	d[node] = ++tt;  //记录最初的访问时间
	for (i = 0; i < n; i++)
	{
		if (mat[node][i] == 0)	continue;
		if (color[i] == 0)	dfs_visit(i);
	} 
	color[i] = 2;  
	f[node] = ++tt;  //访问结束
}

void dfs()
{
	int i;
	for (i = 0; i < n; i++)	color[i] = 0;  //初始化为未访问状态
	tt = 0; //从0开始计时
	for (i = 0; i < n; i++)
	{
		if (color[i] == 0)
			dfs_visit(i);
	}
	for (i = 0; i < n; i++)	printf("%d %d %d\n", i + 1, d[i], f[i]);
}



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

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

using namespace std;

int solve(vector<vector<int> >& V, vector<int>& d, vector<int>& f, int cur, int timestamp) {
  bool change = false;
  for (int i = 0; i < V[cur].size(); ++i) {
    int next = V[cur][i] - 1;
    if (d[next] == 0) {
      ++timestamp;
      d[next] = timestamp;
      timestamp = solve(V, d, f, next, timestamp);
    }
  }
  ++timestamp;
  if (f[cur] == 0)
      f[cur] = timestamp;
  return timestamp;
}

int main(int argc, char* argv[]) {
  int n;
  cin >> n;

  vector<vector<int> > V(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;
    vector<int> tmp(k);
    for (int j = 0; j < k; ++j)
      cin >> tmp[j];
    V[u - 1] = tmp;
  }

  vector<int> d(n);
  vector<int> f(n);
  int timestamp = 0;
  for (int i = 0; i < n; ++i) {
    if (d[i] == 0) {
      ++timestamp;
      d[i] = timestamp;
      timestamp = solve(V, d, f, i, timestamp);
    }
  }

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

  return 0;
}

C++: #include <cstdio>

typedef struct timestamp {
        int d, f;
} timestamp;

const int N = 100;
bool graph[N][N] {{false}};
timestamp ts[N];
int time = 0;

void search(int n, int cur) {
        if (ts[cur].d) {
                return;
        }
        ts[cur].d = ++time;
        for (int i = 0; i < n; i++) {
                if (graph[cur][i]) {
                        search(n, i);
                }
        }
        ts[cur].f = ++time;
}

int main() {
        int n, u, k, v;
        for (int i = 0; i < N; i++) {
                ts[i].d = ts[i].f = 0;
        }

        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
                scanf("%d %d", &u, &k);
                for (int j = 0; j < k; j++) {
                        scanf("%d", &v);
                        graph[u-1][v-1] = true;
                }
        }
        for (int cur = 0; cur < n; cur++) {
                search(n, cur);
        }
        for (int i = 0; i < n; i++) {
                printf("%d %d %d\n", i+1, ts[i].d, ts[i].f);
        }

        return 0;
}