Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 101

var f [MAX]int
var d [MAX]int
var list [][]int
var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(u, cnt int) int {
	if d[u] != 0 {
		return cnt
	}

	cnt++
	d[u] = cnt
	for _, next := range list[u] {
		cnt = dfs(next, cnt)
	}
	cnt++
	f[u] = cnt
	return cnt
}

func main() {
	n := scanInt()
	list = make([][]int, n+1)
	for i := 1; i <= n; i++ {
		u := scanInt()
		k := scanInt()
		for j := 0; j < k; j++ {
			n := scanInt()
			list[u] = append(list[u], n)
		}
	}

	cnt := 0
	for i := 1; i <= n; i++ {
		cnt = dfs(i, cnt)
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= n; i++ {
		fmt.Fprint(w, i)
		fmt.Fprint(w, " ")
		fmt.Fprint(w, d[i])
		fmt.Fprint(w, " ")
		fmt.Fprint(w, f[i])
		fmt.Fprintln(w)
	}
	w.Flush()
}

C++: #include <iostream>
#include <stack>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GREY = 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] = GREY;
	d[r] = ++tt;

	while (!S.empty()) {
		int u = S.top();
		int v = next(u);
		if (v != -1) {
			if (color[v] == WHITE) {
				color[v] = GREY;
				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 >> u;
	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 <stack>

using namespace std;

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

int main(void) {
  int M[N][N];
  int state[N];
  int timeTable[N][2];

  stack<int> st;

  int timeCount = 0;
  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);
  int currentV;
  while(!st.empty()) {
    timeCount++;
    currentV = st.top();
    st.pop();

    if(state[currentV] == YET) {
      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){
      timeTable[currentV][1] = timeCount;
    }
    else {
      timeCount--;
    }
  }


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

C++: #include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

vector<int>E[100];
int s[100], g[100];
bool used[100];
int t;

void dfs(int u) {
	used[u] = true;
	s[u] = ++t;
	for (int v : E[u]) {
		if (!used[v])dfs(v);
	}
	g[u] = ++t;
}
int main() {
	int n; scanf("%d", &n);
	rep(i, n) {
		int u, k; scanf("%d%d", &u, &k); u--;
		rep(j, k) {
			int v; scanf("%d", &v); v--;
			E[u].push_back(v);
		}
	}
	rep(i, n) {
		if (!used[i])dfs(i);
	}
	rep(i, n) {
		printf("%d %d %d\n", i + 1, s[i], g[i]);
	}
}

C++: #if 0
以前の反省を踏まえて、idの扱いを早いうちに決めてよかった
#endif

#include <iostream>
#include <vector>
using namespace std;

int curtime = 0, INF = 10000 * 100000 + 100;
struct result
{
    int reach_time;
    int back_time;
};
result res[110];
struct nodeData
{
    int id;
    vector<int> nodes;
};
vector<nodeData> dataSet;

// idはは1から
// for文は1から回す
vector<int> getNodes(int id)
{
    return dataSet[id - 1].nodes;
}

void dfs(int cur)
{
    // curの訪問時刻を記録
    curtime += 1;
    res[cur].reach_time = curtime;
    vector<int> nodes = getNodes(cur);
    for (int i = 0; i < nodes.size(); i++)
    {
        int node = nodes[i];
        if (res[node].reach_time == INF)
        {
            // 未訪問の場合
            dfs(node);
        }
    }
    curtime += 1;
    res[cur].back_time = curtime;
}

int main()
{
    // 初期化
    int /* 頂点数 */ n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        nodeData temp;
        temp.id = i;
        int id, /* 出次数 */ appear;
        cin >> id >> appear;
        for (int j = 1; j <= appear; j++)
        {
            int node;
            cin >> node;
            temp.nodes.push_back(node);
        }
        dataSet.push_back(temp);
        res[i].reach_time = INF;
    }

    // 処理
    for (int i = 1; i <= n; i++)
    {
        if (res[i].reach_time == INF)
        {
            dfs(i);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << i << " " << res[i].reach_time << " " << res[i].back_time << endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#define MAX_N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n, u, k, v;
int Adj[MAX_N][MAX_N];
int d[MAX_N], f[MAX_N], color[MAX_N];
int tt;

void initialize(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			Adj[i][j] = 0;
		}
	}
}

void check_adj(){
	Adj[u][v] = 1;
}

void dfs_visit(int u){
	color[u] = GRAY;
	d[u] = ++tt;
	for(int v = 0; v < n; v++){
		if(Adj[u][v] == 0) continue;
		if(color[v] == WHITE){
			dfs_visit(v);
		}
	}
	color[u] = BLACK;
	f[u] = ++tt;
}

void dfs(){
	tt = 0;
	for(int i = 0; i < n; i++) color[i] = WHITE;
	for(int i = 0; i < n; i++){
		if(color[i] == WHITE) dfs_visit(i);
	}
	for(int i = 0; i < n; i++){
		printf("%d %d %d\n", i + 1, d[i], f[i]);
	}
}

/* void print_adj(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			cout << Adj[i][j];
			if(j == n - 1) cout << endl;
			else cout << " ";
		}
	}
} */

int main(void){
	cin >> n;
	initialize();
	for(int i = 0; i < n; i++){
		cin >> u;
		u--;
		cin >> k;
		for(int j = 0; j < k; j++){
			cin >> v;
			v--;
			check_adj();
		}
	}
	dfs();
	//print_adj();

	return 0;
}