Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type TimeRecord struct {
	discoverTime int
	finishTime   int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	timeRecordMap := make([]TimeRecord, n)
	childrenMap := make([][]int, n)
	for i := 1; i <= n; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		timeRecordMap[node-1] = TimeRecord{-1, -1}

		childNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		childrenMap[node-1] = make([]int, childNum)
		for j := 0; j < childNum; j++ {
			child, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			childrenMap[node-1][j] = child
		}
	}

	timer := 1
	startNode := 1
	for {
		visit(startNode, &timer, childrenMap, timeRecordMap)
		startNode = findStartNode(childrenMap, timeRecordMap)
		if startNode < 0 {
			break
		}
	}
	for k, v := range timeRecordMap {
		fmt.Printf("%d %d %d\n", k+1, v.discoverTime, v.finishTime)
	}
}

func findStartNode(childrenMap [][]int, timeRecordMap []TimeRecord) int {
	x := -1
	for node, timeRecord := range timeRecordMap {
		if timeRecord.discoverTime < 0 {
			x = node + 1
			break
		}
	}
	return x
}

func visit(currentNode int, timer *int, childrenMap [][]int, timeRecordMap []TimeRecord) {
	if timeRecordMap[currentNode-1].discoverTime > 0 {
		return
	}
	children := childrenMap[currentNode-1]
	timeRecordMap[currentNode-1] = TimeRecord{*timer, timeRecordMap[currentNode-1].finishTime}
	(*timer)++
	for i := 0; i < len(children); i++ {
		visit(children[i], timer, childrenMap, timeRecordMap)
	}
	timeRecordMap[currentNode-1] = TimeRecord{timeRecordMap[currentNode-1].discoverTime, *timer}
	(*timer)++
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

Python: def dfs(v,t):
    d[v]=t
    t+=1
    for i in edge[v]:
        if d[i]==-1:
            t=dfs(i,t)
    f[v]=t
    t+=1
    return t

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
k=1
for i in range(n):
    if d[i]==-1:
        k=dfs(i,k)
for i in range(n):
    print(i+1,d[i],f[i])

Python: from sys import stdin

d_num = int(stdin.readline().strip())

graph = [[] for _ in range(d_num+1)]
for _ in range(d_num):
    u, k, *neighbors = map(int, stdin.readline().split())
    for v in neighbors:
        graph[u].append(v)

visited = set()
timestamps = [None for _ in range(d_num+1)]

def dfs(u, stmp):
    visited.add(u)
    entry = stmp

    stmp += 1
    for v in graph[u]:
        if v not in visited:
            stmp = dfs(v, stmp)

    timestamps[u] = entry, stmp
    return stmp+1

stmp = 1
for v in range(1, d_num+1):
    if v not in visited: stmp = dfs(v, stmp)

for num, (entry, leave) in enumerate(timestamps[1:], 1):
    print(num, entry, leave)

Python: N = int(input())

E = [None] * (N+1)
for _ in range(N):
    u, k, *vs = list(map(int, input().split()))
    E[u] = vs

d = [0] * (N+1)
f = [0] * (N+1)
t = 0

def dfs(v):
    global t, d, f
    if d[v]:
        return
    t += 1
    d[v] = t
    for u in E[v]:
        dfs(u)
    t += 1
    f[v] = t

for v in range(1, N+1):
    dfs(v)

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

Python: import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

time = 0

def dfs(u, adj, d, f, sumi):
    global time
    sumi.add(u)
    time += 1
    d[u] = time

    for v in adj[u]:
        if v not in sumi:
            dfs(v, adj, d, f, sumi)
            
    time += 1

    f[u] = time


def solve():
    n = int(input())
    adj = [None] * n

    for i in range(n):
        u, k, *vs = [int(j) - 1 for j in input().split()]
        # debug(vs, locals())
        adj[u] = vs

    # debug(adj, locals())
    
    d = [0] * n
    f = [0] * n
    sumi = {0}
    mitan = set(range(1, n))

    dfs(0, adj, d, f, sumi)
    mitan -= sumi
    # debug(mitan, locals())

    
    while mitan:
        u = min(mitan)
        dfs(u, adj, d, f, sumi)
        mitan -= sumi
        # debug(mitan, locals())
        

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

    pass

if __name__ == '__main__':
    solve()

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


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

Python: def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList

WHITE=0
GRAY=1
BLACK=2
color=[]
d=[]
f=[]
_time=0

def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time

def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1

def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

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

if __name__=='__main__':
    n,nList=readinput()
    main(n,nList)

Python: n = int(input())
graph = {}
ans = []
np = set(range(1,n+1))
for i in range(n):
    t = list(map(int, input().split()))
    v = t[0]
    k = t[1]
    graph[v] = [t[i+2] for i in range(k)]
    np=np - set(graph[v])
    ans.append([v,None,None])

class Time:
    def __init__(self):
        self.time = 0
    def getTime(self):
        self.time += 1
        return self.time

t = Time()

visited = set()
def dfs(graph, target):
    visited.add(target)
    # ???????????????????¨????
    if ans[target - 1][1] is None:
        ans[target - 1][1] = t.getTime()
    if graph[target] == []:
        if ans[target - 1][2] is None:
            ans[target - 1][2] = t.getTime()        
        return
    for v in sorted(graph[target]):
        # # ???????????????????¨????
        if v not in visited:        
            if ans[v - 1][1] is None:
                ans[v - 1][1] = t.getTime()        
            visited.add(target)
            dfs(graph,v)
    if ans[target - 1][2] is None:
        ans[target - 1][2] = t.getTime()

for v in np:
    dfs(graph,v)
for row in ans:
    print(*row)