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 <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include <float.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
#include <vector>
#include <map>
#include <utility>
#define LLU unsigned long long int
#define LL long long int
#define LD long double
using namespace std;
#define Lfmax DBL_MAX
#define LLUmax ULLONG_MAX
#define LLmax LLONG_MAX
#define dmax INT_MAX
#define vd vector<int>
#define vLL vector<long long int>
#define vLLU vector<unsigned long long int>
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define debug printf("asd\n")
#define debugd(a) printf("--%d\n",a)
#define debugdd(a,b) printf("--%d %d\n",a,b)
#define debugarr(a,b) for(int i=0 ; i<b ; i++)printf("%d ",a[i]);printf("\n")
#define newline printf("\n")
#define forin(a,b,c) for(int a=b ; a<c ; a++)
#define forde(a,b,c) for(int a=b ; a>=c ; a--)
#define UPC(a) (a>='A' && a<='B')
#define LWC(a) (a>='a' && a<='b')

inline void scd(int &a){scanf("%d",&a);}
inline void scdd(int &a,int &b){scanf("%d %d",&a,&b);}
inline void sctd(int &a,int &b,int &c){scanf("%d %d %d",&a,&b,&c);}
inline void sctlld(LL &a,LL &b,LL &c){scanf("%I64d %I64d %I64d",&a,&b,&c);}
inline void scdlld(LL &a,LL &b){scanf("%I64d %I64d",&a,&b);}
inline void sclld(LL &a){scanf("%I64d",&a);}

inline void prd(int a){printf("%d",a);};
inline void prdd(int a,int b){printf("%d %d",a,b);};
inline void prtd(int a, int b, int c){printf("%d %d %d",a,b,c);};
inline void prtlld(LL a, LL b, LL c){printf("%I64d %I64d %I64d",a,b,c);};
inline void prdlld(LL a, LL b){printf("%I64d %I64d",a,b);};
inline void prtlld(LL a){printf("%I64d",a);};

#define scarrd(a,b) for(int index=0 ; index<b ; index++)scanf("%d",&a[index])
#define prarr(a,b) for(int i=0 ; i<b ; i++)printf("%d ",a[i])
#define iniarr(a,b) for(int i=0 ; i<b ;i++)a[i]=0
#define arr(a,b) int *a=(int *)malloc(b*sizeof(int))
#define llarr(a,b) long long int *a=(long long int *)malloc(b*sizeof(long long int))
#define st(a,b) char *a=(char *)malloc(b*sizeof(char))
#define grav 9.8
#define pi 3.14159265


int a[200][200],spos[2000],epos[2000],visit[2000],pos;


void dfs(int now,int n)
{
	//debugd(now);
	spos[now]=pos;
	visit[now]=1;
	pos++;
	for(int i=1 ; i<=n ; i++)
	{
		if(a[now][i]==1 && visit[i]==0)
		dfs(i,n);
	}
	//debugd(now);
	epos[now]=pos;
	pos++;
}

int main()
{
	int c,n,m,b;
	pos=1;
	scd(n);
	forin(i,0,n)
	{
		scdd(c,m);
		forin(j,0,m)
		{
			scd(b);
			a[c][b]=1;			
		}
	}
	
	for(int i=1 ; i<=n ; i++)
	{
		if(visit[i]==0)
		dfs(i,n);
	}
	
	for(int i=1 ; i<=n ; i++)
	printf("%d %d %d\n",i,spos[i],epos[i]);
	return 0;
}

C++: //1908-1930 22m
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define BR printf("\n")
#define INF 2000000000
#define FIND 0
#define END 1

int TimeStump[100][2] = {};//find,end
vector<int> V[100];
stack<int> S;
int time_ = 1;

void DFS(int i){
    if (TimeStump[i][FIND] != 0) return;
    S.push(i);
    TimeStump[i][0] = time_;
    time_++;
    
    REP1(k,V[i].size()){
        if (TimeStump[V[i][k]][FIND] == 0){DFS(V[i][k]);}
    }
    
    S.pop();
    TimeStump[i][1] = time_;
    time_++;
}

int main(){
    INT(N);
    REP1(i,N){
        int from,n;
        scanf("%d %d",&from,&n);
        REP1(j,n){
            INT(to);
            V[i].push_back(to - 1);
        }
    }
    
    REP1(i,N) DFS(i);
    
    REP1(i,N) printf("%d %d %d\n",i+1,TimeStump[i][0],TimeStump[i][1]);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;;

int n,p=0,side[101][101],startTime[101],endTime[101],v[101];

void dfs(int x){
  if(!v[x]) {
    v[x]++; startTime[x]=++p;
	for (int i =1; i<n+1; i++) {
      if(side[x][i]) {
        dfs(i);
      }
    }
	endTime[x]=++p;
  }
}

int main(){
	int a,b,c;
	cin>>n;
	for (int i =0; i<n; i++) {
      cin>> a >> b;
      for (int i =0; i<b; i++) {
        cin >> c;
        side[a][c]++;
      }
    }
  
  for (int i =1; i<n+1; i++) {
    if(!v[i]) dfs(i);
  }
  for (int i =1; i<n+1; i++) {
    cout << i << ' ' << startTime[i] << ' ' << endTime[i] << endl;    
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int d[MAX],f[MAX];
int color[MAX];
vector<int> V[MAX];
int tt=0,n=0;
stack<int> S;

void dfs(int x){
  S.push(x);
  d[x]=++tt;
  color[x]=GRAY;
  while(!S.empty()){
    int u=S.top();
    int next=-1;
    for(int i=0;i<V[u].size();i++){
      if(color[V[u][i]]==WHITE){
        next=V[u][i]; break;
      }
    }
    if(next==-1){
      color[u]=BLACK;
      S.pop();
      f[u]=++tt;
      continue;
    }
    color[next]=GRAY;
    S.push(next);
    d[next]=++tt;
  }
}

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

int main(){
  int u,v,j;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>v;
    u--;
    for(int k=0;k<v;k++){
      cin>>j;
      j--;
      V[u].push_back(j);
    }
  }
  begindfs();
  for(int i=0;i<n;i++){
    cout<<i+1;
    printf(" %d %d\n",d[i],f[i]);
  }

}

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];

// u?????£??\??????v?????????????????????
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;

  // ????¨???????u????§??????¨????????±???????????¢?´¢
  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, i, j;

  cin >> n;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) M[i][j] = 0;
  }

  for (i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}