Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}
	}

	d := make([]int, n+1)
	f := make([]int, n+1)
	t := 1
	for i := 1; i <= n; i++ {
		t = dfs(&g, &d, &f, i, t)
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, d[i], f[i])
	}
}

func dfs(g *[][]int, d *[]int, f *[]int, id int, t int) int {
	if (*d)[id] > 0 {
		return t
	}
	(*d)[id] = t
	t += 1
	for i := 0; i < len((*g)[id]); i++ {
		t = dfs(g, d, f, (*g)[id][i], t)
	}
	(*f)[id] = t
	return t+1
}

Python: def curserch(N,k,t,P,n):
    g = 0
    t = t+1
    P[k]["d"] = t
    for i in range(1,n+1):
        if N[k][i]==1 and P[i]["d"]==0:
            P[i]["f"] = g = curserch(N,i,t,P,n)
    if g !=0:
        return g+1
    else:
        return t+1

n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]


for i in range(n):
    vec = input().split()
    u = int(vec[0])
    k = int(vec[1])
    nodes = vec[2:]
    for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1

P=[{"d":0} for i in range(n+1)]
h = 0
for i in range(1,n+1):
    if P[i]["d"]==0:
        P[i]["f"] = h = curserch(A,i,h,P,n)
for i in range(1,n+1):
    print(i,P[i]["d"],P[i]["f"])

Python: #!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n = int(input())
G1 = [0]*n
G2 = [[0]*n for i in range(n)]
d = [0]*n
f = [0]*n
for i in range(n):
    inp = tuple(map(int, input().split()))
    G1[inp[0]-1]=sorted(inp[2:])
time=0

def dfs(vertex):
    if d[vertex]:return
    global time
    time+=1
    d[vertex]=time
    for v in G1[vertex]:
        if not d[v-1]:
            dfs(v-1)
    time+=1
    f[vertex]=time
    return

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

Python: #http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B&lang=ja

def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
adj = [None]
for _ in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

sndf = [None]
for i in range(1, n+1):
    sndf.append([False, i])

path = []

time = 0

def dfs(u):
    global time
    sndf[u][0] = True
    time += 1
    sndf[u].append(time)

    for v in adj[u]:
        if not sndf[v][0]:
            dfs(v)
    time += 1
    sndf[u].append(time)

for i in range(1, n+1):
    if not sndf[i][0]:
        dfs(i)

for x in sndf[1:]:
    print(*x[1:])

Python: # DFS(再帰） v:頂点
def DFS(v,dis,fin):
  global time
  # 訪問したので発見時刻を記入
  dis[v] = time
  time += 1
  
  # vに隣接している頂点をみる
  for u in V[v]:
    if dis[u] is None:
      DFS(u,dis,fin)
  
  # 処理が完了したので完了時刻を記入
  fin[v] = time
  time += 1
  
n = int(input())
# 0番目を始点として初期化
V = [[i for i in range(n)]]

for i in range(n):
  _, __, *a = map(int, input().split())
  V.append(a)

dis = [None]*(n+1)# 頂点を発見した時刻
fin = [None]*(n+1)# 頂点の完了時刻
time = 0
DFS(0,dis,fin)

for i in range(1,n+1):
  print(i, dis[i], fin[i])

Python: n = int(input())
M = [[0]*n for _ in range(n)]
st = [0]*n
d = [0]*n
f = [0]*n
tt = 0

def dfs(u,t):
    st[u] = 1
    t += 1
    d[u] = t
    for v in range(n):
        if M[u][v] == 0: continue
        if st[v] == 0: t = dfs(v,t)
    st[u] = 2
    t += 1
    f[u] = t
    return t

for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = 1

for i in range(n):
    if st[i] == 0: tt = dfs(i,tt)

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

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

N = I()
adj = [[] for _ in range(N)]
visited = [False for _ in range(N)]
d = [0 for _ in range(N)]
f = [0 for _ in range(N)]
cur_time = 1

for _ in range(N):
    edge_list = LI()
    u, k = edge_list[0], edge_list[1]
    if k == 0:
        continue
    v_list = edge_list[2:]
    u -= 1
    for v in v_list:
        v -= 1
        adj[u].append(v)

def dfs(u):
    global cur_time
    visited[u] = True
    for v in adj[u]:
        if not visited[v]:
            d[v] = cur_time
            cur_time += 1
            dfs(v)
    f[u] = cur_time
    cur_time += 1
    return

for v in range(N):
    if not visited[v]:
        d[v] = cur_time
        cur_time += 1
        dfs(v)

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

Python: n = int(input())
u = [list(map(int,input().split())) for _ in range(n)]
ts = [[0,0] for _ in range(n)]
time = 0

def dfs(i):
  global time
  ts[i][0] = time
  for j in range(u[i][1]):
    a = u[i][j+2]-1
    if ts[a][0] == 0:
      time += 1
      dfs(a)
  time += 1
  ts[i][1] = time
  
for i in range(n):
  if ts[i][0] == 0:
    time += 1
    dfs(i)
  
for i in range(n):
  print(i+1, ts[i][0], ts[i][1])

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
            del find_q[q_size -1]
            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
        del find_q[q_size -1]
        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: 
def DFS(i):
    for j in range(n):
        if A[i][j] == 1 and d[j] == 0:
            time = time + 1
            d[j] = time
            DFS(j)
    time = time + 1
    f[i] = time

n = int(raw_input())
A = [0] * n
for i in range(n):
    A[i] = [0] * n

for i in range(n):
    value = map(int, raw_input().split())
    u = value[0] - 1
    k = value[1]
    nodes = value[2:]
    for i in range(k):
        v = nodes[i] - 1
        A[u][v] = 1

d = [0] * n
f = [0] * n
time = 1
d[0] = time
DFS(0)

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

Python: from collections import defaultdict

def dfs(here, connect, visited, answer):
    global time
    answer[here][1] = time
    time += 1
    visited[here] = 1
    for c in connect[here]:
        if not visited[c]:
            dfs(c, connect, visited, answer)
    answer[here][2] = time
    time += 1

v_num = int(input())
connect = defaultdict(list)
for _ in range(v_num):
    inp = [int(n) - 1 for n in input().split(" ")]
    connect[inp[0]].extend(sorted(inp[2:]))
answer = [[n + 1 for m in range(3)] for n in range(v_num)]
time = 1
visited = [0 for n in range(v_num)]
dfs(0, connect, visited, answer)
for v in range(v_num):
    print(*answer[v])