Go: package main

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

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	starts := make([]int, nVerts+1)
	ends := make([]int, nVerts+1)
	time := 1

	var dfs func(u int)
	dfs = func(u int) {
		if starts[u] > 0 {
			return
		}

		starts[u] = time
		time += 1

		for _, v := range graph[u] {
			dfs(v)
		}

		ends[u] = time
		time += 1
	}

	for i := 1; i <= nVerts; i++ {
		dfs(i)
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d %d\n", i, starts[i], ends[i])
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<stack>
using namespace std;
static const int N=100;
static const int WHITE = 0;
static const int GRAY =1;
static const int BLACK=2;

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

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_visit(int r){
  for(int i=0;i<n;i++)nt[i]=0;

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

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

void dfs(){
  for(int i=0;i<n;i++){
    color[i]=WHITE;
    //nt[i]=0;
  }
  tt=0;
  for(int u=0;u<n;u++){
    if(color[u]==WHITE)dfs_visit(u);
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
  }
}
int main(){
  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();
  return 0;
}

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

const long N = 100;
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[N + 1][N + 1];
long color[N + 1], d[N + 1], f[N + 1], tt;
long nt[N + 1];

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

void dfs_visit(long r){
	for (long i = 1; i <= n; i++) nt[i] = 0;

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

	while (!S.empty()){
		long u = S.top();
		long v = next(u);
		if (v != -1){
			if (color[v] == WHITE){
				color[v] = GRAY;
				d[v] = ++tt;
				S.push(v);
			}
			else {
				S.pop();
				color[u] = BLACK;
				f[u] = ++tt;
			}
		}
	}
}

void dfs(){
	for (long i = 1; i <= n; i++){
		color[i] = WHITE;
		nt[i] = 0;
	}
	tt = 0;

	for (long u = 1; u <= n; u++){
		if (color[u] == WHITE) dfs_visit(u);
	}
	for (long i = 1; i <= n; i++){
		cout << i + 1 << " " << d[i] << " " << f[i] << endl;
	}
}

int main(){
	long u, k, v;
	cin >> n;
	for (long i = 1; i <= n; i++){
		for (long j = 1; j <= n; j++){
			M[i][j] = 0;
		}
	}

	for (long i = 1; i <= n; i++){
		cin >> u >> k;
		for (long j = 1; j <= n; j++){
			cin >> v;
			M[u][v] = 1;
		}
	}

	dfs();

	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

bool was_output = false;
template<class t>
void output(t a){
	if(was_output)cout << " ";
	cout << a;
	was_output = true;
}
void outendl(){
	was_output = false;
	cout << endl;
}

vvector<int> edges;
vector<int> found_time;
vector<int> finish_time;

int dfs(int place, int time){
	++time;
	found_time[place] = time;
	foreach(i, edges[place]){
		if(found_time[i]>=0)continue;
		time = dfs(i, time);
	}
	++time;
	finish_time[place] = time;
	return time;
}

int main(){
	int n;
	cin >> n;
	edges.resize(n);
	found_time.resize(n, -1);
	finish_time.resize(n, -1);
	rep(i, n){
		int t;
		int time;
		cin >> t >> time;
		--t;
		rep(j, time){
			int to;
			cin >> to;
			--to;
			edges[t].push_back(to);
		}
	}
	int time = 0;
	rep(i, n){
		if(found_time[i]<0)time = dfs(i, time);
	}
	rep(i, n){
		output(i+1);
		output(found_time[i]);
		output(finish_time[i]);
		outendl();
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define bit(n) (1LL<<(n))
#define pcnt(x) __builtin_popcountll(x)
#define PB push_back
#define MP make_pair
#define endl "\n"
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;
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; }
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int d[110],f[110];
int t;
bool visit[110];
vector<vector<int>> vec(110);

void dfs(int a) {
  if(!visit[a]){
    visit[a] = true;
    d[a] = ++t;
    for(auto x: vec[a]) dfs(x);
    f[a] = ++t;
  }
}

int main() {
  int n; cin >> n;
  FOR(i,0,n-1){
    int u,k,v; cin >> u >> k;
    --u;
    FOR(j,1,k){
      cin >> v;
      --v;
      vec[u].PB(v);
    }
  }
  FOR(i,0,n-1) dfs(i);
  FOR(i,0,n-1) cout << i+1 << " " << d[i] << " " << f[i] << "\n";




}

C++: #include <iostream>
#include "memory.h"
#include <stack>

using namespace std;

bool matrix[100][100];
int n;

int next(int k){
	for(int i = 0; i < n; i++){
		if(matrix[k][i] == true)
			return i + 1;
	}
	return false;
}

int main(){
	int index, temp, num, current, count = 1;
	int start_end[100][2];
	stack<int> back;

	memset(matrix, 0, sizeof(matrix));
	memset(start_end, 0, sizeof(start_end));

	cin >> n;

	for(int i = 0; i < n; i++){
		cin >> index >> num;
		while(num--){
			cin >> temp;
			matrix[index-1][temp-1] = true;
		}
	}
	back.push(0);
	current = 0;
	while(!back.empty()){
		int r;
		r = next(current);
		if(start_end[current][0] == 0){
			start_end[current][0] = count;
			count++;
		}
		if(r){
			matrix[current][r-1] = false;
			back.push(current);
			current = r - 1;
		}
		else{
			if(start_end[current][1] == 0){
				start_end[current][1] = count;
				count++;
			}
			current = back.top();
			back.pop();
		}
		
	}
	for(int i = 0; i < n; i++){
		cout << i + 1 << " " << start_end[i][0] << " " << start_end[i][1] << endl;
	}


}