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 <vector>
using namespace std;

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B

vector<int> map[100];
int found[100];
int finish[100];
bool visit[100];

int dfs(int x, int t) {
	found[x] = t;
	visit[x] = true;
	for (int i = 0; i < map[x].size(); i++) {
		if (visit[map[x][i]]) continue;
		t = dfs(map[x][i], t + 1);
	}
	finish[x] = ++t;
	return t;
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a, b, c;
		cin >> a >> c;
		for (int j = 0; j < c; j++) {
			cin >> b;
			map[a - 1].push_back(b - 1);
		}
	}
	
	int t = 1;
	for (int i = 0; i < n; i++) 
		if (!visit[i]) t = dfs(i, t) + 1;
	
	for (int i = 0; i < n; i++)
		cout << (i + 1) << " " << found[i] << " " << finish[i] << endl;
	return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

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){
            cout<<"a"<<endl;
            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 <cstdio>

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

using namespace std;

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

void dfs(int u){
  color[u]=GRAY;
  d[u]=++t;
  for(int i=1;i<=n;i++){
    if(adj[u][i]==1 && color[i]==WHITE) dfs(i);
  }
  color[u]=BLACK;
  f[u]=++t;
}

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

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 <cstdio>
#include <iostream>
#include <stack>
#define MAX_VERTEXES 100
#define ZERO 0
#define NIL -1

using namespace std;

//think easy make easy

int n;//num of vertexes(頂点)
int matrix_graph[MAX_VERTEXES+1+1][MAX_VERTEXES+1+1];//1オリジン

stack<int> stk;
bool discovered[MAX_VERTEXES+1+1];//1オリジン
int discovered_time[MAX_VERTEXES+1+1];
int completed_time[MAX_VERTEXES+1+1];
int current_time = 0;

void DFS(int v){
	int v_tmp;
	for(int i=1;i<=n;i++){
		if(i == v);
		else if(matrix_graph[v][i] == 1 && !discovered[i]){
			current_time++;
			discovered_time[i] = current_time;
			stk.push(v);
			matrix_graph[v][i] == 0;
			discovered[i] = true;
			DFS(i);
		}else if(matrix_graph[v][i] == 1 && discovered[i]){
			matrix_graph[v][i] == 0;
		}

		if(i == n){
			current_time++;
			completed_time[v] = current_time;
			//if(!stk.empty())
			//{
				//cout <<stk.top() <<endl;
				//v_tmp = stk.top();
				//stk.pop();
				//DFS(v_tmp);
			//}
		}
	}
}

int main(){
	scanf("%d",&n);

	int v_num;
	int k;//v_numの出次数

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			matrix_graph[i][j] = ZERO;
		}
	}

	for(int i=0;i<n;i++){
		scanf("%d",&v_num);
		scanf("%d",&k);

		for(int j=0;j<k;j++){
			int v_num2;
			scanf("%d",&v_num2);
			matrix_graph[v_num][v_num2] = 1;
		}
	}

	for(int i=1;i<=n;i++){
		discovered[i] = false;
		discovered_time[i] = NIL;
		completed_time[i] = NIL;
	}

	for(int i=1;i<=n;i++){
		current_time++;
		if(!discovered[i]){
			discovered[i] = true;
			discovered_time[i] = current_time;
			DFS(i);
		}
	}

	for(int i=1;i<=n;i++){
		cout <<i <<" " <<discovered_time[i] <<" " <<completed_time[i] <<endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> d;
vector<int> f;
vector<bool> used;
vector< vector<int> > v;
int n;
int cnt = 1;

void dfs(int pos){
  d[pos] = cnt; cnt++;
  used[pos] = true;
  for(int i = 0; i < n; i++){
    if(v[pos][i] && !used[i]){
      dfs(i);
    }
  }
  f[pos] = cnt; cnt++;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  d = f = vector<int>(n, -1);
  used = vector<bool>(n, false);
  v = vector< vector<int> >(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      int a; cin >> a;
      a--;
      v[u][a] = 1;
    }
  }
  dfs(0);
  for(int i = 0; i < n; i++){
    cout << i+1 << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1e18;
long long mod=1e9+7;
vector<vector<int>>edge(110);
int find_time[110]={};
int end_time[110];
bool reached[110]={};
int now_time=0;

void search(int now,int before){
    if(reached[now]){
        //cout<<now+1<<"already finished"<<endl;
        return;
    }
    reached[now]=true;

    //if(now==before)return;
    now_time++;
    //cout<<now_time<<"time "<<now+1<<"p "<<before+1<<"before"<<endl;
    if(find_time[now]==0){
        find_time[now]=now_time;
    }
    
    for(auto next:edge[now]){
        if(next==before||end_time[now]!=0)continue;
        search(next,now);
    }
    now_time++;
    end_time[now]=now_time;
    return;
}
int main(){


    int n;
    cin>>n;
    rep(i,n){
        int u,k;
        cin>>u>>k;
        u--;
        rep(l,k){
            int next;
            cin>>next;
            next--;
            edge[u].emplace_back(next);
        }
    }
    rep(i,n)search(i,-1);
    //cout<<endl;
    rep(i,n){
        cout<<i+1<<" "<<find_time[i]<<" "<<end_time[i]<<endl;;
        //if(i!=n-1)cout<<endl;
    }
    return 0;

}
/*


*/