Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	currentVertex := &vertices[currentId-1]
	if currentVertex.isVisited() {
		return timestamp
	}
	timestamp = timestamp + 1
	currentVertex.firstVisitAt = timestamp
	for _, adjId := range currentVertex.adjs {
		timestamp = dfs(adjId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func solveDfs(vertices []vertex) {
	timestamp := 0
	for {
		vid := vertexId(-1)
		for i := 0; i < len(vertices); i++ {
			if !vertices[i].isVisited() {
				vid = vertexId(i+1)
				break
			}
		}
		if vid > 0 {
			timestamp = dfs(vid, timestamp, vertices)
		} else {
			break
		}
	}
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveDfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

Python: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

n = int(input())
info = [list(map(int, input().split())) for _ in range(n)]
ans = [[i + 1, -1, -1] for i in range(n)]
check = [0 for _ in range(n)]
time = 1


def dfs(num):
    check[num] = 1
    global time
    if ans[num][1] == -1:
        ans[num][1] = time
        time += 1
    for i in info[num][2:]:
        if check[i - 1] == 0:
            dfs(i - 1)
    if ans[num][2] == -1:
        ans[num][2] = time
        time += 1


for i in range(n):
    if check[i] == 0:
        dfs(i)

for i in ans:
    i = list(map(str, i))
    print(" ".join(i))

Python: n = int(input())
a = [[] for i in range(n)]

for i in range(n):
    v = list(map(int, input().split()))
    u = v[0] - 1
    for j in v[2:]:
        a[u].append(j-1)

d = [0] * n
f = [0] * n
isDiscovered = [False] * n
time_stamp = 0

def dfs(x):
    global time_stamp
    isDiscovered [x] = True
    time_stamp += 1
    d[x] = time_stamp
    for i in a[x]:
        if not isDiscovered[i]:
            dfs(i)
    time_stamp += 1
    f[x] = time_stamp

for i in range(n):
    if not isDiscovered[i]:
        dfs(i)

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

Python: def visit(u):
    global color, d, f, t
    color[u] = GRAY
    d[u] += t
    t += 1
    for v in range(n):
        if (M[u][v] == 0):
            continue
        else:
            if (color[v] == WHITE):
                visit(v)
    color[u] = BLACK
    f[u] = t
    t += 1

n = int(input())
M = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    tmp = list(map(int, input().split()))
    for i in range(tmp[1]):
        M[tmp[0]-1][tmp[i+2]-1] = 1

WHITE = 0
GRAY = 1
BLACK = 2
color = [WHITE for i in range(n)]
d, f = [0 for i in range(n)]
t = 1
for u in range(n):
    if (color[u] == WHITE):
        visit(u)
for u in range(n):
    print(str(u+1), str(d[u]), str(f[u]), sep = " ")

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()]
    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 i in range(N):
    temp = [i + 1, ans[0][i+1], ans[1][i+1]]
    print(* temp)

Python: def main():
    """有向グラフの深さ優先探索
    
    Note: V[i]は頂点iから通じる頂点の集合を意味するが, 
             V[i]: {i}とV[i]: Noneは同値とされている。
    """
    stack = []
    passed_lines = []
    
    n = int(input())
    V = [None]*(n+1)
    D = [None]*(n+1)
    F = [None]*(n+1)
    passed_vertices = set()
    vertices = set(range(1, n+1))
    
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = set(v)

    i = 1
    time = 1
    stack.append(i)
    passed_vertices.add(i)
    D[i] = time
    while i <= n:
        new_vertices = list(V[i] - passed_vertices - {i})
        new_vertices.sort()
        if new_vertices:
            i = new_vertices[0]
            stack.append(i)
            time += 1
            if D[i] is None:
                D[i] = time
                passed_vertices.add(i)
            continue
        else:
            while stack:
                i = stack.pop()
                time += 1
                if list(V[i] - passed_vertices - {i}):
                    stack.append(i)
                    time -= 1
                    break
                if F[i] is None: 
                    F[i] = time
        if not list(V[i] - passed_vertices - {i}) and not stack:
            rest_vertices = list(vertices-passed_vertices)
            if rest_vertices:
                i = min(rest_vertices)
                stack.append(i)
                time += 1
                if D[i] is None:
                    D[i] = time
                    passed_vertices.add(i)
            else:
                break
    for id, d, f in zip(range(1, n+1), D[1:], F[1:]):
        print("{} {} {}".format(id, d, f))
        
if __name__ == "__main__":
    main()

Python: #coding:utf-8
        
def DFS(i,A,d,f,t):
    if d[i-1]:
        return t
    d[i-1]=t
    t+=1
    for j in A[i-1]:
        t=DFS(j,A,d,f,t)
    f[i-1]=t
    t+=1
    return t

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
    d=[0]*n
    f=[0]*n
    t=1
    for i in range(n):
        if d[i]==0:
            t=DFS(i+1,A,d,f,t)
    for i in range(n):
        print("{} {} {}".format(i+1,d[i],f[i]))

Python: def d(u):
 global t
 C[u]=0;t+=1;D[u]=t
 for v in R:
  if M[u][v]and C[v]:d(v)
 t+=1;F[u]=t
N=int(input());R=range(N)
M=[[0]*N for _ in R]
for e in[input().split()for _ in R]:
 for v in e[2:]:M[int(e[0])-1][int(v)-1]=1
C,D,F=[1]*N,[0]*N,[0]*N
t=0
for i in R:
 if C[i]:d(i)
for i in R:print(i+1,D[i],F[i])