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 <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define debug(x) cout<< #x <<": "<<x<<endl
#define debug2(x,y) cout<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
template<class T> using vv=vector<vector< T > >;

vi ts_d;
vi ts_f;

template<class T>
void dfs(int cur, int &time_visit, T graph) {
  // visiting cur
  time_visit += 1;
  ts_d[cur] = time_visit;
  for (auto dst : graph[cur]) {
    if (ts_d[dst] == 0) {
      dfs(dst, time_visit, graph);
    }
  }
  time_visit += 1;
  ts_f[cur] = time_visit;
  return;
}

int main() {
  int time = 0;
  int n;
  cin >> n;
  ts_d.reserve(n);
  ts_f.reserve(n);
  ts_d.assign(1, 0);
  vector<set<int> > graph(n, set<int>());

  for (int i = 0; i < n; ++i) {
    int num;
    cin >> num;
    num -= 1;
    int out;
    cin >> out;
    for (int j = 0; j < out; ++j) {
      int tmp;
      cin >> tmp;
      tmp -= 1;
      graph[i].insert(tmp);
    }
  }

  for (int i = 0; i < n; ++i) {
    if (ts_d[i] == 0) {
      dfs(i, time, graph);
    }
  }

  for (int i = 0; i < n; ++i) {
    printf("%d %d %d\n", i+1, ts_d[i], ts_f[i]);
  }

  return 0;
}

C++: #include<stdio.h>
int d[101],e[101],t=0,n;
bool went[101],graph[101][101];
void dfs(int x){
    if(went[x])return;
    went[x]=true;
    t++;
    d[x]=t;
    for(int i=1;i<=n;i++){if(graph[x][i])dfs(i);}
    t++;
    e[x]=t;
    return;
}

int main(){
    int m,temp,a;
    scanf("%d",&n);
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++)graph[i][j]=false;
        went[i]=false;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&m);
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            graph[a][temp]=true;
        }
    }
    for(int i=1;i<=n;i++)dfs(i);
    for(int i=1;i<=n;i++)printf("%d %d %d\n",i,d[i],e[i]);
}

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

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1

using namespace std;

int adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1],f[MAX+1];
stack<int> S;
int n;
int t=0;

int next(int u){
  for(int i=1;i<=n;i++){
    if(adj[u][i]==1&&color[i]==WHITE) return i;
  }
  return NIL;
}

void dfs(int u){
  S.push(u);
  color[u]=GRAY;
  d[u]=++t;
  while(S.empty()==false){
    u=S.top();
    int v=next(u);
    if(v!=NIL){
      color[v]=GRAY;
      d[v]=++t;
      S.push(v);
    }else{
      S.pop();
      color[u]=BLACK;
      f[u]=++t;
    }
  }
}

void dfs_init(){
  for(int i=1;i<=n;i++) color[i]=WHITE;
  dfs(1);
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  dfs_init();
  for(int id=1;id<=n;id++){
    printf("%d %d %d\n",id,d[id],f[id]);
  }
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)

int d[105], f[105];
bool seen[105];

void dfs(vector<vector<int> > &G, int n, int &x){
    if(seen[n]) return;
    x++;
    d[n] = x;
    seen[n] = true;

    rep(i, G[n].size()){
        int p = G[n][i];
        if(seen[p]) continue;
        dfs(G, p, x);
    }

    x++;
    f[n] = x;
}

int main(){
    int n;
    cin >> n;

    vector<vector<int> > G(n);
    rep(i, n){
        int u, k;
        cin >> u >> k;
        rep(j, k){
            int v;
            cin >> v;
            v--;
            G[i].push_back(v);
        }
    }

    rep(i, n){
        seen[i] = false;
    }
    int x = 0;
    rep(i, n){
        dfs(G, i, x);
    }

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


    return 0;
}

C++: #include<cstdio>
const char N_MAX = 105;
short time=0;
bool matrix[N_MAX][N_MAX];
char n;
struct Node{
    char state;//0:undiscovered 1:active 2:end
    short stTime, endTime;
};
Node V[N_MAX];
char next(char num){
    for(char i=0;i<n;i++){
        if(matrix[num][i]&&V[i].state==0){
            return i;
        }
    }
    return -1;
}
void dfs(char num){
    if(num==-1) return;
    V[num].stTime = ++time;
    V[num].state = 1;
    for(char i=next(num); i!=-1;i=next(num)){
        dfs(i);
    }
    V[num].state = 2;
    V[num].endTime = ++time;
}
void buildMatrix(){
	for(char i=0; i<n;i++){
		int degree, index;
		scanf("%u%u", &index, &degree);
		for(char j=0; j<degree; j++){
			int temp;
			scanf("%d", &temp);
			matrix[index-1][temp-1] = true;
		}
	}
}
int main(){
    scanf("%d", &n);
    buildMatrix();
    for(char i=0; i<N_MAX; i++){
        if(V[i].state==0){
            dfs(i);
        }
    }
    for(char i=0;i<n;i++){
        printf("%d %d %d\n", i+1, V[i].stTime, V[i].endTime);
    }
    return 0;
}

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

using namespace std;

struct Edge
{
	int to;
};

vector<vector<int>> DFS(vector<vector<Edge>>& G)
{
	int timecounter = 0;
	vector<vector<int>> result;
	result = vector<vector<int>>(2, vector<int>(G.size(), 0));
	vector<int> prev(G.size(),0);
	
	for(int i = 0; i < G.size(); i++){
		int next = i + 1;
		while(result[1][i] == 0){
			int tmp;
			if(result[0][next - 1] == 0){
				timecounter += 1;	
				result[0][next - 1] = timecounter;
			}

			if(G[next - 1].empty()){
				if(result[1][next - 1] == 0){
					timecounter += 1;
					result[1][next - 1] = timecounter;
				}
				next = prev[next - 1];
			}else{
				tmp = G[next - 1].begin()->to;
				G[next - 1].erase(G[next - 1].begin());
				prev[tmp - 1] = next;
				next = tmp;
			}
		}
	}
	return result;
}

int main(void)
{
	int n;
	cin >> n;

	vector<vector<Edge>> AdjList(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;
			AdjList[i].push_back(Edge{v});
		}
	}

	vector<vector<int>> timestump = DFS(AdjList);
	
	for(int i = 0; i < AdjList.size(); i++){
		cout << i + 1 << " " << timestump[0][i] << " " << timestump[1][i] << endl;
	}
	return 0;
}