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<stdio.h>
#include<stack>
using namespace std;
int tu[1000][1000];
int a[10000];
int an[10000];
int d[10000];
int f[10000];
int shijian,n;
int next(int q){
for(int i=an[q];i<n;i++){
    an[q]=i+1;
    if(tu[q][i])return i;
}
return -1;
}
void sou(int u){
     stack<int > s;
     s.push(u);
     a[u]=2;
     d[u]=++shijian;
     while(!s.empty()){
        int x=s.top();
        int v=next(x);
        if(v!=-1){
            if(a[v]==1){
                a[v]=2;
                d[v]=++shijian;
                s.push(v);
            }
        }
        else {
            s.pop();
            a[x]=3;
            f[x]=++shijian;
        }
     }
}
void shensou(){
     for(int u=0;u<n;u++){
        a[u]=1;
        an[u]=0;
     }
     shijian=0;
     for(int u=0;u<n;u++){
        if(a[u]==1)sou(u);
     }
     for(int u=0;u<n;u++){
         printf("%d %d %d\n",u+1,d[u],f[u]);
     }
}


int main(){
   int v;
   int m;
   int x;
   scanf("%d",&n);
   for(int i=0;i<n;i++){
        scanf("%d%d",&v,&m);
        v--;
        for(int j=0;j<m;j++){
            scanf("%d",&x);
            x--;
            tu[v][x]=1;
        }
   }
   shensou();
return 0;
}

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 <stdio.h>
#define N 100
#define WHITE 0
#define GRAY  1
#define BLACK 2

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

void dfs_visit(int u)
{
    int v;
    color[u]=GRAY;
    d[u]=++tt;
    for(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()
{
    int u;
    for(u=0;u<n;u++)
        color[u]=WHITE;
    tt=0;
    for(u=0;u<n;u++)
        if(color[u]==WHITE)
            dfs_visit(u);
    for(u=0;u<n;u++)
        printf("%d %d %d\n",u+1,d[u],f[u]);
}

int main()
{
    int u,v,k,i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            M[i][j]=0;
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&u,&k);
        u--;
        for(j=0;j<k;j++)
        {
            scanf("%d",&v);
            v--;
            M[u][v]=1;
        }
    }
    dfs();
    return 0;
}

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

struct edge{
    int node;
    edge *next;
}nodes[105];

int n,t[105][2],visit[105],cnt=0;

void dfs_visit(int i){
    t[i][0]=++cnt;
    visit[i]=1;
    edge *e=nodes[i].next;
    if(e!=NULL){
        while(e!=NULL){
            if(!visit[e->node])dfs_visit(e->node);
            e=e->next;
        }
    }
    t[i][1]=++cnt;
}

void dfs(){
    int i;
    for(i=1;i<=n;i++){
        if(visit[i]==0)dfs_visit(i);
    }
}

int main(){
    memset(visit,0,sizeof(visit));
    memset(t,0,sizeof(t));
    cin>>n;
    int i,j;
    for(i=1;i<=n;i++){
        int ii,n_i;
        cin>>ii>>n_i;
        edge* first=&nodes[i];
        for(j=0;j<n_i;j++){
            edge *e=new edge();
            cin>>e->node;
            first->next=e;
            first=e;
        }
    }
    dfs();
//    for(i=0;i<n;i++){
//        edge *e=nodes[i].next;
//        while(e!=NULL){
//            cout<<e->node<<" ";
//            e=e->next;
//        }
//        cout<<endl;
//    }
    for(i=1;i<=n;i++){
        cout<<i<<" "<<t[i][0]<<" "<<t[i][1]<<endl;
    }
    return 0;
}

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

#include <cassert>
#include <climits>

#include <string.h>

#include <vector>
#include <map>
#include <stack>

enum Color
{
    kWhite,
    kGray,
    kBlack,
};

// 頂点数.
int n;

// 各頂点の隣接リスト表現.
std::map< int, std::vector<int> > adj_list;

// 各頂点の訪問状態を管理する.
std::vector<Color> c;

// 初回訪問時.
std::vector<int> d;

// 二回目訪問時.
std::vector<int> f;

void makeAdjListFromInput(int n)
{
    for (int i = 0; i < n; ++i)
    {
        int vertex;
        int degree;

        std::cin >> vertex;
        std::cin >> degree;

        assert(vertex <= n);
        assert(degree <= n);

        std::vector<int> adj_vertexes;

        for (int j = 0; j < degree; ++j)
        {
            int adj;
            std::cin >> adj;
            adj_vertexes.push_back(adj - 1);
        }

        adj_list[vertex - 1] = adj_vertexes;
    }
}

int nextVertex(int current)
{
    std::vector< int > adjs = adj_list[current];
    for (std::vector< int >::iterator it = adjs.begin(); it != adjs.end(); ++it)
    {
        if (c[(*it)] == kWhite)
            return (*it);
    }

    // 見つからなかった.
    return INT_MAX;
}

int firstVertex()
{
    for (int i = 0; i < n; ++i)
    {
        if (c[i] == kWhite)
            return i;
    }

    return INT_MAX;
}

void depthFirstSearch()
{
    int counter = 0;
    std::stack<int> tmp_stack;

    // 諸々初期化.
    for (int i = 0; i < n; ++i)
    {
        c.push_back(kWhite);
        d.push_back(0);
        f.push_back(0);
    }

    while (true)
    {
        int first = firstVertex();
        if (first == INT_MAX)
            break;
        tmp_stack.push(first);
        c[first] = kGray;
        d[first] = ++counter;

        while (! tmp_stack.empty())
        {
            int current = tmp_stack.top();
            int next = nextVertex(current);
            if (next != INT_MAX)
            {
                tmp_stack.push(next);
                c[next] = kGray;
                d[next] = ++counter;
            }
            else
            {
                int pop = tmp_stack.top();
                tmp_stack.pop();
                c[pop] = kBlack;
                f[pop] = ++counter;
            }
        }
    }
}

int main()
{


    std::cin >> n;

    makeAdjListFromInput(n);

    depthFirstSearch();

    for (std::map< int, std::vector<int> >::iterator it = adj_list.begin(); it != adj_list.end(); ++it)
    {
        int v = it->first;
        std::cout << v + 1 << " " << d[v] << " " << f[v] << std::endl;
    }

    return 0;
}