Go: package main

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

type TimeRecord struct {
	discoverTime int
	finishTime   int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	timeRecordMap := make([]TimeRecord, n)
	childrenMap := make([][]int, n)
	for i := 1; i <= n; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		timeRecordMap[node-1] = TimeRecord{-1, -1}

		childNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		childrenMap[node-1] = make([]int, childNum)
		for j := 0; j < childNum; j++ {
			child, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			childrenMap[node-1][j] = child
		}
	}

	timer := 1
	startNode := 1
	for {
		visit(startNode, &timer, childrenMap, timeRecordMap)
		startNode = findStartNode(childrenMap, timeRecordMap)
		if startNode < 0 {
			break
		}
	}
	for k, v := range timeRecordMap {
		fmt.Printf("%d %d %d\n", k+1, v.discoverTime, v.finishTime)
	}
}

func findStartNode(childrenMap [][]int, timeRecordMap []TimeRecord) int {
	x := -1
	for node, timeRecord := range timeRecordMap {
		if timeRecord.discoverTime < 0 {
			x = node + 1
			break
		}
	}
	return x
}

func visit(currentNode int, timer *int, childrenMap [][]int, timeRecordMap []TimeRecord) {
	if timeRecordMap[currentNode-1].discoverTime > 0 {
		return
	}
	children := childrenMap[currentNode-1]
	timeRecordMap[currentNode-1] = TimeRecord{*timer, timeRecordMap[currentNode-1].finishTime}
	(*timer)++
	for i := 0; i < len(children); i++ {
		visit(children[i], timer, childrenMap, timeRecordMap)
	}
	timeRecordMap[currentNode-1] = TimeRecord{timeRecordMap[currentNode-1].discoverTime, *timer}
	(*timer)++
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

Python: cin = open(0).read().strip().split('\n')
n = int(cin[0])
g = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]]

first_order = [-1] * n
last_order = [-1] * n
ptr = 1

def dfs(g, seen, idx):
    global first_order, first_order, ptr

    first_order[idx] = ptr
    ptr += 1

    seen[idx] = True
    for i in g[idx]:
        # idxから行ける各頂点について
        if seen[i]: continue
        dfs(g, seen, i)

    last_order[idx] = ptr
    ptr += 1

seen = [False] * n
dfs(g, seen, 0)

# 孤立している頂点対策
for i in range(n):
    if not seen[i]:
        ans = dfs(g, seen, i)

# 出力
for idx, (f, l) in enumerate(zip(first_order, last_order)):
    print(idx+1, f, l)

Python: global t
t = 0

class Node:
    def __init__(self, num, deg, neib):
        self.num = num
        self.deg = deg
        self.neib = neib

def dfs(G, d, f, now): #now:1,2,...
    print("now:",now)
    global t
    t += 1
    print("t:",t)
    if f[now-1] != 0: t -= 1
    else:
        d[now-1] = t
        print("d:",d)
        print("f:",f)
        for i in G[now-1].neib:
            print("i:",i)
            if i != -1:
                print("next")
                dfs(G, d, f, i)
            else:
                print("end of branch")
            
        t += 1
        if f[now-1] == 0:
            f[now-1] = t

        print("f:",f)


dnum = int(input()) #頂点数
N = []
for i in range(dnum):
    data = input().split()
    
    u = int(data[0])
    k = int(data[1])
    if k > 0:
        v = list(map(int, data[2:]))
    else:
        v = [-1]

    node = Node(u,k,v)
    N.append(node)

d = [0]*dnum #find time
f = [0]*dnum #finish time

dfs(N, d, f, 1)

for i in range(dnum):
    print(i+1, d[i], f[i])

Python: import sys

WHITE = 0
GRAY = 1
BLACK = 2


def dbg_record(records):
    for index, record in enumerate(records):
        print("???????????????: %d" % index)
        if isinstance(record, dict):
            print("??\??????: %d" % record.get("visit", -1))
            print("?????????: %d" % record.get("leave", -1))
        else:
            print("?????????")
        print("")


def dfs(matrix):
    def internal_dfs(node_id, current_time):
        if state[node_id] == WHITE:
            # ?¨??????????
            state[node_id] = GRAY
            stack.append(node_id)
            records[node_id] = {"visit": current_time}  # ?¨??????????????¨????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                internal_dfs(next_id, current_time + 1)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                internal_dfs(stack.pop(), current_time + 1)
        elif state[node_id] == GRAY:
            # ????????????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                stack.append(node_id)
                # internal_dfs(next_id, current_time + 1)
                internal_dfs(next_id, current_time)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time  # ????????????????¨????
                internal_dfs(stack.pop(), current_time + 1)
            else:  # ??????????§????????????£???????????¨??? (node_id should be 0)
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time
        elif state[node_id] == BLACK:
            print("Black!!")

    state = [WHITE] * len(matrix)  # White, Gray, Black
    records = [None] * len(matrix)  # Save time when the node is processed
    stack = []

    internal_dfs(0, 1)
    # dbg_record(records)
    return records


if __name__ == "__main__":
    # ??????????????????
    # lines = [
    #     "6",
    #     "1 2 2 3",
    #     "2 2 3 4",
    #     "3 1 5",
    #     "4 1 6",
    #     "5 1 6",
    #     "6 0",
    # ]
    lines = sys.stdin.readlines()

    # Create n * n matrix
    dimension = int(lines[0])
    matrix = []
    for x in range(dimension):
        matrix.append([0] * dimension)

    # Set edge info
    for index, line in enumerate(lines[1:]):
        for edge in [int(x) for x in line.strip().split(" ")[2:]]:
            matrix[index][edge - 1] = 1

    result = dfs(matrix)
    for index, line in enumerate(result):
        print("%d %d %d" % (index + 1, line.get("visit", -1), line.get("leave", -1)))

Python: #!/usr/bin/env python3

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float('inf')

n_nodes = int(input())
graph = [[] for _ in range(n_nodes)]

for _ in range(n_nodes):
    line = list(map(int, input().split()))
    u, k = line[0], line[1]
    if k > 0:
        for v in line[2:]:
            graph[u - 1].append(v - 1)
# pprint(graph)


def dfs(v):
    global time
    time += 1
    for v_adj in graph[v]:
        if d[v_adj] == -1:
            d[v_adj] = time
            dfs(v_adj)
    f[v] = time
    time += 1


d = [-1] * n_nodes
f = [-1] * n_nodes
time = 1

for v in range(n_nodes):
    if d[v] == -1:
        d[v] = time
        dfs(v)

# pprint(d)
# pprint(f)

for v in range(n_nodes):
    print(f"{v + 1} {d[v]} {f[v]}")