Go: package main

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

var N int
var A [][]int
var t int
var color, d, f []int

const (
	WHITE = iota
	GRAY
	BLACK
)

func dfsVisit(u int) {
	color[u] = GRAY
	t ++
	d[u] = t
	for v:=0; v<N; v++ {
		if A[u][v] == 0 {
			continue
		}
		if color[v] == WHITE {
			dfsVisit(v)
		}
	}
	color[u] = BLACK
	t++
	f[u] = t
}

func dfs() {
	for u:=0; u<N; u++ {
		if color[u] == WHITE {
			dfsVisit(u)
		}
	}
	for u:=0; u<N; u++{
		fmt.Printf("%d %d %d\n", u+1, d[u], f[u])
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	color = make([]int, N)
	d = make([]int, N)
	f = make([]int, N)
	A = make([][]int, N)
	for i:=0; i<N; i++{
		A[i] = make([]int, N)
	}
	for i:=0; i<N; i++{
		index := nextInt(scanner) - 1
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			v := nextInt(scanner) - 1
			A[index][v] = 1
		}
	}
	dfs()
}

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

int n;
vector < int > G[105];
int st[105] = {};
int et[105];
int t = 1;

void dfs(int v)
{
    if ( st[v] != 0 ) return;
    st[v] = t;
    t++;
    for ( auto &x : G[v] ) {
        dfs(x);
    }
    et[v] = t;
    t++;
}

int main()
{
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        int u, k;
        cin >> u >> k;
        for ( int j = 0; j < k; j++ ) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
        sort(begin(G[u]), end(G[u]));
    }

    for ( int i = 1; i <= n; i++ ) dfs(i);
    
    for ( int i = 1; i <= n; i++ ) {
        cout << i << " " << st[i] << " " << et[i] << endl;
    }

    return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
template<class T>
ll sum(const T& a){return accumulate(all(a),0LL);}

ll n;
ll now;
vector<vector<bool> > g;
vector<bool> used;
vl startTime,endTime;
void dfs(ll idx){
    used[idx]=1;
    startTime[idx]=++now;
    rep(i,n){
        if(g[idx][i]&&!used[i])dfs(i);
    }
    endTime[idx]=++now;
}
void solve(){
    cin>>n;
    g=vector<vector<bool> >(n,vector<bool>(n));
    used=vector<bool>(n);
    startTime=vl(n);
    endTime=vl(n);
    rep(i,n){
        ll idx,m;cin>>idx>>m;
        idx--;
        rep(k,m){
            ll to;cin>>to;to--;
            g[idx][to]=true;
        }
    }
    rep(i,n){
        if(!used[i])dfs(i);
    }
    rep(i,n){
        cout<<(i+1)<<" "<<startTime[i]<<" "<<endTime[i]<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}

C++: #include <iostream>
#include <stack>

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[MAX];

void input(){
    for(int i=0;i<MAX;i++){
        dst[i] = -1;
        for(int j=0;j<MAX;j++){
            matrix[i][j] = 0;
        }
    }
    for(int i=1;i<=num;i++){
        int v; cin >> v;
        int tmp; cin >>tmp;
        for(int j=0;j<tmp;j++){
            int vi; cin >> vi;
            matrix[v-1][vi-1] = 1;
        }
    }
}

void bfs(){
    stack<int> S;
    S.push(0);
    dst[0] = 0;
    while(S.size() != 0){
        int cur = S.top();
        for(int i=0;i<num;i++){
            if(matrix[cur][i]==1){
                if(dst[i]==-1){
                    S.push(i);
                    dst[i] = dst[cur] + 1;
                }
            }
        }
        S.pop();
    }
    for(int i=0;i<num;i++){
        cout << i+1 << " " <<  dst[i] << endl;
    }
}

int main(){
    cin >> num;
    input();
    bfs();
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

vector<vector<int>> g;
vector<int> d, f;

void dfs(int v, int &t) {
	d[v] = t++;
	for (auto nv : g[v]) {
		if (d[nv]) continue;
		dfs(nv, t);
	}
	f[v] = t++;
}

int main() {
	int n;
	cin >> n;
	g.resize(n);
	rep(i, n) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(i, k) {
			int v;
			cin >> v;
			v--;
			g[u].push_back(v);
		}
	}
	d.assign(n, 0);
	f.assign(n, 0);
	int t = 1;
	for (int i = 0; i < n; i++) {
		if (d[i]) continue;
		dfs(i, t);
	}
	rep(i, n) {
		cout << i + 1 << " " << d[i] << " " << f[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <queue>

using namespace std;

int d[101];
int f[101];
bool isUsed[101];
int cnt = 0;

void DFS(vector< vector<int> >& G, int vertex)
{
	d[vertex] = ++cnt;
	for (int i = 0; i < G[vertex].size(); ++i)
	{
		if (G[vertex][i] == 1 && isUsed[i] == false)
		{
			isUsed[i] = true;
			DFS(G, i);
		}
	}
	f[vertex] = ++cnt;
}

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > G(n, vector<int>(n));
	for (int i = 0; i < n; ++i)
	{
		int u, k;
		cin >> u >> k;
		--u;
		for (int j = 0; j < k; ++j)
		{
			int v;
			cin >> v;
			--v;
			G[u][v] = 1;
		}
	}
	for (int i = 0; i < 101; ++i)
	{
		isUsed[i] = false;
	}
	for (int i = 0; i < n; ++i)
	{
		if (isUsed[i] == false)
		{
			isUsed[i] = true;
			DFS(G, i);
		}
	}
	for (int i = 0; i < n; ++i)
	{
		cout << i + 1 << " " << d[i] << " " << f[i] << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

struct GraphParser {
  GraphParser(vector<vector<int>>& edges_)
      : edges(edges_), start(edges.size(), 0), end(edges.size(), 0) {
  }

  void run() {
    t = 0;
    for (size_t i = 0; i < edges.size(); ++i) {
      if (!start[i]) {
        recur(i);
      }
    }
  }

  void run_stack() {
    t = 0;
    for (size_t j = 0; j < edges.size(); ++j) {
      if (start[j]) continue;
      start[j] = ++t;
      st.push(make_pair(j, 0));
      while(!st.empty()) {
        auto& p = st.top();
        auto v = p.first;
        auto& i = p.second;
        if (i < edges[v].size()) {
          auto v2 = edges[v][i++];
          if (!start[v2]) {
            start[v2] = ++t;
            st.push(make_pair(v2, 0));
          }
        } else {
          end[v] = ++t;
          st.pop();
        }
      }
    }
  }

  vector<vector<int>>& edges;
  vector<int> start;
  vector<int> end;
  stack<pair<int, size_t>> st;
  int t;

 private:
  void recur(int cur) {
    if (start[cur]) {
      return;
    }
    start[cur] = ++t;
    for (auto&& e : edges[cur]) {
      recur(e);
    }
    end[cur] = ++t;
  }
};

int main() {
  int n;
  cin >> n;
  vector<vector<int>> edges(n, vector<int>());
  for (int i = 0; i < n; ++i) {
    int u;
    int k;
    cin >> u >> k;
    edges[u - 1].reserve(k);
    for (int j = 0; j < k; ++j) {
      int v;
      cin >> v;
      edges[u - 1].push_back(v - 1);
    }
  }

  GraphParser p(edges);
  p.run_stack();
  for (int i = 0; i < n; ++i) {
    cout << (i + 1) << " " << p.start[i] << " " << p.end[i] << endl;
  }
}