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 <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i,m,n) for(int i = m - 1; i >= n; i--)
#define ALL(v) v.begin(), v.end()
#define CMAX(m, x)  m = max(m,x)
#define CMIN(m, x)  m = min(m,x)
#define itn int
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define YES() cout << "YES" << endl
#define NO() cout << "NO" << endl
typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;

int cnt = 0;
void dfs(int i,vector<vector<int>> &g, vector<vector<int>> &ans){
    if(ans[i][0] == 0){
        cnt++; 
        ans[i][0] = cnt;
        REP(j,g[i].size()){
            dfs(g[i][j] - 1,g,ans);  
        }
        cnt++;

        ans[i][1] = cnt;
    }
}
int main(){
    int n;
    cin >> n;
    int u,k;
    vector<vector<int>> g(n);
    vector<vector<int>> ans(n,vector<int>(2,0));
    REP(i,n){
        cin >> u >> k;
        g[u - 1] = vector<int>(k);
        REP(j,k)
            cin >> g[u - 1][j];
    }
    REP(i,n)
        dfs(i,g,ans);
    REP(i,n){
        printf("%d %d %d\n",i + 1, ans[i][0],ans[i][1]);
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;
#define MAX 100000

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>

int A[101][101];
int ans[101][3];
int an = 1;


int judge(int i, int j){
  if(ans[i][j] == 0){
    ans[i][j] = an;
    an++;
  }
}

int serch(int a[101][101], int  ni, int n){
  int  j;
  
  for(j=0; j<n; j++){
    if(A[ni][j] == 1){
      judge(j, 1);
      serch(A, j, n);
    }
  }
  judge(ni, 2);
}

int  main(){
  int i, j, k, n, x, y;
  
  scanf("%d", &n);
  
  for(i=0; i<n; i++){
    for(j=0; j<n; j++)
      A[i][j] = 0;
    ans[i][0] = i+1;
  }
  
  for(i=0; i<n; i++){
    scanf("%d %d", &x, &y);
    for(j=0; j<y; j++){
      scanf("%d", &k);
      A[i][k-1] = 1;
    }
  }
  
  for(j=0; j<n; j++){
  judge(j,1);
    for(i=0; i<n; i++){
      if(A[j][i] == 1){
        judge(i, 1);
        serch(A, i, n);
      }
    }
  judge(j, 2);
  }
  
  for(i=0; i<n; i++){
    for(j=0; j<3; j++){
      if(j>0)
        printf(" ");
      printf("%d", ans[i][j]);
    }
    printf("\n");
  }
  
  return 0;
}

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

using namespace std;

int t;
int N;
int d[110], f[110];
vector<vector<int> > adj(110);
void dfs(int cur){
    d[cur] = t;
    t += 1;
    //cerr << "visiting " << cur << " " << t << endl;
    for(int i = 0; i < adj[cur].size(); ++i){
        int dst = adj[cur][i];
        if(d[dst] == 0){
            dfs(dst);
        }
    }
    f[cur] = t;
    t++;
}

int main(){
    cin >> N;
    t = 1;
    int u, k;
    for(int i = 0; i < N; ++i){
        d[i] = 0;
        f[i] = 0;
    }
    for(int i = 0; i < N; ++i){
        cin >> u >> k;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            adj[u].push_back(v);
        }
        sort(adj[u].begin(), adj[u].end());
    }
    for(int i = 1; i < N+1; ++i){
        if(d[i] == 0) dfs(i);
    }
    
    for(int i = 1; i < N+1; ++i){
        cout << i << " " << d[i] << " " << f[i] << endl;
    }
}

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

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int V = 100;
int n, t;
vector<edge> es[V];
int d[V], f[V];

void dfs(int v) {
  int i;
  d[v] = ++t;
  for (i=0; i<(int)es[v].size(); i++) {
    int to = es[v][i].to;
    if (d[to] == -1) dfs(to);
  }
  f[v] = ++t;
}

int main() {
  int i, j;
  memset(d, 0xff, sizeof(d));
  memset(f, 0, sizeof(f));
  for (i=0; i<V; i++) es[i].clear();

  scanf("%d ", &n);
  for (i=0; i<n; i++) {
    int u, v, k;
    scanf("%d ", &u);
    scanf("%d ", &k);
    for (j=0; j<k; j++) {
      scanf("%d ", &v);
      es[u-1].psb(edge(u-1, v-1, 0));
    }
  }

  t = 0;
  for (i=0; i<n; i++)
    if (d[i] == -1) dfs(i);

  for (i=0; i<n; i++) 
    printf("%d %d %d\n", i+1, d[i], f[i]);

	return 0;
}

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

#define N 100

using namespace std;

int d[N],f[N],m[N][N],ucolor[N],t,n,nt[N];

int next(int u2){
	int r;
	for(r=nt[u2];r<n;r++){
		nt[u2] = r+1;
		if(m[u2][r]){
			return r;
		}
	}
	return -1;
}

void dfsvisit(int uu){
	int u3=0,v,i;
	for(i=0;i<n;i++){
		nt[i] = 0;
	}
	stack<int> s;
	s.push(uu);
	ucolor[uu] = 1;
	d[uu] = ++t;
	while( !s.empty() ){
		u3 = s.top();
		v = next(u3);
		if(v != -1){
			if(ucolor[v] == 0){
				ucolor[v] = 1;
				d[v] = ++t;
				s.push(v);
			}
		}else{
			s.pop();
			ucolor[u3] = 2;
			f[u3] = ++t;
		}
	}
}

void dfs(){
	int i,j;
	for(i=0;i<n;i++){
		ucolor[i] = 0;
		nt[i] = 0;
	}
	for(j=0;j<n;j++){
		if(ucolor[j]==0){
			dfsvisit(j);
		}
	}
	for(i=0;i<n;i++){
		printf("%d %d %d\n",i+1,d[i],f[i]);
	}
}

int main(void){
	int i,j,u1,k,box;
	t = 0;// 時間の初期化
	for(i=0;i<N;i++){
		for(j=0;j<N;j++)m[i][j]=0;
	}
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&u1,&k);
		u1--; //頂点番号、その隣接する頂点の数
		for(j=0;j<k;j++){
			scanf("%d",&box);
			box--; // 隣接する頂点の番号
			m[u1][box] = 1;
		}
	}
	dfs();
	return 0;
}