Go: #include <bits/stdc++.h>
using namespace std;
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;
static const int N     = 100;

int n, tt;
vector<vector<int>> M(N, vector<int>(N));
vector<int> color(N), d(N), f(N);

void dfs_visit(int u) {
  color[u] = GRAY;
  d[u]     = ++tt;
  for (int 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() {
  tt = 0;

  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE) dfs_visit(u);
  }
  for (int u = 0; u < n; u++) {
    printf("%d %d %d\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, v, k;
  cin >> n;
  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();
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
class Graph{
private:
  int node;
  int edge_num;
  vector<vector<int>> edge;  
  vector<int> f;
  vector<int> d;
  int time;
public:
  //***************Constractor****************************
  Graph(int n,vector<int> a,vector<int> b){
    node = n;
    edge_num = a.size();
    time = 1;
    edge.resize(node);
    f.resize(node,0);
    d.resize(node);    
    rep(i,edge_num){
      edge[a[i]].push_back(b[i]);
    }
  }

  //***********************************************
  
  void simple_dfs(int v){
    f[v] = time++;
    for(auto w:edge[v]){
      if(f[w]>0) continue;
      simple_dfs(w);
    }
    d[v] = time++;
  }

  void run_dfs(int s){//check each node can reach goal
    rep(i,node){
      if(f[i]<=0) simple_dfs(i);
    }
  }

  void show(){
    rep(i,node){
      cout << i+1 << " " << f[i] << " " << d[i] << "\n";
    }
  }
};


int main()
{
  int n;
  cin >> n;
  vector<int> a,b;
  rep(i,n){
    int aa;cin >> aa;aa--;
    int m;cin >> m;
    rep(i,m){
      int bb;cin >> bb;bb--;
      a.push_back(aa);
      b.push_back(bb);      
    }
  }

  Graph gp(n,a,b);
  gp.run_dfs(0);
  gp.show();
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

vector<set<ll>> v(101);
vector<ll> st(101,-1);
vector<ll> et(101,-1);

ll dfs(ll n,ll time) {
  st[n]=time;
  time++;
  for(auto p:v[n]) {
    if(st[p]<0) {
      time=dfs(p,time);
    }
  }
  et[n]=time;
  return time+1;
}

int main() {
  ll n;
  cin >> n;
  for(ll i=0;i<n;i++) {
    ll f,tn;
    cin >> f >> tn;
    for(ll j=0;j<tn;j++) {
      ll temp;
      cin >> temp;
      v[f].insert(temp);
    }
  }
  for(ll i=1;i<=n;i++) {
    v[0].insert(i);
  }
  dfs(0,0);
  for(ll i=1;i<=n;i++) {
    cout << i << " " << st[i] << " " << et[i] << endl;
  }
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
 
typedef long long ll;

#define rep(i,n) for(ll i = 0; i < n; i++)
 
using namespace std;
 
const ll INF = ll(1e18)+1;
const double PI=3.14159265358979323846;

vector<vector<int> > G(105);
vector<int> go(105), back(105);
vector<bool> seen(105);
int now=1;

void dfs(int s){

  go[s]   = now++;
  seen[s] = true;

  rep(i,G[s].size()){
    if(seen[G[s][i]]) continue;    
    dfs(G[s][i]);
  }
  
  back[s] = now++;
}

int main(){
  int n;
  cin >> n;
  rep(i,n){
    int u,k;
    cin >> u >> k;
    rep(j,k){
      int x;
      cin >> x;
      G[u].push_back(x);
    }    
  }
  for(int i=1; i<=n; i++) if(!seen[i]) dfs(i);
  rep(i,n){
    cout << i+1 << " " << go[i+1] << " " << back[i+1] << endl;
  }
}

C++: // 深さ優先探索
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

#define MAX_N 100
// 値の定義
const int EXIST = 1;
const int NO_EXIST = 0;

const int WHITE = 0;// 未訪問
const int GRAY = 1; // 訪問中
const int BLACK = 2;// 訪問完了

// グラフ情報を保持する配列
int ary_graph[MAX_N + 1][MAX_N + 1];

// 各頂点の訪問状態を保持する配列
int color[MAX_N + 1];

// 発見時刻と完了時刻を保持する構造体
typedef struct
{
    int start_time;
    int end_time;
}SearchTime;

SearchTime st_SearchTime[MAX_N];
int search_T = 0;

void depthFirstSearch(int now_vertex, int vertex_num);

int main(void)
{
    int n, u, num, v_num;

    for(int i = 0; i < MAX_N + 1; i++)
    {
        color[i] = WHITE;

        for(int j = 0; j < MAX_N + 1; j++)
        {
            // 配列の初期化
            ary_graph[i][j] = NO_EXIST;
        }
    }

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> u;
        cin >> num;

        for(int j = 0; j < num; j++)
        {
            // 隣接点を記憶する
            cin >> v_num;
            ary_graph[u][v_num] = EXIST;
        }
    }

    // 深さ優先探索を実施
    for(int i = 1; i <= n; i++)
    {
        if( color[i] == WHITE )
        {
            depthFirstSearch(i, n);
        }
    }

    // 結果の表示
    for(int i = 1; i <= n; i++)
    {
        printf("%d %d %d\n", i, st_SearchTime[i].start_time, st_SearchTime[i].end_time);
    }

    return 0;
}

// 深さ優先探索を実施する関数
// now_vertex : 現在の頂点のNo.
// next_vertex : 次の頂点のNo.
// vertex_num : グラフの頂点数
void depthFirstSearch(int now_vertex, int vertex_num)
{
    // 訪問中
    color[now_vertex] = GRAY;

    // 発見時刻の設定
    search_T++;
    st_SearchTime[now_vertex].start_time = search_T;

    for(int i = 1; i <= vertex_num; i++)
    {
        if( color[i] == WHITE
            && ary_graph[now_vertex][i] == EXIST )
        {
            depthFirstSearch(i, vertex_num);
        }
    }

    // 訪問完了
    color[now_vertex] = BLACK;

    // 終了時刻の設定
    search_T++;
    st_SearchTime[now_vertex].end_time = search_T;

    return;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int n; cin >> n; vector<vector<int> >vec(n, vector<int>());
	rep(i, 0, n) {
		int u, k; cin >> u >> k;
		u--;
		rep(j, 0, k) {
			int v; cin >> v; v--;
			vec[u].push_back(v);
		}
	}
	const int DONE = 1;
	const int YET = 0;
	vector<int>check(n, 0);
	vector<pair<int, int> >stamp(n);
	stack<int>S;
	S.push(0);
	int t = 1;

	while (!S.empty()) {
		int u = S.top();
		if (check[u] == YET) {
			stamp[u].first = t++;
			check[u] = DONE;
		}
	here:
		if (DBG) {
			cout << "t == " << t << endl;
			rep(x, 0, n) {
				cout << x+1<<" "<<stamp[x].first << " " << stamp[x].second << endl;
			}
		}
		rep(i, 0, vec[u].size()) {
			if (check[vec[u][i]] == YET) {
				S.push(vec[u][i]);
				check[vec[u][i]] = DONE;
				stamp[vec[u][i]].first = t++;
				u = vec[u][i];
				goto here;
			}
		}
		u = S.top(); S.pop();
		stamp[u].second = t++;
	}
	
	rep(i, 0, n) {
		cout << i + 1 << " " << stamp[i].first << " " << stamp[i].second << endl;;
	}

	return 0;
}

C++: #include <cstdio>
int dt[101][101];
int d[101], f[101];

int n;
int t=1;
void dfs(int s){
	d[s]=t++;
	for(int i=1;i<=n;i++){
		if(dt[s][i]&&d[i]==0){
			dfs(i);
		}
	}
	f[s]=t++;
}
int main(){
	int fi, v, g;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&fi,&v);
		for(int j=1;j<=v;j++){
			scanf("%d",&g);
			dt[fi][g]=1;
		}
	}
	d[1]=1;
	int flag=true;
	dfs(1);
	for(int i=1;i<=n;i++){
		if(!d[i]){
			dfs(i);
		}
	}
			
	for(int i=1;i<=n;i++){
		printf("%d %d %d\n",i,d[i],f[i]);
	}
	return 0;
}