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) doesAllAdjsVisited(vertices []vertex) bool {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return false
		}
	}
	return true
}

func (v *vertex) isFirstVisit() bool {
	return v.firstVisitAt == 0
}

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

func (v *vertex) nextVertex(vertices []vertex) (vertexId, error) {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return vId, nil
		}
	}
	return vertexId(-1), fmt.Errorf("illegal")
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	timestamp = timestamp + 1
	currentVertex := &vertices[currentId-1]
	if currentVertex.isFirstVisit() {
		currentVertex.firstVisitAt = timestamp
	}
	for !currentVertex.doesAllAdjsVisited(vertices) {
		nextVertexId, _ := currentVertex.nextVertex(vertices)
		timestamp = dfs(nextVertexId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

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

	dfs(vertexId(1), 0, 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: n = int(input())
A = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1
        A[int(j)-1][int(u)-1] = 1

# for row in A:
#     msg = ' '.join(map(str, row))
#     # print(msg)

d = [-1] * n
f = [-1] * n

t = 0
d[0] = t

# recursive


def dfs(u):
    global t
    # print('visit:', str(u), str(t))
    t += 1
    d[u] = t
    for i in range(len(A[u])):
        if A[u][i] == 1 and d[i] == -1:
            dfs(i)
    t += 1
    f[u] = t
            

dfs(0)


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

Python: n = int(input())
E = [[]]*n

for i in range(n):
    inputs = list(map(int, input().split()))
    if inputs[1]==0:
        continue
    else:
        for j in inputs[2:]:
            E[i] = E[i] + [j-1]

beg = [-1]*n
fin = [-1]*n  

t = 0      
def f(i):
    global t
    #print(i,t)
    t += 1
    beg[i] = t
    for c in E[i]:
        if beg[c]>0:
            continue
        else:
            f(c)
    t+=1
    fin[i] = t
    

for i in range(n):
    if beg[i]<0:
        f(i)
for i in range(n):
    print(i+1, beg[i], fin[i])

Python: # N=4
# G=[[2],[4],[],[3]]
import sys
sys.setrecursionlimit(1000000)

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

v=[False]*N
d=[0]*N
f=[0]*N
timecount=1

def dfs(i):
    global timecount
    if v[i] : return
    v[i] = True
    d[i] = timecount
    # print("D",i+1,timecount)
    timecount += 1
    for p in G[i]:
        dfs(p-1)
    # print("F",i+1,timecount)
    f[i] = timecount
    timecount += 1
    return

for i in range(N):
  if v[i]==0: dfs(i)

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

Python: class Node(object):
    def __init__(self, V):
        self.V = V
        self.find_time = 0
        self.finish_time = 0
        self.is_finded = False
 
 
n = int(input())
nodes = [0] * 101
for i in range(n):
    data = input().split()
    V = list(map(int, data[2:]))
    V.sort(reverse = True)
    nodes[int(data[0])] = Node(V)
 
find_q = []
find_q.append(1)
cnt = 1
q_size = len(find_q)
while q_size != 0:
    #print(find_q)
    node = nodes[find_q[q_size -1]]
 
    if node.is_finded == False:
        node.find_time = cnt
        cnt += 1
        node.is_finded = True
        if len(node.V) == 0:
            node.finish_time = cnt
            cnt += 1
            find_q.pop()
            q_size = len(find_q)
        else:
            for x in node.V:
                if x in find_q:
                    find_q.remove(x)
                if nodes[x].finish_time == 0: 
                    find_q.append(x)
            q_size = len(find_q)
 
    else:   #node is finded
        node.finish_time = cnt
        cnt += 1
        find_q.pop()
        q_size = len(find_q)
        continue
 
for i in range(1, n+1):
    if not isinstance(nodes[i], int):
        print(i, nodes[i].find_time, nodes[i].finish_time)

Python: n=int(input())
adj=[]
for i in range(n):
      adj.append(list(map(int, input().split())))

edge=[[0 for i2 in range(n)]for i1 in range(n)]

#??£??\???????????????

for i in range(n):
    for j in range(adj[i][1]):
        edge[i][adj[i][j+2]-1]=1

#print(edge)

discover=[0 for i in range(n)]
final=[0 for i in range(n)]
stack=[0]

def dfs(id,time):
    for i in range(n):
        c=0
        if edge[id][i]==1 and discover[i]==0:
            stack.append(id)
            discover[i]=time
            c+=1
            #print(discover,final,i,stack)
            dfs(i,time+1)
        else:
            pass
    if c==0:
        if len(stack)>0:
            if final[id]==0:
                final[id] = time
                #print("back",discover,final,id,stack)
                dfs(stack.pop(), time + 1)
            else:
                #print("back", discover, final, id, stack)
                dfs(stack.pop(),time)

discover[0]=1
dfs(0,2)

for i in range(n):
    print(i+1,discover[i],final[i])

Python: import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
time = 1
def dfs(node):
    global time
    if visited[node] != -1:
        return
    visited[node] = 0
    d[node] = time
    time += 1
    for v in edge[node]:
        dfs(v)
    f[node] = time
    time += 1

if __name__ == "__main__":
    n = int(input())
    edge = []
    for i in range(n):
        u,k,*v_li = map(int,input().split())
        for j in range(len(v_li)):
            v_li[j] -= 1
        u-=1
        edge.append(v_li)
    d = {}
    f = {}
    visited = [-1]*n
    
    for i in range(n):
        dfs(i)
    for i in range(n):
        print(i+1,d[i],f[i])

Python: # -*- coding: utf-8 -*-
"""
Created on Sun Jul  8 10:56:33 2018

@author: maezawa
"""
n = int(input())
adj = [[] for _ in range(n)]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]

for j in range(n):
    ain = list(map(int, input().split()))
    u = ain[0]
    k = ain[1]
    for i in range(k):
        adj[u-1].append(ain[i+2]-1)
    adj[u-1].sort()
    #print(*adj[u-1])

t = 1
stack = [0]

while stack:
    print(stack)
    current = stack[-1]
    if d[current] == 0:
        d[current] = t
        t += 1
    flag = 0
    for k in adj[current]:
        if d[k] == 0:
            stack.append(k)
            flag = 1
            break
    if flag == 0:
        f[current] = t
        t += 1
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack)

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