Go: package main

/*
深さ優先探索
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		ints := eGetInts(scanner)
		num, vcount := ints[0], ints[1]
		g[i] = make([]int, vcount)
		for j := 0; j < vcount; j++ {
			g[num][j] = ints[j+2]
		}
	}
	hist := make([]struct{ d, f int }, n+1)
	t := 0
	var visit func(int)
	visit = func(num int) {
		if hist[num].d == 0 {
			stack := []int{}
			t++
			hist[num].d = t
			stack = append(stack, num)
			for len(stack) > 0 {
				// トップを訪問する
				top := stack[len(stack)-1]
				for i := 0; i < len(g[top]); i++ {
					if hist[g[top][i]].d == 0 {
						t++
						hist[g[top][i]].d = t
						stack = append(stack, g[top][i])
						goto endloop
					}
				}
				// 訪問場所がなくなったら
				// pop
				stack = stack[0 : len(stack)-1]
				t++
				hist[top].f = t
			endloop:
			}
		}
	}
	for i := 1; i < n+1; i++ {
		visit(i)
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, hist[i].d, hist[i].f)
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

Python: #!/usr/bin/env python3
import sys
sys.setrecursionlimit(15000)
n = int(sys.stdin.readline())

ret = []
inp = sys.stdin.readlines()
G = [None]*(n)
d = [0]*(n)
f = [0]*(n)
def dfs(i,t):
  if d[i] != 0:
    return t
  t = t+1
  d[i] = t
  for j in G[i]:
    t = dfs(j,t)
  f[i] = t+1
  return f[i]
for i in range(n):
  v = list(map(int,inp[i].split()))
  G[v[0]-1] = list(map(lambda x: int(x-1),v[2:]))
#print(G)
t = 0
for j in range(n):
  t = dfs(j,t)

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

Python: N = int(input())
UKV = [list(map(int,input().split())) for _ in range(N)]

seen = [False]*N
first_time = [-1]*N
last_time = [-1]*N
time = 0
def dfs(G, v):
    global seen
    global first_time
    global last_time
    global time
    seen[v-1] = True
    time += 1
    first_time[v-1] = time
    for next_v in G[v]:
        if seen[next_v-1]:
            continue
        dfs(G, next_v)
    time += 1
    last_time[v-1] = time

G = {}
for ukv in UKV:
    G[ukv[0]] = sorted(ukv[2:])
for i in range(1,N+1):
    if seen[i-1]:
        continue
    dfs(G, i)

for i in range(1,N+1):
    print(i,first_time[i-1],last_time[i-1])

Python: G = {}
for _ in range(int(input())):
  x = list(map(int, input().split()))
  G[x[0]] = sorted(x[2:])

t = 1
stack = [1]
timestamp = {1:[t]}

while len(stack) > 0:
  t += 1
  v = stack[-1]

  while len(G[v]) > 0:
    v = G[v].pop(0)
    if v in timestamp: continue
    stack.append(v)
    timestamp[v] = [t]
    break
  else:
    v = stack.pop()
    timestamp[v].append(t)

for i in timestamp:
  print(i, *timestamp[i])

Python: import sys

time = 0
def dfs(u):
    global time
    flag_list[u] = 1
    time += 1
    d_list[u] = time
    for v in range(n):
        if m_list[u][v] == 1 and flag_list[v] == 0:
            dfs(v)
    flag_list[u] = 2
    time += 1
    f_list[u] = time


input = sys.stdin.readline
n = int(input())
m_list = [[0]*n for _ in range(n)]
for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0] - 1
    if input_list[1] > 0:
        for a in input_list[2:]:
            m_list[index][a-1] = 1
    
d_list = [0]*n
f_list = [0]*n
flag_list = [0]*n # 0: 未到達, 1: 処理中, 2: 完了
for i in range(n):
    if flag_list[i] == 0:
        dfs(i)

for i in range(n):
    print(i+1, d_list[i], f_list[i])

Python: G = {}
N = int(input())
for _ in range(N):
    L = list(map(int, input().split()))
    G[L[0]] = L[2:]

# print(G)
d = [-1] * N
f = [-1] * N
q = [1]
visited = [False] * N

time = 1


def dfs(v):
    global time
    visited[v-1] = True

    for nv in G[v]:
        if d[nv-1] < 0:
            d[nv-1] = time
            time += 1
        if not visited[nv-1]:
            dfs(nv)

    f[v-1] = time
    time += 1


for i in range(1, N+1):
    if not visited[i-1]:
        d[i-1] = time
        time += 1
        dfs(i)

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

Python: import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [[] for _ in range(n)]

edge = []
for d in data:
    edge.append(d[2:])


def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt

tmp = 0
for i in range(n):
    tmp = dfs(i, tmp)

for i in range(n):
    print(i + 1, *ans[i], sep=" ")

Python: class Node:
    def __init__(self, data):
        self.data = data
        self.neighbors = list()
        self.find_time = 0
        self.end_time = 0

N = int(input())
work_id = 0
nodes = list()
nodes.append(Node(0))


for _ in range(N):
    tmp_list = list( map(int, input().split()) )
    node = Node(tmp_list[0])
    for i in range(tmp_list[1]):
        node.neighbors.append(tmp_list[i+2])
    nodes.append(node)
    nodes[0].neighbors.append(node.data)

# for node in nodes:
#     print(node.data, ":", node.neighbors)


def search(node):
    global work_id

    if node.find_time != 0:
        return
    
    if node.data != 0:
        work_id += 1
        node.find_time = work_id

    if len(node.neighbors) > 0:
        for neighbor in node.neighbors:
            search(nodes[neighbor])
    
    if node.data != 0:
        work_id += 1
        node.end_time = work_id

search(nodes[0])

for node in nodes:
    if node.data == 0: continue
    print(node.data, node.find_time, node.end_time)