Go: package main

import "fmt"

type node struct {
	id, d, v int
	adj      []node
}

var n int
var seen []bool
var graph []node

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func dfs(v, t int) int {
	// fmt.Printf("dfs(v %d, t %d) en:\n", v, t)
	seen[v] = true
	graph[v].v = t
	tt := t
	for _, vv := range graph[v].adj {
		if !seen[vv.id] {
			tt = max(tt, dfs(vv.id, tt+1))
		}
	}
	graph[v].d = tt + 1
	// fmt.Printf("dfs(v %d, t %d) ex: d %d\n", v, t, graph[v].d)
	return graph[v].d
}

func main() {
	fmt.Scan(&n)
	graph = make([]node, n+1)
	for i := 1; i <= n; i++ {
		var u, k, v int
		fmt.Scan(&u, &k)
		graph[u].id = u
		// 頂点uはk個の隣接頂点を持っている
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			var t node
			t.id = v
			graph[u].adj = append(graph[u].adj, t)
		}
	}
	// fmt.Println("n", n, "graph", graph)
	seen = make([]bool, n+1)
	// t := dfs(1, 1)
	v, t := 1, 1
	done := false
	for !done {
		tt := dfs(v, t)
		done = true
		for i := range seen {
			if !seen[i] {
				done = false
				v = graph[i].id
				t = tt
				break
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, graph[i].v, graph[i].d)
	}
}

Python: j = 1
def dfs(graph, start, result, visited=None):
    global j
    if visited == None:
        visited = []
    visited.append(start)
    result[start][1]  = j
    for next in graph[start][2:]:
        if next in visited:
            continue
        j += 1
        dfs(graph, next, result, visited)
    else:
        j += 1
        result[start][2] = j

n = int(input())
g = [[]]
r = [[i,0,0] for i in range(n + 1)]

for i in range(n):
    g.append(list(map(int, input().split())))
dfs(g,1,r)
for i in range(1,n+1):
    print(*r[i])

Python: N = int(input())
M = []
for i in range(N):
    a = list(map(int,input().strip().split()))
    b = [int(i+1 in a[2:]) for i in range(N)]
    M.append(b)

time = 1
D = [-1 for _ in range(N)]
F = [-1 for _ in range(N)]
def dfs(src):
    global time # 関数内でglobal変数の書き換えることを宣言
    D[src] = time # 訪問時刻を記録
    time += 1
    for dst in range(N):
        # srcからdstに移動可能で、dstが未訪問だったら
        if M[src][dst] == 1 and D[dst] == -1:
            dfs(dst)
    F[src] = time # 訪問終了時刻を記録
    time += 1
    # 関数の終わりで親に戻る(青矢印)

for root in range(N):
    # 番号が若い節点から
    if D[root] == -1:
        # D[root]が未訪問だったら
        dfs(root) # dfsを始める

[print(i+1, D[i], F[i]) for i in range(N)]

Python: n = int(input())
graph = [[] for _ in range(n)]
for i in range(n):
    in_list = list(map(int, input().split()))
    u = in_list[0]
    graph[u - 1] = in_list[2:]

d = [0 for _ in range(n)]
f = [0 for _ in range(n)]
time = 0

def dfs(now, prev, graph, d, f, time):
    if d[now] != 0:
        return d, f, time
    time += 1
    d[now] = time
    if not graph[now]:
        f[now] = time + 1
        time += 1
        return d, f, time
    for k in graph[now]:
        if k - 1 == prev or d[k-1] !=0:
            continue
        _, _, time = dfs(k - 1, now, graph, d, f, time)
    else:
        time += 1
        f[now] = time
    return d, f, time



for i in range(n):
    d, f, time = dfs(i, 0, graph, d, f, time)
    print(i+1, d[i], f[i])

Python: n = int(input())

A = [[] for i in range(n)]

for i in range(n):
  A[i] = list(map(int, input().split()))

B = [[0 for i in range(2)] for j in range(n)]
order = []

time = 0

def dfs(n, num):
  global time
  while time < 2*n:
    a = num
    time += 1
    if B[num-1][0] == 0: # まだ発見していないとき
      if A[num-1][1] != 0: # 次の頂点があるとき
        order.append(A[num-1][0]) # 発見したことを記録する
        B[num-1][0] = time # 発見時刻を記録する
        A[num-1][1] -= 1 # 次の頂点数を１減らす
        if B[A[num-1][2]-1][0] == 0: # 次の頂点がまだ発見されていないとき
          num = A[num-1].pop(2) # 次の頂点に移動
        else: # 次の頂点が発見済みのとき
          del A[num-1][2] # 次の頂点をルートから削除
      else: # 次の頂点がないとき
        l = len(order) # これまで経由した頂点数
        order.append(A[num-1][0]) # 発見したことを記録する
        order.append(A[num-1][0]) # 調べ終えたことを記録する
        B[num-1][0] = time # 発見時刻を記録する
        time += 1 # 時刻を１進める
        B[num-1][1] = time # 調査終了時刻を記録する
        num = order[l-1] # １つ前の頂点に戻る
    elif  B[num-1][1] == 0: # 発見しているが、調べ終えていないとき
      if A[num-1][1] == 0: # 次の頂点がないとき
        order.append(A[num-1][0]) # 調べ終えたことを記録する
        B[num-1][1] = time # 調査終了時刻を記録する
        m = order.index(num) # 発見した順番を調べる
        num = order[m-1] # 発見する前の頂点に移動
      else: # 次の頂点があるとき
        A[num-1][1] -= 1 # 次の頂点数を１減らす
        order.append(A[num-1][0]) # 経由したことを記録する
        if B[A[num-1][2]-1][0] == 0: # 次の頂点がまだ発見されていないとき
          num = A[num-1].pop(2) # 次の頂点に移動
        else: # 次の頂点が発見済みのとき
          del A[num-1][2] # つぎの頂点をルートから削除
        time -= 1 # 時刻を戻す
    else: # すでに調査終了した頂点に移動したとき
      l = len(order) # これまで経由した頂点数
      num = order[l-1] # １つ前の頂点に戻る
      time -= 1 # 時刻を戻す
      if num == a:
        break

for i in range(n):
  if B[i][0] == 0:
    dfs(n, A[i][0])

for i in range(n):
  print(str(i+1) + " ", end="")
  print(" ".join(map(str, B[i])))

Python: if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    N = 100
    WHITE = 0
    GRAY = 1
    BLACK = 2

    M = [[0]*N for _ in range(N)]
    color = [WHITE]*N  # colorの初期化は最初に行っておく。
    d = [0]*N
    f = [0]*N

    def dfs_visit(u):
        global tt
        color[u] = GRAY
        tt += 1
        d[u] = tt # 最初の訪問
        for v in range(n):
            if M[u][v] == 0:
                continue
            if color[v] == WHITE:
                dfs_visit(v)
        color[u] = BLACK
        tt += 1
        f[u] = tt # 訪問の完了

    def dfs():
        global tt
        tt = 0

        for u in range(n):
            if color[u] == WHITE:
                # 未訪問のuを始点として深さ優先探索
                dfs_visit(u)
        for u in range(n):
            print(str(u+1) + ' ' + str(d[u]) + ' ' + str(f[u]))

    n = int(input())
    for _ in range(n):
        u, k, *vv = input().split()
        u = int(u)-1  # 0オリジンにする
        k = int(k)    # kは使わない
        for v in vv:
            v = int(v)-1
            M[u][v] = 1
    dfs()

Python: def depth(s, time):
    time += 1
    flag[s] = 1
    d[s] = time
    for i in range(1, n+1):
        if G[s][i] == 1:
            if flag[i] == 0:
                depth(i, time)
                time = f[i]
    f[s] = time + 1
 
n = int(raw_input())
G = [[0 for i in range(n+1)] for j in range(n+1)]
d = [0 for i in range(n+1)]
f = [0 for i in range(n+1)]
 
for i in range(n):
    v = map(int, raw_input().split())
    for j in range(v[1]):
        G[v[0]][v[2+j]] = 1
 
flag = [0 for i in range(n+1)]
for i in range(1, n+1):
    if flag[i] == 0:
        depth(i, max(f))
 
for i in range(1, n+1):
    print(str(i) + " " + str(d[i]) + " " + str(f[i]))

Python: def visit(u):
    global visit_color, d, f, t
    color[u] = GRAY
    d[u] += t
    t += 1
    for v in range(n):
        if (M[u][v] == 0):
            continue
        else:
            if (color[v] == WHITE):
                visit(v)
    color[u] = BLACK
    f[u] = t
    t += 1

n = int(input())
M = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    tmp = list(map(int, input().split()))
    for i in range(tmp[1]):
        M[tmp[0]-1][tmp[i+2]-1] = 1

for u in range(n):
    if (color[u] == WHITE):
        visit(u)
for u in range(n):
    print(str(u+1), str(d[u]), str(f[u]), sep = " ")