Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var depth [101]int
var finish [101]int
var d [101]int
var n, time int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func visit(graph [][]int, u int) {
	depth[u] = 1
	time++
	d[u] = time
	for i := 0; i < n; i++ {
		if graph[u][i] == 1 {
			if depth[i] == 0 {
				visit(graph, i)
			}
		}
	}
	depth[u] = 2
	time++
	finish[u] = time
}
func main() {
	sc.Split(bufio.ScanWords)

	n = nextInt()
	graph := make([][]int, n)
	for i := 0; i < n; i++ {
		graph[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		s := nextInt()
		t := nextInt()
		if t == 0 {
			continue
		}
		for i := 0; i < t; i++ {
			u := nextInt()
			graph[s-1][u-1] = 1
		}
	}
	for i := 0; i < n; i++ {
		if depth[i] == 0 {
			visit(graph, i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, d[i], finish[i])
	}
}

C++: //
//  main.cpp
//  619
//
//  Created by 8595198919 on 14/06/16.
//  Copyright (c) 2014年 8595198919. All rights reserved.
//

#define _2
#ifdef _1
#include <iostream>
#include <cstdio>
#include <queue>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;

int dist[105];
queue<int> que;
vector<int> G[105];
int main(int argc, const char * argv[])
{
    int n;
    cin >> n;
    for(int i = 0;i <= n;i++) {
        dist[i]=-1;
    }
    for(int i = 0;i < n;i++){
        int u,k;
        int to;
        scanf("%d %d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d",&to);
            G[u].push_back(to);
        }
    }
    que.push(1);
    dist[1]=0;
    while(!que.empty()){
        int now = que.front();
        que.pop();
        int d = dist[now];
        rep(i,G[now].size()){
            if(dist[G[now][i]]==-1){
                dist[G[now][i]]=d+1;
                que.push(G[now][i]);
            }
        }
    }
    for(int i = 1;i <= n;i++) {
        cout <<i <<" " << dist[i]<<endl;
    }
}
#endif

#ifdef _2

#include <iostream>
#include <cstdio>
#include <queue>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;


vector<int> G[105];
int d[105];
int f[105];
int tm = 1;
void dfs(int p){
    d[p]=tm;
    rep(i,G[p].size()){
        if(d[G[p][i]]==-1){
            tm++;
            dfs(G[p][i]);
        }
    }
    f[p] = ++tm;
    return;
}
int main(int argc, const char * argv[])
{
    int n;
    cin >> n;
    for(int i = 0;i <= n;i++) {
        d[i]=-1;
        f[i]=-1;
    }
    for(int i = 0;i < n;i++){
        int u,k;
        int to;
        scanf("%d %d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d",&to);
            G[u].push_back(to);
        }
    }
    dfs(1);
    for(int i = 1;i <= n;i++){
        printf("%d %d %d\n",i,d[i],f[i]);
    }
}

#endif

C++: #include <stack>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
#define N 100
int n, m[N][N];
int color[N], d[N], f[N], tt;
int gcd(int a, int b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}
int mod = 1000000000 + 7;
void dfs_visit(int i){
    color[i] = 1;
    d[i] = ++tt;
    for(int j = 0; j < n; j++){
        if(m[i][j] == 0){
            continue;
        }
        if(color[j] == 0){
            dfs_visit(j);
        }
    }
    color[i] = 2;
    f[i] = ++tt;
}
void dfs(){
    for(int i = 0; i < n; i++){
        color[i] = 0;
    }
    tt = 0;
    for(int i = 0; i < n; i++){
        if(color[i] == 0){
            dfs_visit(i);
        }
    }
    for(int i = 0; i < n; i++){
        cout << i+1 << " "<<  d[i] << " " << f[i] << endl;
    }
}
int main(){
    //std::stack<int> stack;
    int u, k, v;
    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++){
        cin >> u >> k;
        u --;
        for(int j = 0; j < k; j++){
            cin >> v;
            v--;
            m[u][v] = 1;
        }
    }
    dfs();
    //cout << masu << endl;
    return 0;
}

C++: #include <cstdio>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[N][N];
int color[N],d[N],f[N],tt;

//??¨???????????°?????°?????±?????????????´¢
void dfs_visit(int u){
    int v;
    color[u]=GRAY;
    //???????????????
    d[u]=++tt;
    for(v=0;v<n;v++){
        if(M[u][v]==0)
            continue;
        if(color[v]==WHITE)
            dfs_visit(v);
    }
    color[u]=BLACK;
    //????????????
    f[u]=++tt;
}

void dfs(){
    int u;
    //????§????
    for(u=0;u<n;u++)
        color[u]=WHITE;
    tt=0;
    
    for(u=0;u<n;u++)
        //??\????????????u?????????????????±?????????????´¢
        if(color[u]==WHITE)
            dfs_visit(u);
    for(u=0;u<n;u++)
        printf("%d %d %d\n",u+1,d[u],f[u]);
}

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

C++: #include <iostream>

#include <queue>

using namespace std;

int a[101][101];

int n=0;

int found[101];

int ended[101];

int aaa(int i, int time);

int main(){

  for(int i=0; i<101; i++)

    for(int j=0; j<101; j++)

      a[i][j]=0;

  int u, k, v;

  cin>>n;

  for(int i=0; i<n; i++){

    cin>>u>>k;

    for(int j=0; j<k; j++){

      cin>>v;

      a[u][v]=1;

    }

  }

  int time=0;

  for(int i=0; i<101; i++){

    found[i]=0;

    ended[i]=0;

  }

  for(int i=1; i<n+1; i++){

    if(found[i]>0)

      continue;

    time++;

    found[i]=time;

    time=aaa(i, time);

    ended[i]=time;

  } 

  for(int i=1; i<n+1; i++)

    cout<<i<<' '<<found[i]<<' '<<ended[i]<<endl;

}

int aaa(int i, int time){

  for(int k=1; k<n+1; k++){

    if(a[i][k]==1&&found[k]==0){

      time++;

      found[k]=time;

      time=aaa(k, time);

      ended[k]=time;

    } 

  }

  time++;

  return time;

}

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

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

#define NODE_NUM (100)

int n;
int current_time = 0;
int M[NODE_NUM][NODE_NUM] = { 0 };
int ans[NODE_NUM][2] = { 0 };
stack<int> S;

void DepthFirstSearch(int node)
{
	int current, next;

	current_time++;

	S.push(node);
	ans[node - 1][0] = current_time;

	while (!S.empty()) {
		current_time++;

		current = S.top();
		next = -1;

		rep(i, n) {
			if (M[current - 1][i] == 1 && ans[i][0] == 0) {
				next = i + 1;
				break;
			}
		}

		if (next == -1) {
			ans[current - 1][1] = current_time;
			S.pop();
		}
		else {
			ans[next - 1][0] = current_time;
			S.push(next);
		}
	}
}

int main()
{
	int u, k, v;
	bool NowSearching = true;

	cin >> n;
	rep(i, n) {
		cin >> u >> k;
		rep(j, k) {
			cin >> v;
			M[u - 1][v - 1] = 1;
		}
	}

	rep(i, n) {
		if (ans[i][0] == 0) DepthFirstSearch(i + 1);
	}

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

	return 0;
}