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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

std::vector<vector<int> > G;
std::vector<int> flag;
std::vector<int> in;
std::vector<int> out;
int N;
int dfs(int u, int count) {
	if (flag[u] == 1) return -1;
	flag[u] = 1;
	
	in[u] = count;
	for (int i = 0; i < N; ++i) {
		if (flag[i] == 0 && G[u][i] == 1) {
			count = dfs(i, count + 1);
		}
	}
	out[u] = count + 1;
	return count + 1;
}

int main(int argc, char const *argv[]) {
	
	cin >> N;

	for (int i = 0; i < N; ++i) {
		G.push_back(vector<int>());
		flag.push_back(0);
		in.push_back(0);
		out.push_back(0);
		for (int j = 0; j < N; ++j) {
			G[i].push_back(0);
		}
	}

	for (int i = 0; i < N; ++i) {
		int u, k;
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; ++j) {
			int v;
			cin >> v;
			v--;
			G[u][v] = 1;
		}
	}

	int count = 1;
	for(int i = 0; i < N; i++) {
		int r = dfs(i, count);
		if (r != -1) {
			count = r + 1;
		}
	}

	for (int i = 0; i < N; ++i) {
		cout << i + 1 << " " << in[i] << " " << out[i] << endl;
	}

	return 0;
}

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

using namespace std;

static const int N = 100;
static const int YET = 0;
static const int ALREADY = 1;

int state[N];
int timeTable[N][2];
int timeCount = 0;
int currentV;
stack<int> st;
int M[N][N];

void travel() {
  while(!st.empty()) {
    currentV = st.top();
    st.pop();

    if(state[currentV] == YET) {
      timeCount++;
      timeTable[currentV][0] = timeCount;
      state[currentV] = ALREADY;
      st.push(currentV);

      for(int i = N; i > -1; i--) {
        if(M[currentV][i] == 1 && state[i] == YET) {
          st.push(i);
        }
      }
    }
    else if(timeTable[currentV][1] == 0){
      timeCount++;
      timeTable[currentV][1] = timeCount;
    }
  }
}
int main(void) {


  int n, u, k, v;

  cin >> n;

  //?????????
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

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

  st.push(0);
  travel();
  for(int i = 0; i < N; i++) {
    if(state[i] == YET) {
      st.push(i);
      travel();
    }
  }


  for(int i = 0; i < n; i++) {
    cout << i + 1 << " " << timeTable[i][0] << " " << timeTable[i][1] << endl;
  }
  return 0;
}

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

static const int MAX_NUM = 100;

void dsf_cal(int matrix[][MAX_NUM + 1], int state[], int time[][2], int index, int num)
{
    static int t = 0;
    stack<int> S;

    S.push(index);
    state[index]   = 1;
    time[index][0] = t;
    ++t;

    while (S.empty() != true) {
        int i;
        int target = S.top();
        for (i = 0; i < num; i++) {
            if (state[i] == 0) {
                S.push(i);
                state[i]   = 1;
                time[i][0] = t;
                ++t;
                break;
            }
        }
        if (i == num) {
            time[target][1] = t;
            ++t;
            S.pop();
        }
    }

    return;
}

void DSF(int matrix[][MAX_NUM + 1], int num)
{
    int state[MAX_NUM]   = {0};
    int time[MAX_NUM][2] = {0};

    for (int i = 0; i < num; i++) {
        if (state[i] == 0) {
            dsf_cal(matrix, state, time, i, num);
        }
    }

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

    return;
}

int main(void)
{
    int num;
    int index, elemNum;
    int matrix[MAX_NUM + 1][MAX_NUM + 1] = {0};

    scanf("%d", &num);
    for (int i = 0; i < num; i++) {
        scanf("%d %d", &index, &elemNum);
        for (int j = 0; j < elemNum; j++) {
            scanf("%d", matrix[i+1][j]);
        }
    }

    DSF(matrix, num);

    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++)
using namespace std;
int ms[100][100],n,tt=0;
int color[101],nt[101];
int d[100],f[100];
int next(int v){
    for(int i=nt[v];i<n;i++){
        nt[v]=i+1;
        if(ms[v][i]==1)return i;
    }
    return -1;
}

void visit_dfs(int v){
    stack<int> s;
    s.push(v);
    d[v]=++tt;
    color[v]=1;
    while(!s.empty()){
        int u=s.top();
        int uu=next(u);
        if(uu!=-1){
            if(color[uu]==0){
            s.push(uu);
            d[uu]=++tt;
            color[uu]=1;
            }
        }else{
            s.pop();
            f[u]=++tt;
            color[u]=2;
        }
    }
    
    
}
void dfs(){
    for(int i=0;i<n;i++){
        color[i]=0;
        nt[i]=0;
    }
    for(int i=0;i<n;i++){
        if(color[i]==0){
            visit_dfs(i);
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
    }
}

int main(){
    
    cin>>n;
    rep(i,n){
        int a,b,c;
        cin>>a>>b;
        a--;
        rep(j,b){
            cin>>c;
            c--;
            ms[a][c]=1;
        }
    }
    
    dfs();
    return 0;

    
}