Go: package main

import "fmt"

type node struct {
	id, d, v int
	adj      []node
}

var n int
var seen []bool
var graph []node

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func dfs(v, t int) int {
	// fmt.Printf("dfs(v %d, t %d) en:\n", v, t)
	seen[v] = true
	graph[v].v = t
	tt := t
	for _, vv := range graph[v].adj {
		if !seen[vv.id] {
			tt = max(tt, dfs(vv.id, tt+1))
		}
	}
	graph[v].d = tt + 1
	// fmt.Printf("dfs(v %d, t %d) ex: d %d\n", v, t, graph[v].d)
	return graph[v].d
}

func main() {
	fmt.Scan(&n)
	graph = make([]node, n+1)
	for i := 1; i <= n; i++ {
		var u, k, v int
		fmt.Scan(&u, &k)
		graph[u].id = u
		// 頂点uはk個の隣接頂点を持っている
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			var t node
			t.id = v
			graph[u].adj = append(graph[u].adj, t)
		}
	}
	// fmt.Println("n", n, "graph", graph)
	seen = make([]bool, n+1)
	// t := dfs(1, 1)
	v, t := 1, 1
	done := false
	for !done {
		tt := dfs(v, t)
		done = true
		for i := range seen {
			if !seen[i] {
				done = false
				v = graph[i].id
				t = tt
				break
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, graph[i].v, graph[i].d)
	}
}

Python: def dfs_time(G, s):
    # ?????????
    color = {key: 'White' for key in G.keys()}
    d = {key: 0 for key in G.keys()}
    f = {key: 0 for key in G.keys()}

    # ????????????
    S = [s]
    color[s] = 'Gray'
    time = 1
    d[s] = time

    while len(S) > 0:
        u = S[-1]
        v_list = G[u]
        if v_list:
            v = v_list.pop()
        else: v = None
        if v is not None:
            if color[v] == 'White':
                color[v] = 'Gray'
                time += 1
                d[v] = time
                S.append(v)
        else:
            S.pop()
            color[u] = 'Black'
            time += 1
            f[u] = time

    return d, f


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp

    d, f= dfs_time(G, 1)

    for node in G:
        print('{0} {1} {2}'.format(node, d[node], f[node]))

Python: # coding: UTF-8
from collections import deque
#入力値の整理
n = int(input())
adj={}
visited={}
d={}
f={}
t=0
for i in range(n):
  raw=list(map(int,input().split()))
  u = raw.pop(0)
  k = raw.pop(0)
  raw.sort()
  q = deque(raw)
  adj[u] = q
  visited[u] = False
  d[u] = -1
  f[u] = -1

next = 1
v = next
d[next] = t + 1
stack = deque()

while True:
  t = t+1
  clear = True
  while adj[v]:
    cand = adj[v].popleft()
    if not visited[cand]:
      next = cand
      d[cand] = t + 1
      visited[cand] = True
      clear = False
      break
  if clear:
    f[v] = t + 1
    if stack:
      next = stack.pop()
      v = next
    else:
      break
  else:
    stack.append(v)
    v = next

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

#return d,f

Python: import sys
from collections import defaultdict


time = 1


def dfs(g, visited, start, ans):
    global time
    visited[start - 1] = True
    ans[start - 1][0] = time
    for e in g[start]:
        if not visited[e - 1]:
            time += 1
            dfs(g, visited, e, ans)
    time += 1
    ans[start - 1][1] = time


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    g = defaultdict(list)
    for i in range(n):
        line = list(map(int, input().split()))
        if line[1] > 0:
            g[line[0]] = line[2:]
    visited = [False] * n

    ans = [[1, 1] for _ in range(n)]
    global time
    while False in visited:
        start = visited.index(False) + 1
        dfs(g, visited, start, ans)
        time += 1

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


if __name__ == "__main__":
    main()

Python: from sys import stdin
n = int(stdin.readline())
M = [None] + [list(map(int, stdin.readline().split()[2:])) for i in range(n)]
sndf = [None] + [[False, i] for i in range(1, n + 1)]
tt = 0
def dfs(u):
    global tt
    sndf[u][0] = True
    tt += 1
    sndf[u].append(tt)
    for v in M[u]:
        if not sndf[v][0]:
            dfs(v)
    tt += 1
    sndf[u].append(tt)
for i in range(1, n + 1):
    if not sndf[i][0]:
        dfs(i)
for x in sndf[1:]:
    print(*x[1:])

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

Python: import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
idx = 1


def resolve():
    def dfs(v):
        global idx
        begin[v] = idx
        idx += 1
        for u in edge[v]:
            if begin[u] != 0:
                continue
            else:
                dfs(u)
        end[v] = idx
        idx += 1

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *V = map(int, input().split())
        for v in V:
            edge[u - 1].append(v - 1)

    begin = [0] * n
    end = [0] * n
    for i in range(n):
        if begin[i] == 0:
            dfs(i)
    for i in range(n):
        print(i + 1, begin[i], end[i])


if __name__ == '__main__':
    resolve()

Python: # Graph I - Depth First Search
def dfs(idx,A):
    global time,d,f
    d[idx] = time
    time += 1
    for i,v in enumerate(A[idx]):
        if v == 1 and d[i] == 0:
            dfs(i,A)
    f[idx] = time
    time += 1

n = int(input())
A = []
for _ in range(n):
    ss = list(map(int, input().split()))
    u = ss[0] - 1
    row = [0 for _ in range(n)]
    for i in range(ss[1]):
        idx = 2 + i
        row[ss[idx]-1] = 1
    A.append(row)

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

for i,t in enumerate(zip(d,f)):
    print(i+1,t[0],t[1])

Python: n = int(input())
graph = [[0 for i in range(n)] for j in range(n)]
d = [0] * n
f = [0] * n
g = [0] * n

time = 0

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

def search(i):
    global time
    time = time + 1
    d[i] = time
    g[i] = 1
    for j in range(i, n):
        if (graph[i][j] == 1) & (g[j] == 0):
            search(j)
    time = time + 1
    f[i] = time if f[i] == 0 else f[i]


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

Python: INTERVAL = 1
WHITE = 0
GRAY = 1
BLACK = 2


def depth_first_search(u):
    global time

    time += INTERVAL
    d[u] += time
    color[u] = GRAY

    for v in range(n):
        if matrix[u][v] and color[v] == WHITE:
            depth_first_search(v)

    time += INTERVAL
    f[u] += time
    color[u] = BLACK

# INPUT
n = int(input())


time = 0
color = [WHITE] * n
d = [0] * n
f = [0] * n

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

for i in range(n):

    u, k, *V = map(int, input().split())

    for v in V:
        v -= 1
        matrix[i][v] = 1


# PROCESS
u_search = 0
while True:

    depth_first_search(u_search)

    if WHITE not in color:
        break
    else:
        u_search = color.index(WHITE)
        

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