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
	for {
		startNode := findStartNode(childrenMap, timeRecordMap)
		if startNode > 0 {
			visit(startNode, &timer, childrenMap, timeRecordMap)
		} else {
			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
		}
	}
	if x < 0 {
		return -1
	}

	found := false
	i := 0
	for i < len(childrenMap) {
		children := childrenMap[i]
		for j := 0; j < len(children); j++ {
			if children[j] == x {
				x = j + 1
				found = true
				break
			}
		}
		if found {
			i = 0
		} else {
			i++
		}
	}
	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: #!/usr/bin/env python
# -*- coding: utf-8 -*-

"""

input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0

output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

"""
import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2


def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None


def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = map(lambda x: x.split(), _input[1:])

    # config length = (v_num + 1)
    # stack = []
    visited = [UNVISITED] * (v_num + 1)
    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))
    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1
    timer = 1
    dfs_init()

    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print(index, *time_info)

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)]
v = []

for i in range(n):
    g.append(list(map(int, input().split())))


for i in range(1, n + 1):
    if i in v:
        continue
    dfs(g,i,r,v)
    j += 1

for i in range(1,n+1):
    print(*r[i])

Python: # -*- coding: utf-8 -*-
"""
Created on Thu May  7 23:19:34 2020

@author: Kanaru Sato
"""
def dfs(v):
    global t
    d[v] = t
    t += 1
    if conlist[v]:
        for connectedv in conlist[v]:
            if d[connectedv] == -1:
                dfs(connectedv)
    f[v] = t
    t += 1

n = int(input())
conlist = [[] for i in range(n)]
for i in range(n):
    _ = list(map(int,input().split()))
    [conlist[i].append(_[k]-1) for k in range(2,_[1]+2)]

d = [-1 for i in range(n)]
f = [-1 for i in range(n)]
t = 1

for i in range(n):
    if d[i] == -1:
        dfs(i)    

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

Python: import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N = int(input())
G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)]
D = [-1]*N
D2 = [-1]*N
def dfs(cur, now):
    if D[cur] != -1: return now
    now += 1
    D[cur] = now
    for nex in G[cur]:
        now = dfs(nex,now)
    now += 1
    D2[cur] = now
    return now
now = 0
for i in range(N):
    now = dfs(i,now)
for i in range(N):
    print(i+1,D[i],D2[i])

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