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())
graph = [list(map(int, input().split())) for i in range(n)]

v_color = ["WHITE"] * (n+1) # WHITE:????¨???????GRAY:?¨??????????BLACK:??¢?´¢??????
d = [0] * (n+1)
f = [0] * (n+1)
stack = []
time = 0
def DFS(u):
    global time
    stack.append(u)

    while stack != []:
        u = stack[-1]
        time += 1
        
        v_color[u] = "GRAY"
        if d[u] == 0:
            d[u] = time
        
        for Next in graph[u-1][2:]:
            if v_color[Next] == "WHITE":
                stack.append(Next)
                break
        
        else:
            v_color[u] = "BLACK"
            f[u] = time +1
            stack.pop()
        
for i in range(1,n+1):
    if v_color[i] == "WHITE":
        DFS(i)
        time += 1
    
for i in range(1,n+1):
    print(i,d[i],f[i])

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_A&lang=jp
# Graph: python3
#  2018.12.05 yonezawa

import sys
input = sys.stdin.readline

class node:
    def __init__(self):
        n = self.n = int(input())
        self.cnt = 0
        self.start = [-1] * n
        self.end = [-1] * n
        self.pos = [1] * n
        self.nmap = [[0 for i in range(n)] for i in range(n)]

        for _ in range(n):
            l = list(map(int,input().split()))
            a = l[0] - 1
            for i in l[2:]:
                self.nmap[a][i-1] = 1
    def printMap(self):
        for i in self.nmap:
            print (*i)

    def printResult(self):
        for i in range(self.n):
            print(i+1,self.start[i],self.end[i])

    def dfs(self,u):
        self.cnt += 1
        self.start[u] = self.cnt
        self.pos[u] = 0
        for i in range(self.n):
            if self.nmap[u][i] == 1 and self.pos[i] == 1:    
                self.dfs(i)
        self.cnt += 1
        self.end[u] = self.cnt
                
def main():
    a = node()
    for i in range(a.n):
        if a.pos[i] == 1:
            a.dfs(i)
    a.printResult()

if __name__ == '__main__':
    main()

Python: # -*- coding: utf-8 -*-
# 標準入力を取得
n = int(input())
G = {}
for _ in range(n):
    g = list(map(int, input().split()))
    G[g[0]] = g[2:]

# 求解処理
seen = [False for _ in range(n)]
ans = []


def dfs(v: int) -> None:
    seen[v - 1] = True
    ans.append(v)

    for w in G[v]:
        if not seen[w - 1]:
            dfs(w)

    ans.append(v)


while sum(seen) != n:
    v = seen.index(False)
    dfs(v + 1)

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

for i, a in enumerate(ans):
    if d[a - 1] == -1:
        d[a - 1] = i + 1
    else:
        f[a - 1] = i + 1

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

Python: # coding=utf-8

n = int(input())
first_v = 0
time = 0
L = []
d = [None for _ in range(n)]
f = [None for _ in range(n)]
stuck = []
for _ in range(n):
    u, k, *v = map(int, input().split())
    L.append(list(map(lambda x: x-1, v)))

v = first_v #??¢?´¢????????????
while True:
    """print("------------------------------")
    print("stuck:", *stuck)
    print("L:", L)
    print("d:", *d)
    print("f:", *f)
    print("time:", time)
    print("v:", v)"""
    if len(L[v]) > 0:
        stuck.append(v)
        if not d[v]:
            time += 1
            d[v] = time
        v = L[v].pop(0)
    elif len(stuck) > 0:
        if not d[v]:
            time += 1
            d[v] = time
        if not f[v]:
            time += 1
            f[v] = time
        v = stuck.pop(-1)
    else:
        time += 1
        f[first_v] = time
        if None in d:
            first_v = d.index(None)
            v = first_v
        else:
            break

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

Python: # coding: UTF-8
from collections import deque
n = int(input())
arrow = {}
visited = set()
d = {}
f = {}


for i in range(n):
  raw = list(map(int,input().split()))
  #print(raw)
  u = raw.pop(0)
  k = raw.pop(0)
  raw.sort()
  q = deque(raw)
  arrow[u] = q
  d[u] = -1
  f[u] = -1

def dfslocal(v,t,arrow,f,d):
  t += 1
  d[v] = t
  visited.add(v)
  #print("First visit to " + str(v) + " at " + str(t) + "!")
  while arrow[v]:
    k = arrow[v].popleft()
    if k not in visited:
      
      t = dfslocal(k,t,arrow,f,d)
  f[v] = t+1
  return t+1

def dfsglobal(arrow,f,d):
  t = 0
  for u in range(n):
    if u+1 not in visited:
      t = dfslocal(u+1,t,arrow,f,d)
  return t

t = dfsglobal(arrow,f,d)

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

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())
d = deque()
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 = 1
dfs(1)
for i in range(1,n+1):
    tmp = "{} {} {}".format(i,S[i],F[i])
    print(tmp)

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

global time
time = 0

def dfs(u: int, visited: list, d: list, f: list, M: list, n: int):
    global time
    visited[u] = True
    time += 1
    d[u] = time
    for j in range(n):
        if M[u][j] == 1 and not visited[j]:
            dfs(j, visited, d, f, M, n)
    time += 1
    f[u] += time

def main():
    n = int(input())
    visited = [False] * n
    M = [[0]*n for _ in range(n)]
    for _ in range(n):
        a = list(map(int, input().split()))
        for i in range(a[1]):
            M[a[0]-1][a[i+2]-1] = 1
    d = [0] * n
    f = [0] * n
    for i in range(n):
        if not visited[i]:
            dfs(i, visited, d, f, M, n)
    for i in range(n):
        print(i+1, d[i], f[i])

if __name__ == '__main__': main()