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: def main():
    n = int(input())
    _next, state, d, f = [], [False] * n, [0] * n, [0] * n
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next.append(sorted(v, reverse=True))
    stack = []
    time = 0

    while True:
        for i in range(n):
            if not state[i]:
                stack.append(i)
                state[i] = True
                time += 1
                d[i] = time
                break
        if not stack:
            break
        while stack:
            u = stack[-1]
            if _next[u]:
                v = _next[u].pop()
                if not state[v]:
                    state[v] = True
                    time += 1
                    d[v] = time
                    stack.append(v)
            else:
                time += 1
                f[u] = time
                stack.pop()

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


if __name__ == '__main__':
    main()

Python: def dfs(v, cnt):
  D[v] = cnt
  cnt += 1
  for c in edge[v]:
    if D[c] == -1:
      cnt = dfs(c, cnt)
  F[v] = cnt
  cnt += 1
  return cnt

N = int(input())
edge = [[] for _ in range(N)]
for _ in range(N):
  u, _, *v = map(lambda x:int(x)-1, input().split())
  edge[u] = sorted(v)
D, F = [-1] * N, [-1] * N
c = 1
for i in range(N):
  if D[i] == -1:
    c = dfs(i, c)
for i, (d, f) in enumerate(zip(D, F)):
  print(i + 1, d, f)

Python: # =============================================================================
#　Depth First Search
# =============================================================================
import sys
sys.setrecursionlimit(10**8)

N=int(input())
Graph=[list(map(int,input().split()))[2:] for _ in range(N)]
Graph.insert(0,[])
seen=[False for _ in range(N+1)]
first_order=[0]*(N+1)
last_order=[0]*(N+1)
time=1

def dfs(Graph,v):
    global seen
    global time
    seen[v]=True
    first_order[v]=time
    time+=1
    for nv in Graph[v]:
        if seen[nv]:
            continue
        dfs(Graph,nv)

    last_order[v]=time
    time+=1

for v in range(1,N+1):
    if not seen[v]:
        dfs(Graph,v)
for i in range(1,N+1):
    print(i,first_order[i],last_order[i])

Python: def dfs(n):
    global t
    S[n] = t
    DP[n]=1
    for i in A[n][2:]:
        if not DP[i]:
            t += 1
            dfs(i)
    t += 1
    F[n] = t
n = int(input())
A = [0]*(n+1)
S = [0]*(n+1)
F = [0]*(n+1)
DP = [0]*(n+1)
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
t = 0
for i in range(1,n+1):
    if not DP[i]:
        t+=1
        dfs(i)
for i in range(1,n+1):
    tmp = "{} {} {}".format(i,S[i],F[i])
    print(tmp)

Python: import sys
sys.setrecursionlimit(10**7)
time = 0
def input():
    return sys.stdin.readline()
n = int(input())
G = [[]]
d = [0]*(n+1)
f = [0]*(n+1)
for _ in range(n):
    G.append(list(map(int,input().split()))[2:])
def dfs(e):
    global time
    d[e] = time
    for nexte in G[e]:
        if d[nexte] == 0:
            time += 1
            dfs(nexte)
    time += 1
    f[e] = time

for i in range(1,n+1):
    if d[i] == 0:
        time += 1
        dfs(i)
for i in range(1,n+1):
    print(i,d[i],f[i])

Python: n=int(input())
#V[u]=[k,[v],d,f]
V=[[0,[],0,0]]
for i in range(n):
    v=[int(i) for i in input().split(" ")]
    u=v.pop(0)
    k=v.pop(0)
    v.sort()
    V.append([k,v,0,0])


def is_finished(Id):
    k=V[Id][0]
    v=V[Id][1]
    for i in range(k):
        if V[v[i]][2]==0:
            return v[i]
    return -1

time=1

while time<=2*n:
 for i in range(1,n+1):
     if V[i][2]==0:
         cur=i
         log=[i]
         break
     print(cur)
 while len(log)>0:
    if V[cur][2]==0:
        V[cur][2]=time
        time+=1


    x=is_finished(cur)
    if x!=-1:
        cur=x
        log.append(cur)
    else:
        try:
            log.pop(-1)
            V[cur][3]=time
            time+=1
            if V[cur][3]==V[cur][2]:
                time+=1
                V[cur][3]+=1

            cur=log[-1]
        except:
            pass
for i in range(1,n+1):
    print(i,V[i][2],V[i][3])

Python: import sys
sys.setrecursionlimit(3000)

n = int(input())
X = []
for _ in range(n):
    x = list(map(int, input().split()))
    if x[1] != 0:
        X.append(x[2:])
    else:
        X.append([])

df = [[0, 0] for _ in range(n)]
t = 0
def search(i = 0):
    global t
    if df[i][0] == 0:
        t += 1
        df[i][0] = t
    for x in X[i]:
        if df[x - 1][0] == 0:
            search(x - 1)
    if df[i][1] == 0:
        t += 1
        df[i][1] = t

for i in range(n):
    search(i)

for i, x in enumerate(df):
    print('{} {} {}'.format(i + 1, x[0], x[1]))

Python: import sys
input = sys.stdin.readline

from collections import deque

S = deque([])

def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        M[i][A[k]-1] = 1


def dfs(u):
    global t
    t = t + 1
    S.append(u)
    c[u] = 1
    d[u] = t

    for v in range(n):
        if M[u][v] and c[v] == 0:
            dfs(v)
    c[u] = 2
    t += 1
    f[u] = t


n = int(input())
#color
c = [0] * n
d = [0] * n
f = [0] * n

M = [[0] * n for _ in range(n)]
t = 0

for i in range(n):
    A = list(map(int, input().split()))
    getNeighbor(A,i)

for num in range(n):
    if d[num] == 0:
        dfs(num)

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