Go: package main

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

/***************************************************************
stackによる深さ優先探索を実装する。
***************************************************************/

const (
	N     = 100
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

var (
	n            int
	Adj          [N][N]int
	statusColor  [N]int
	discoverTime [N]int
	finishTime   [N]int
	clock        int
	nt           [N]int
)

/***************************************************************
Stackの定義:
- field
	container: ノードの格納されたスライス
	sp: スタックポインタ
- method
	size(): スタックに積まれているノードの個数を返す
	top(): スタックの一番上に積まれているノードを返す
	isEmpty(): スタックが空かどうかを返す
	pop(): スタックの要素をポップし削除する
	push(): スタックに要素を積む
***************************************************************/

type Stack struct {
	container []int
	sp        int
}

func (s *Stack) size() int {
	return len(s.container)
}

func (s *Stack) top() int {
	return s.container[s.sp]
}

func (s *Stack) isEmpty() bool {
	return s.sp == 0
}

func (s *Stack) pop() int {
	_pop := func() {
		s.container = s.container[:len(s.container)-1]
	}
	defer _pop()
	if s.isEmpty() {
		panic("There is no elements in stack!")
	}
	s.sp--
	return s.container[s.sp]
}

func (s *Stack) push(n int) {
	s.sp++
	s.container[s.sp] = n
}

func next(u int) int {
	for v := nt[u]; v < n; v++ {
		nt[u] = v + 1
		if Adj[u][v] == 1 {
			return v
		}
	}
	return -1
}

func _DFS(target int) {
	for i := 0; i < n; i++ {
		nt[i] = 0
	}
	stack := new(Stack)
	stack.container = make([]int, N)
	stack.push(target)
	statusColor[target] = GRAY
	clock++
	discoverTime[target] = clock
	for !stack.isEmpty() {
		u := stack.top()
		v := next(u)
		if v != -1 {
			if statusColor[v] == WHITE {
				statusColor[v] = GRAY
				clock++
				discoverTime[v] = clock
				stack.push(v)
			}
		} else {
			stack.pop()
			statusColor[u] = BLACK
			clock++
			finishTime[u] = clock
		}
	}
}

func DFS() {
	for i := 0; i < n; i++ {
		statusColor[i] = WHITE
		nt[i] = 0
	}
	clock = 0
	for i := 0; i < n; i++ {
		if statusColor[i] == WHITE {
			_DFS(i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, discoverTime[i], finishTime[i])
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		u--
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ := strconv.Atoi(scanner.Text())
			v--
			Adj[u][v] = 1
		}
	}

	DFS()
}

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;
}