Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	currentVertex := &vertices[currentId-1]
	if currentVertex.isVisited() {
		return timestamp
	}
	timestamp = timestamp + 1
	currentVertex.firstVisitAt = timestamp
	for _, adjId := range currentVertex.adjs {
		timestamp = dfs(adjId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func solveDfs(vertices []vertex) {
	timestamp := 0
	for {
		vid := vertexId(-1)
		for i := 0; i < len(vertices); i++ {
			if !vertices[i].isVisited() {
				vid = vertexId(i+1)
				break
			}
		}
		if vid > 0 {
			timestamp = dfs(vid, timestamp, vertices)
		} else {
			break
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveDfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-22T23:42:35+09:00
 */

#include<bits/stdc++.h>
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int n;
int m[100][100];
int color[100],d[100],f[100],tt;

int nt[100];

//color while-1 Gray-2 Black-3
int next(int u){
    for(int v=nt[u]; v<n; ++v){
        nt[u] = v + 1;
        if(m[u][v]) return v;
    }
    return -1;
}

void dfs(int r){
    rep(i,n) nt[i] = 0;

    stack<int> S;
    S.push(r);
    color[r] = 1;
    d[r] = ++tt;

    while(!S.empty()){
        int u = S.top();
        int v = next(u);
        if(v != -1){
            if(color[v] == 0){
                color[v] = 1;
                d[v] = ++tt;
                S.push(v);
            }
        }else{
            S.pop();
            color[u] = 2;
            f[u] = ++tt;
        }
    }
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i,n)rep(j,n) m[i][j] = 0;

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

  //dfs

  rep(i,n){
       color[i] = 0;
       nt[i] = 0;
   }
  tt = 0;

  rep(u,n){
      if(color[u] == 0) dfs(u);
  }

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

  return 0;
}
/********  Main() Ends Here *************/

C++: #include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
void func(int p);
typedef pair<int,int>P;
int n;
array<array<int,101>,101>arr;
array<P,101>arrp;
array<bool,101>arrb;
int Time;
int main(void){
	int u,k,v;
	int i,j;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			arr[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			arr[u][v]=1;
		}
	}
	arrb.fill(false);
	Time=0;
	for(i=1;i<=n;i++){
		if(!arrb[i]){
			func(i);
		}
	}
	for(i=1;i<=n;i++){
		cout<<i<<" "<<arrp[i].F<<" "<<arrp[i].S<<endl;
	}
	return 0;
}
void func(int p){
	int i,j;
	//cout<<p<<endl;
	arrb[p]=true;
	Time++;
	arrp[p].F=Time;
	for(i=1;i<=n;i++){
		if(arr[p][i]==1){
			if(!arrb[i]){
				func(i);
			}
		}
	}
	Time++;
	arrp[p].S=Time;
}