Go: package main

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

/***************************************************************
stackによる深さ優先探索を実装する。
***************************************************************/

const (
	N     = 100
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

var (
	n            int
	Adj          [N][N]int
	statusColor  [N]int
	discoverTime [N]int
	finishTime   [N]int
	clock        int
	nt           [N]int
)

/***************************************************************
Stackの定義:
- field
	container: ノードの格納されたスライス
	sp: スタックポインタ
- method
	size(): スタックに積まれているノードの個数を返す
	top(): スタックの一番上に積まれているノードを返す
	isEmpty(): スタックが空かどうかを返す
	pop(): スタックの要素をポップし削除する
	push(): スタックに要素を積む
***************************************************************/

type Stack struct {
	container []int
	sp        int
}

func (s *Stack) size() int {
	return len(s.container)
}

func (s *Stack) top() int {
	return s.container[s.sp]
}

func (s *Stack) isEmpty() bool {
	return s.sp == 0
}

func (s *Stack) pop() int {
	_pop := func() {
		s.container = s.container[:len(s.container)-1]
	}
	defer _pop()
	if s.isEmpty() {
		panic("There is no elements in stack!")
	}
	s.sp--
	return s.container[s.sp]
}

func (s *Stack) push(n int) {
	s.sp++
	s.container[s.sp] = n
}

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

func _DFS(target int) {
	for i := 0; i < n; i++ {
		nt[i] = 0
	}
	stack := new(Stack)
	stack.container = make([]int, N+1)
	stack.push(target)
	statusColor[target] = GRAY
	clock++
	discoverTime[target] = clock
	for !stack.isEmpty() {
		u := stack.top()
		v := next(u)
		if v != -1 {
			if statusColor[v] == WHITE {
				statusColor[v] = GRAY
				clock++
				discoverTime[v] = clock
				stack.push(v)
			}
		} else {
			stack.pop()
			statusColor[u] = BLACK
			clock++
			finishTime[u] = clock
		}
	}
}

func DFS() {
	for i := 0; i < n; i++ {
		statusColor[i] = WHITE
		nt[i] = 0
	}
	clock = 0
	for i := 0; i < n; i++ {
		if statusColor[i] == WHITE {
			_DFS(i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, discoverTime[i], finishTime[i])
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		u--
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ := strconv.Atoi(scanner.Text())
			v--
			Adj[u][v] = 1
		}
	}

	DFS()
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100;

vector<int> G[N];
int visited[N], checked[N];

int find_no_visited(int n) {
	int ret = -1;
	rep(i, n) {
		if (visited[i]) continue;
		ret = i;
		break;
	}
	return ret;
}

int find_no_visited(int n, int i) {
	int ret = -1;
	for (auto j : G[i]) {
		if (visited[j]) continue;
		ret = j;
		break;
	}
	return ret;
}

int main() {
	int n; cin >> n;
	rep(i, n) {
		int _, n2; cin >> _ >> n2;
		rep(j, n2) {
			int k; cin >> k; k--;
			G[i].push_back(k);
		}
	}
	rep(i, n) { visited[i] = 0; checked[i] = 0;}

	stack<int> S;
	int T = 1;
	S.push(0);
	visited[0] = T;
	while (true) {
		T++;
		if (S.empty()) {
			int i = find_no_visited(n);
			if (i != -1) {
				visited[i] = T;
				S.push(i);
				continue;
			}
			else
				break;
		}
		int i = find_no_visited(n, S.top());
		if (i != -1) {
			visited[i] = T;
			S.push(i);
		}
		else {
			checked[S.top()] = T;
			S.pop();
		}
	}
	rep(i, n) cout << i+1 << ' ' << visited[i] << ' ' << checked[i] << endl;
}

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

typedef long long ll;

ll d[100], f[100], now, cnt;
char flag[100];
vector<int> vec[100];

int dfs(int n) {
  if(f[n]) return 0;
  cnt++;
  d[n] = cnt;
  vector<int>::iterator it;
  for(it = vec[n].begin(); it != vec[n].end(); ++it) {
    if(!f[*it]) dfs(*it);
  }
  ++cnt;
  f[n] = cnt;
  return 0;
}

int main(void) {
  ll n;
  scanf("%d", &n);
  now = 0, cnt = 0;
  rep(i, n) flag[i] = 0;
  rep(i, n) {
    int u, k;
    scanf("%d%d", &u, &k);
    u--;
    rep(j, k) {
      int v;
      scanf("%d", &v);
      v--;
      vec[u].push_back(v);
    }
    sort(vec[u].begin(), vec[u].end());
  }
  dfs(0);
  rep(i, n) printf("%d %lld %lld\n", i + 1, d[i], f[i]);
  return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
static const int N=100,WHITE=0,GRAY=1,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++) {
    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<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
using namespace std;

const int N = 100;
const int WHITE = 0;
const int GRAY = 1;
const int 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] = 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++) {
        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++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>

bool table[102][102] = {};
int color[102] = {}; //0:?????¢?´¢???1:?¨??????????, 2:??¢?´¢??????
int n, counter = 0;
int t_visit[102] = {};
int t_finish[102] = {};

void DFS(int y) {
	
	for (int j = 0; j <= n - 1;j++) {

		//?¨????or??¢?´¢???????????£???????????????
		if ((table[y][j] == 1)&&(color[j] == 0)) {
			color[y] = 1; //?¨???????????????????
			if (t_visit[y] == 0) t_visit[y] = ++counter;
			DFS(j);
		}

	}

	color[y] = 2; //??¢?´¢???????????????
	if (t_visit[y] == 0) t_visit[y] = ++counter;
	if (t_finish[y] == 0) t_finish[y] = ++counter;
	
}

int main(void) {
	int u, k, s;

	scanf("%d", &n);

	for (int i = 0;i <= n - 1;i++) {
		scanf("%d %d", &u, &k);
		for (int j = 0;j <= k - 1;j++) {
			scanf("%d", &s);
			table[u - 1][s - 1] = 1;
		}
	}

	DFS(0);

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

	return 0;
}