Go: package main

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

// repr state of visited node
// WHITE yet visit
// GEAY  visiting
// BLACK visited
const (
	WHITE int = iota
	GRAY
	BLACK
)

type dfsGraph struct {
	g      [][]int
	colors []int
	stime  []int // Start time of visiting node
	ftime  []int // Finish time of visiting node
}

var time int

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func (dg dfsGraph) dfsVisit(u int) {
	// 頂点uを訪問中に設定
	dg.colors[u] = GRAY
	time++
	dg.stime[u] = time

	// uの隣接nodeを訪問
	for v := 0; v < len(dg.g); v++ {
		if dg.g[u][v] == 0 {
			continue // 辺がなければスキップ
		}

		// vが未訪問なら訪問
		if dg.colors[v] == WHITE {
			dg.dfsVisit(v)
		}
	}

	// 頂点uを訪問済みに設定
	dg.colors[u] = BLACK
	time++
	dg.ftime[u] = time
}

func (dg dfsGraph) dfs() {
	for i := 0; i < len(dg.g); i++ {
		if dg.colors[i] == WHITE {
			dg.dfsVisit(i)
		}
	}
}

func (dg dfsGraph) printDfsTime() {
	for i := 0; i < len(dg.g); i++ {
		fmt.Printf("%d %d %d\n", i+1, dg.stime[i], dg.ftime[i])
	}
}

func main() {
	adj := makeAdjList()
	colors := make([]int, len(adj))
	stime := make([]int, len(adj))
	ftime := make([]int, len(adj))
	graph := dfsGraph{adj, colors, stime, ftime}
	graph.dfs()
	graph.printDfsTime()
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
using Graph = vector<vector<int>>;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数

//-------------------------------------------------
// 
void yes(){
  cout <<"Yes"<<endl ;
}
void no(){
  cout <<"No"<<endl ;
}


//-------------------------------------------------
// メモ
/*






*/
//-------------------------------------------------


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n ;
  cin >> n ;
  int time = 1 ;
  vint d(n,0) ;
  vint f(n,0) ;
  vint seen(n,0) ;
  vint chec(n,0) ;
  vector<queue<int>> gra(n) ;
  REP(i,n){
    int a, b ;
    cin >> a >> b ;
    REP(j,b){
      int c ;
      cin >> c ;
      gra[a-1].push(c-1) ;
    }
    gra[a-1].push(-1) ;
  }
  
  stack<int> S ;
  
  S.push(0) ;
  
  while(!S.empty()){
    int k = S.top() ;
    if(seen[k]==0){
      seen[k] = 1 ;
      d[k] = time ;
    }
    if(gra[k].front()!=-1){
      if(seen[gra[k].front()]==0){
        int l = gra[k].front() ;
        S.push(l) ;
        gra[k].pop() ;
        time++ ;
      }
      else{
        gra[k].pop() ;
        continue ;
      }
    }
    else{
      if(chec[k]==0){
        time++ ;
        chec[k] = 1 ;
        f[k] = time ;
      }
      S.pop() ;
    }
    if(S.empty()){
      REP(i,n){
        if(seen[i]==0){
          S.push(i) ;
          time++ ;
          break ;
        }
      }
    }
  }
  
  
  REP(i,n){
    cout << i+1 <<" "<<d[i] <<" " <<f[i] <<endl ;
  }
      
  return 0 ;
}

C++: #include <bits/stdc++.h>
#define rep(i,s,n) for(int i=s;i<n;i++)
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
using P=pair<int,int>;
using G=vector<vector<int>>;
using ll=long long;

string subsum(int* A,int n,int m){
  bitset<2010> E(1);

  rep(i,0,n){
    E |= (E<<A[i]);
  }

  if(E.test(m)) return "yes";
  else return "no";
}

vector<bool> seen;
vector<int> d;
vector<int> f;


int dfs(const G &G,int v,int& time){
    seen[v] = true; //v探索済み
    d[v]=time++;

    for(auto next_v :G[v]){
        if(seen[next_v])continue;//もう探索していた
        dfs(G,next_v,time);
    }

    f[v]=time++;
    return time;
}

int main(int argc,char **argv){
  int n; cin>>n;
  G graph(n);
  rep(i,0,n){
    int u,k; cin>>u>>k;
    rep(j,0,k){
      int vj;
      cin>>vj;
      if(i!=vj-1)graph.at(u-1).pb(vj-1);
      //graph.at(vj-1).pb(i);
    }
  }

  d.assign(n,0);
  f.assign(n,0);
  int first_ptr=1;
  seen.assign(n,false);

  rep(i,0,n){
    if(seen[i]==false){
      first_ptr=dfs(graph,i,first_ptr);
    }
  }

  rep(i,0,n){
    cout<<i+1<<" "<<d[i]<<" "<<f[i]<<endl;
  }



  
  }

C++: #include <stdio.h>

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

int serch(int a[][], 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 judge(int i, int j){
  ans[i][j] = an;
  an++;
}

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;
    }
  }
  
  judge(0, 1);
  
  for(i=0; i<n; i++){
    if(A[0][i] == 1){
      judge(i, 1);
      serch(A, i, n);
    }
  }
  judge(0, 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>
using namespace std;

int N, W;
int v[100], w[100];
int DP[10001];

int main(void) {
  cin >> N >> W;
  for (int i=0; i<N; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i=0; i<N; i++) {
    for (int j=w[i]; j<=W; j++) {
      DP[j] = max(DP[j], DP[j-w[i]]+v[i]);
    }
  }

  cout << DP[W] << endl;

  return 0;
}

C++: def dfs(v, cnt):
    D[v] = cnt
    cnt += 1
    for c in edge[v]:
        if D[c] == -1:
            cnt = dfs(c, cnt)
    F[v] = cnt
    cnt += 1
    return cnt


V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v)
D, F = [-1] * V, [-1] * V
c = 1
for i in range(V):
    if D[i] == -1:
        c = dfs(i, c)
for i, (d, f) in enumerate(zip(D, F)):
    print(i, d, f)

C++: #include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > v(101);
int dp[101][2] = { 0 };
int n, t = 0;

void dfs(int a) {
	t++;
	if (dp[a][0] == 0) { dp[a][0] = t; }
	for (int i = 0;i < v[a].size();i++) {
		if (dp[v[a][i]][0] == 0) {
			dfs(v[a][i]);
		}
	}
	t++;
	if (dp[a][1] == 0) { dp[a][1] = t; }
}

int main() {
	cin >> n;
	for (int i = 0; i < n;i++) {
		int u,k; cin >> u >> k;
		for (int j = 0; j < k;j++) {
			int x; cin >> x;
			v[u].push_back(x);
		}
	}
	dfs(1);
	for (int i = 1; i <= n;i++) {
		cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl;
	}
}