Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve(parseProblem())
}

type graph struct {
	size  int
	edges [][]int
}

func parseProblem() *graph {
	n := scanInt(sc)
	g := &graph{
		size:  n,
		edges: make([][]int, n),
	}
	for i := 0; i < g.size; i++ {
		_ = scanInt(sc)
		k := scanInt(sc)
		g.edges[i] = make([]int, k)
		for j := 0; j < k; j++ {
			g.edges[i][j] = scanInt(sc) - 1
		}
	}
	return g
}

func resolve(g *graph) {
	ds := make([]int, g.size)
	fs := make([]int, g.size)
	time := 0
	for i := 0; i < len(ds); i++ {
		if ds[i] == 0 {
			time++
			time = _dfs(ds, fs, time, g, i)
		}
	}
	for i := 0; i < len(ds); i++ {
		fmt.Println(i+1, ds[i], fs[i])
	}
	return
}

func _dfs(ds, fs []int, time int, g *graph, idx int) int {
	ds[idx] = time
	for _, next := range g.edges[idx] {
		if ds[next] > 0 {
			continue
		}
		time++
		time = _dfs(ds, fs, time, g, next)
	}
	time++
	fs[idx] = time
	return time
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: global graph
global seen
token=0

def dfs(i):
    global token
    token+=1
    seen[i][0]=token
    for x in graph[i]:
        if seen[x][0]==0:
            dfs(x)
    token+=1
    seen[i][1]=token


n=int(input())

graph=[None for _ in range(n+1)]

for i in range(n):
    lst=list(map(int,input().split()))
    graph[lst[0]]=lst[2:]

seen=[[0,0] for _ in range(n+1)]

for i in range(1,n+1):
    if seen[i][0]==0:
        dfs(i)

for i in range(1,n+1):
    print("{} {} {}".format(i,seen[i][0],seen[i][1]))

Python: from collections import deque


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

M = [[0 for _ in range(N)] for _ in range(N)]
color = [0 for _ in range(N)]
d = [0 for _ in range(N)]
f = [0 for _ in range(N)]
nt = [0 for _ in range(N)]
tt = 0


def main():
    n = int(input())
    for _ in range(n):
        u, k, *v_s = map(int, input().split(' '))
        for j in range(k):
            M[u-1][v_s[j-1]-1] = 1

    dfs(n)

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


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


def dfs_visit(n, r):
    global tt
    color[r] = GRAY
    tt += 1
    d[r] = tt
    for v in range(0, n):
        if M[r][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(n, v)
    color[r] = BLACK
    tt += 1
    f[r] = tt


if __name__ == '__main__':
    main()

Python: def dfs(u):
    global time
    color[u] = 'Gray'
    time += 1
    d[u] = time
    for v in range(1, n+1):
        if data[u][v] == 0:
            continue
        if color[v] == 'White':
            dfs(v)
    color[u] = 'Black'
    time += 1
    f[u] = time


n = int(input())
A = [list(map(int, input().split()))for _ in range(n)]
data = [[0]*(n+1) for _ in range(n+1)]
color = ['White']*(n+1)
d = [None]*(n+1)
f = [None]*(n+1)
time = 0
for i in A:
    for j in i[2:]:
        data[i[0]][j] = 1

for u in range(1, n+1):
    if color[u] == 'White':
        dfs(u)
for i in range(1, n+1):
    print('{} {} {}'.format(i, d[i], f[i]))

Python: from enum import Enum

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

def dfs_visit(u, total_time):
  colors[u] = Color.GRAY
  total_time += 1
  d[u] = total_time
  stack.append(u)
  for v in range(N):
    if adj[u][v] == 1 and colors[v] == Color.WHITE:
      total_time = dfs_visit(v, total_time)
  colors[u] = Color.BLACK
  total_time += 1
  f[u] = total_time
  return total_time



def dfs(total_time):
  for i in range(N):
    if colors[i] != Color.WHITE:
      continue
    total_time = dfs_visit(i, total_time)

N = int(input())
L = [list(map(int, input().split())) for i in range(N)]
adj = [[0 for i in range(N)] for j in range(N)]

for i in range(N):
  for node in L[i][2:]:
    adj[i][node-1] = 1

colors = [Color.WHITE for i in range(N)]
stack = []
total_time = 0
d = [0 for i in range(N)]
f = [0 for i in range(N)]

dfs(total_time)

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

Python: # -*- coding: utf-8 -*-
'''
所要時間は、分位であった。
'''


# ライブラリのインポート
#import re
import sys
input = sys.stdin.readline
#import heapq
#import bisect
import collections
#import math
global time, Ls, Lf, E,LG,c,n
time = 1

def main():
    global time, Ls, Lf, E, LG,c,n
    c = 0
    n = int(input().strip())
    E = [[] for _ in range(n)]
    LG = collections.deque()
    for line in sys.stdin:
        Ltmp = list(map(int, list(line.strip().split())))
        if not Ltmp[1] == 0: 
            E[Ltmp[0]-1] = Ltmp[2:]
    Ls = [0]*n 
    Lf = [0]*n
    dfs(0)
    for i in range(n):
        print(i+1, Ls[i],Lf[i])
    
def dfs(cur):
    global time, Ls, Lf, E, LG,c,n
    LG.append(cur)
    if Ls[cur] == 0: Ls[cur] = time
    time += 1
    for i in range(len(E[cur])):
        check = E[cur][i] - 1
        if Ls[check] == 0: dfs(check)
    if Lf[cur] == 0:
        Lf[cur] = time
    try:
        x = LG.pop()
        if x == cur: x = LG.pop() 
        dfs(x)
    except:
        time += 1
        for i in range(c,n):
            if Ls[i] == 0:
                c = i
                dfs(i) 
    return




    


    
if __name__ == '__main__':
    main()

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])