Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}
	}

	d := make([]int, n+1)
	f := make([]int, n+1)
	dfs(&g, &d, &f, 1, 1)

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, d[i], f[i])
	}
}

func dfs(g *[][]int, d *[]int, f *[]int, id int, t int) int {
	if (*d)[id] > 0 {
		return t
	}
	(*d)[id] = t
	t += 1
	for i := 0; i < len((*g)[id]); i++ {
		t = dfs(g, d, f, (*g)[id][i], t)
	}
	(*f)[id] = t
	return t+1
}

C++: #include<bits/stdc++.h>
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 nt[N], color[N];
int d[N], f[N];
int tt;

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

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

void dfs_init() {
	tt = 0;
	for (int i = 0; i < n; i++)
		color[i] = white;
	
	for (int i = 0; i < n; i++) {
		if (color[i] == white)
			dfs(i);
	}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << d[i] << " " << f[i] << endl;
	}
}

int main() {
	int k, u, 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_init();
	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] > 0) 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 <bits/stdc++.h>
using namespace std;
#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++) {
        // 未訪問のuを始点として深さ優先探索
        if (color[u] == WHITE) dfs_visit(u);
    }
    for (u = 0; u < n; u++) {
        cout << u+1 << " " << d[u] << " " << f[u] << endl;
    }
}

int main() {
    int u, v, k, 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;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int d[100];
int f[100];
int now = 1;
void dfs(vector<vector<int> > v, int i) {
  if (d[i]) return;
  d[i] = now++;
  for (int vv : v[i])
    if (i != vv) dfs(v, vv);
  f[i] = now++;
}

int main() {
  int n;
  cin >> n;

  vector<vector<int> > v(n);
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    vector<int> vv(k);
    rep(j, k) {
      cin >> vv[j];
      vv[j]--;
    }
    v[i] = vv;
  }

  rep(i, n) if (!d[i]) dfs(v, i);
  rep(i, n) { printf("%d %d %d\n", i + 1, d[i], f[i]); }
  return 0;
}

C++: /*
 * ALDS1_11_B.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include<iostream>
#include<vector>
#include<stack>

using namespace std;

int d[101];
int f[101];
int nowTime;

stack<int> st;

void dfsRec(vector<int>* a, int v) {
	if(d[v] > 0) return;
	nowTime++;
	d[v] = nowTime;
	for(int &x:a[v]) {
		dfsRec(a, x);
	}
	nowTime++;
	f[v] = nowTime;
}

void dfsStack(vector<int>* a, int rt) {
	st.push(rt);
	while(!st.empty()) {
		int v = st.top();
		if(d[v] == 0) d[v] = ++nowTime;
		bool found = false;
		for(int &x:a[v]) {
			if(d[x] == 0) {
				st.push(x); found = true; break;
			}
		}
		if(!found) {
			st.pop();
			f[v] = ++nowTime;
		}
	}
}

int main() {
	int n;
	vector<int> adj[101];

	cin >> n;
	for(int i=0; i<n; i++) {
		int v;
		int k;
		cin >> v >> k;
		for(int j=0; j<k; j++) {
			int u;
			cin >> u;
			adj[v].push_back(u);
		}
	}

	for(int i=1; i<=n; i++) {
		if(d[i] ==0) dfsStack(adj, i);
	}

//	for(int i=1; i<=n; i++) {
//		if(d[i] ==0) dfsRec(adj, i);
//	}

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

C++: #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>

int main(){
    int n;
    scanf("%d",&n);
    int a[n+1][n+1];
    int i,j;
    for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;
    for(i=1;i<=n;i++){
        int q,k;
        scanf("%d %d",&q,&k);
        for(j=1;j<=k;j++) {
            int p;
            scanf("%d",&p);
            a[q][p]=1;
        }
    }
    int time=0;
    std::stack<int>S;
    int ans[n+1][3];
    
    
    
    S.push(1);
    time++;
    ans[1][1]=time;
    
    int color[n+1];
    color[1]=1;
    for(i=2;i<=n;i++)color[i]=0;//0??????????¨????
    
    while(S.size()>=1){
        int flag=0;//flag=0??????pop
        for(i=1;i<=n;i++){
            if(i!=S.top()&& a[S.top()][i]==1 &&color[i]==0){
                S.push(i);
                color[i]=1;
                time++;
                ans[i][1]=time;
                flag=1;
                break;
            }
        }
        if(flag==0){
            time++;
            ans[S.top()][2]=time;
            S.pop();
        }
    }
    
    for(i=1;i<=n;i++){
        printf("%d %d %d\n",i,ans[i][1],ans[i][2]);
        
    }
    
    
    return 0;
}