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()
}

Python: #!/usr/bin/env python3
from enum import Enum


class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}')


def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs()


if __name__ == '__main__':
    main()

Python: n = int(input())
V = [[] for _ in range(n)]
for i in range(n):
    data = list(map(int, input().split(" ")))
    V[i] = data[2:]

answer = [[None, None] for _ in range(n)]

counter = 1


def dfs(v):
    global counter
    if answer[v][0] == None:
        answer[v][0] = counter
        counter += 1
        # ノードの子供の管理
        for e in V[v]:
            dfs(e-1)
        answer[v][1] = counter
        counter += 1


for i in range(len(answer)):
    if answer[i][0] == None:
        dfs(i)

for i in range(len(answer)):
    print(str(i+1) + " " + " ".join(map(str, answer[i])))

Python: def depth_search(i, adj, d, f, t):
    if d[i-1]:
        return t
    d[i-1] = t
    t += 1                                          
    for v in adj[i-1]:
        t = depth_search(v, adj, d, f, t)
    f[i-1] = t                                      
    t += 1
    return t

n = int(input())
adj = [list(map(int, input().split()))[2:] for _ in range(n)] 

d = [0] * n
f = [0] * n

t = 1
for i in range(n):
    if d[i] == 0:
        t = depth_search(i+1, adj, d, f, t)

for i, df in enumerate(zip(d, f)):
    print(i + 1, *df)

Python: def MAIN():
    n = int(input())
    ans = [[i, 0, 0] for i in range((n + 1))]
    G = [[] for _ in range((n + 1))]
    for _ in range(n):
        u, k, *v = map(int, input().split())
        G[u] = v
    cnt = 1
    def dfs(u):
        nonlocal ans
        if ans[u][1] != 0:
            return
        nonlocal cnt
        ans[u][1] = cnt
        cnt += 1
        for v in G[u]:
            dfs(v)
        ans[u][2] = cnt
        cnt += 1
    for i in range(1, n + 1):
        if ans[i][1] == 0:
            dfs(i)
    for i in range(1, n + 1):
        print(*ans[i])
MAIN()

Python: # 深さ優先探索（行きがけ、帰りがけ）
import sys
input = sys.stdin.readline
from collections import deque

# グラフの作成(無向グラフでは#を消す)
N = int(input())
graph = [deque([]) for _ in range(N + 1)]
for _ in range(N):
    u, k, * v = [int(x) for x in input().split()] # uは頂点番号、kは隣接頂点の個数
    v.sort()
    for i in v:
        graph[u].append(i)
        # graph[i].append(u) # 無向グラフ

time = 0
arrive_time = [-1] * (N + 1) # 到着時刻
finish_time = [-1] * (N + 1) # 探索終了時刻

# 深さ優先探索
def dfs(v):
    global time
    time += 1
    stack = [v]
    arrive_time[v] = time
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if arrive_time[w] < 0:
                time += 1
                arrive_time[w] = time
                stack.append(w) 
        else:
            time += 1
            finish_time[v] = time
            stack.pop()          
    return [arrive_time, finish_time]

# 孤立している頂点対策
for i in range(N):
    if arrive_time[i + 1] < 0:
        ans = dfs(i + 1)

# 頂点番号、到着時刻、終了時刻
for j in range(N):
    temp = [j + 1, ans[0][j + 1], ans[1][j + 1]]
    print(* temp)

Python: graph={}
num = int(input())
Adj = [[0 for i in range(num)] for i in range(num)]
for i in range(num):
    l = list(map(int,input().split()))
    graph[str(l[0])]=l[2:]

color = [0] * num
d = [0] * num
f = [0] * num
t = 0
def dfs(u):
    global t
    color[u - 1] = 1
    t += 1
    d[u - 1] = t
    for i in range(1, num+1):
        if Adj[u - 1][i - 1] == 1 and color[i - 1] == 0:
            dfs(i)
    color[u - 1] = 2
    t += 1
    f[u - 1] = t
 
for i in range(1, num+1):
    if color[i-1] == 0:
        dfs(i)
for i in range(num):
    print("{} {} {}".format(i+1,d[i],f[i]))

Python: n = int(input())
G = [[] for _ in range(n)]
u1 = 0
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u

time = 1
u = u1
D = []
out = [[i+1, 0, 0] for i in range(n)]
T = []
while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for d in D:
        if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1

for ni in range(n):
    print(*out[ni])