Go: package main

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

const N = 100

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph struct {
	M               [N][N]int
	color, d, f, nt [N]int
	tt              int
}

func (g *Graph) next(u, n int) int {
	for v := g.nt[u]; v < n; v++ {
		g.nt[u] = v + 1
		if g.M[u][v] != 0 {
			return v
		}
	}
	return -1
}

func (g *Graph) dfsVisit(r, n int) {
	var S []int
	S = append([]int{r}, S...)
	g.color[r] = GRAY
	g.tt++
	g.d[r] = g.tt

	for len(S) != 0 {
		u := S[0]
		v := g.next(u, n)
		if v == -1 {
			S = S[1:]
			g.color[u] = BLACK
			g.tt++
			g.f[u] = g.tt
			continue
		}

		if g.color[v] != WHITE {
			continue
		}

		g.color[v] = GRAY
		g.tt++
		g.d[v] = g.tt
		S = append([]int{v}, S...)
	}
}

func (g *Graph) dfs(n int) string {
	for i := 0; i < n; i++ {
		g.color[i] = WHITE
	}
	g.tt = 0

	for u := 0; u < n; u++ {
		if g.color[u] == WHITE {
			g.dfsVisit(u, n)
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d %d %d\n", i+1, g.d[i], g.f[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.dfs(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;
using VL=vector<ll>;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
const long long INF =  999999999;
const int WHITE=0;
const int GREY=1;
const int BLACK=2;

int color[110],tt,n,d[110],f[110];
vector<int> g[110];

void dfs_visit(int v){
  color[v]=GREY;
  d[v]=++tt;
  REP(i,g[v].size()){
    if(color[g[v][i]]==WHITE){
      dfs_visit(g[v][i]);
    }
  }
  color[v]=BLACK;
  f[v]=++tt;
}

void dfs(){
  REP(i,n){
    color[i]=WHITE;
  }
  REP(i,n){
    if(color[i]==WHITE)dfs_visit(i);
  }

  REP(i,n){
    printf("%d %d %d\n",i+1,d[i],f[i]);
  }
}

int main(){
  cin  >>n;
  int u,k;
  REP(i,n){
    cin >>u>>k;
    u--;
    REP(i,k){
      int p;
      cin >>p;
      p--;
      g[u].push_back(p);
    }
  }
  tt=0;

  dfs();
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
// stackのパターン
// 実習資料では「srcの訪問時刻は0」となっているが、題意は1である。

#include <iostream>
#include <cstdio>
#include <stack> // 入れたものから出していく引っ越しトラック型の構造。
using namespace std;

int G[110][110];
int n;
int u, k, v;

void make_matrix() {
  fill(&G[0][0], &G[0][0]+110*110, 0);
  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", &v);
      v--;
      G[u][v] = 1;
    }
  }
}

int timer; // timeだとエラーが出たので。
int d[110]; // 訪問時刻。ナマの数字を入れる(indexを-1処理しない)。
int f[110]; // 終了時刻。ナマの数字を入れる。
int parent[110]; // 訪問する原因となった数字を入れる。ナマの数字を入れる。
int children[110]; // Stackに追加した頂点の数。

void init() {
  timer = 1;
  fill(d, d+110, -1);
  fill(f, f+110, -1);
  fill(parent, parent+110, -1);
  fill(children, children+110, 0);
}

void dfs(int src) {
  cerr << "dfs root is " << src << endl;
  stack<int> S; // todo訪問先
  S.push(src);
  while (! S.empty()) {
    int cur = S.top(); // 一番最近入れた数字を訪問。
    d[cur] = timer;
    timer++;
    S.pop();
    cerr << "visiting " << cur << " at the time " << d[cur] << endl;
    for (int i=1; i<=n; i++) { // 節点i
      if (G[cur-1][i-1] == 1 && d[i] == -1) {
	S.push(i);
	parent[i] = cur;
	children[cur]++;
      }
    }
    int hantei = cur;
    while (hantei != -1 && children[hantei] == 0) {
      f[hantei] = timer;
      timer++;
      hantei = parent[hantei];
      children[hantei]--;
    }
  }
}

int main() {
  make_matrix();
  init();
  for (int i=1; i<=n; i++) { // 1から連結していない連結成分にも番号をふる。
    if (d[i] == -1) {
      dfs(i);
    }
  }
  for (int i=1; i<=n; i++) {
    printf("%d %d %d\n", i, d[i], f[i]);
  }
}

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

int n, u, k, v;
int d[101], f[101];
pair<int, vector<int>> G[101];

int ti = 1;

void dfs(int id) {
	d[id] = ti ++;

	vector<int>& adj = G[id].second;
	for (int i=0, l=adj.size(); i<l; ++i) {
		if ( ! d[adj[i]]) {
			dfs(adj[i]);
		}
	}
	f[id] = ti ++;
}

int main() {
	cin >> n;
	for (int i=0; i<n; ++i) {
		cin >> u >> k;
		G[u].first = u;
		while (k --) {
			cin >> v;
			G[u].second.push_back(v);
		}
	}
	dfs(1);

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

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <stack>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

#define INF 1000000
vector<int> ds(100, INF);
vector<int> fs(100);
int t = 0;

vector<vector<int>> edges;
bool dfs(int now){
    if(ds[now] != INF)
        return false;
    t++;
    ds[now] = t;
    for(auto it = edges[now].begin(); it != edges[now].end();it++) {
        bool flag = dfs(*it);
        // t++;
    }
    // if(edges[now].size() == 0)
    t++;
    fs[now] = t;
    return true;
}

int check(int n) {
    rep(i, n) {
        if(ds[i] == INF)
            return i;
    }
    return -1;
}

int main(){
    int n; cin >> n;
    vector<int> tmp;
    rep(i, n) {
        edges.push_back(tmp);
        int id, num; cin >> id >> num;
        rep(j, num) {
            int dst; cin >> dst;
            dst--;
            edges[i].push_back(dst);
        }
        // sort(edges[i].rbegin(), edges[i].rend());
    }
    dfs(0);
    while(check(n) != -1) {
        dfs(check(n));
    }
    rep(i, n) {
        cout << i + 1 << " " << ds[i] << " " << fs[i] << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>

using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100

int n, M[N][N];
int color[N], d[N], f[N], t;
int nt[N];

int nxt(int u){
	for (int v = nt[u]; v < n; ++v){
		nt[u] = v + 1;
		if (M[u][v]) return v;
	}
	return -1;
}

void dfs_visit(int idx){
	for (int i = 0; i < n; ++i) nt[i] = 0;

	stack<int> s; s.push(idx);
	color[idx] = GRAY;
	d[idx] = ++t;

	while (!s.empty()) {
		int u = s.top();
		int v = nxt(u);
		if (-1 != v){
			if (WHITE == color[v]){
				color[v] = GRAY;
				d[v] = ++t;
				s.push(v);
			}
		}
		else{
			s.pop();
			color[u] = BLACK;
			f[u] = ++t;
		}
	}
}

void dfs(){
	for (int i = 0; i < n; ++i)	color[i] = WHITE;

	t = 0;
	for (int i = 0; i < n; ++i){
		if (color[i] == WHITE) dfs_visit(i);
	}

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

}


int main(){
	cin >> 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){
		int u, d, v; cin >> u >> d;
		for (int j = 0; j < d; ++j)
		{
			cin >> v;
			M[u - 1][v - 1] = 1;
		}
	}

	dfs();
	return 0;
}