Go: #include <bits/stdc++.h>
using namespace std;
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;
static const int N     = 100;

int n, tt;
vector<vector<int>> M(N, vector<int>(N));
vector<int> color(N), d(N), f(N);

void dfs_visit(int u) {
  color[u] = GRAY;
  d[u]     = ++tt;
  for (int v = 0; v < n; v++) {
    if (M[u][v] == 0) continue;
    if (color[v] == WHITE) { dfs_visit(v); }
  }
  color[u] = BLACK;
  f[u]     = ++tt;
}

void dfs() {
  tt = 0;

  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE) dfs_visit(u);
  }
  for (int u = 0; u < n; u++) {
    printf("%d %d %d\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, v, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();
}

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

import sys

WHITE = 0
GRAY = 1
BLACK = 2


def dbg_record(records):
    for index, record in enumerate(records):
        print("???????????????: %d" % index)
        if isinstance(record, dict):
            print("??\??????: %d" % record.get("visit", -1))
            print("?????????: %d" % record.get("leave", -1))
        else:
            print("?????????")
        print("")


def dfs(matrix):
    def internal_dfs(node_id, current_time):
        if state[node_id] == WHITE:
            # ?¨??????????
            state[node_id] = GRAY
            stack.append(node_id)
            records[node_id] = {"visit": current_time}  # ?¨??????????????¨????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                return internal_dfs(next_id, current_time + 1)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                return internal_dfs(stack.pop(), current_time + 1)
        elif state[node_id] == GRAY:
            # ????????????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                stack.append(node_id)
                return internal_dfs(next_id, current_time)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time  # ????????????????¨????
                return internal_dfs(stack.pop(), current_time + 1)
            else:  # ??????????§????????????£???????????¨??? (node_id should be 0)
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time
                return current_time
        elif state[node_id] == BLACK:
            print("Black!!")

    state = [WHITE] * len(matrix)  # White, Gray, Black
    records = [None] * len(matrix)  # Save time when the node is processed
    stack = []
    current_time = 0

    while True:
        node_id = None
        for index, record in enumerate(records):
            if record is None:
                node_id = index
                break

        if node_id is None:
            break
        else:
            current_time = internal_dfs(node_id, current_time + 1)

    return records


if __name__ == "__main__":
    # ??????????????????
    # lines = [
    #     "6",
    #     "1 2 2 3",
    #     "2 2 3 4",
    #     "3 1 5",
    #     "4 1 6",
    #     "5 1 6",
    #     "6 0",
    # ]
    lines = sys.stdin.readlines()

    # Create n * n matrix
    dimension = int(lines[0])
    matrix = [[0 for col in range(dimension)] for row in range(dimension)]

    # Set edge info
    for index, line in enumerate(lines[1:]):
        for edge in [int(x) for x in line.strip().split(" ")[2:]]:
            matrix[index][edge - 1] = 1

    records = dfs(matrix)
    for index, line in enumerate(records):
        if line is not None:
            print("%d %d %d" % (index + 1, line.get("visit", -1), line.get("leave", -1)))
        else:
            print("%d None None" % (index + 1))

Python: N = int(input())

G = [[0] * N for _ in range(N)]
time = 0
times = [{'s': 0, 'f': 0} for _ in range(N)]

for _ in range(N):
    u, n, *K = map(int, input().split())
    for k in K:
        G[u - 1][k - 1] = 1

def dfs(u):
    global time
    time += 1
    times[u]['s'] = time
    for i in range(N):
        if G[u][i] == 1 and times[i]['s'] == 0:
            dfs(i)
    else:
        time += 1
        times[u]['f'] = time

for i in range(N):
    if times[i]['s'] == 0:
        dfs(i)

for i, time in enumerate(times):
    print(i + 1, *time.values())

Python: #16D8101014F KurumeRyunosuke 2018/6/20

n=int(input())
ls=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
	tmp=input().split()
	for j in range(int(tmp[1])):
		ls[i][int(tmp[j+2])-1]=1

"""
for i in range(n):
    for j in range(n):
        if(j!=n-1):
            print(int(ls[i][j]), end=' ')
        else:
            print(int(ls[i][j]))
"""
stuck=[0]
count=1
d=[0 for i in range(n)]
f=[0 for i in range(n)]
d[0]=count
for i in range(n):
	ls[i][0]=0

while True:
	count+=1
	p=0
	#print("S",d)
	for i in range(n):
		if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	if p is 0:
		#print("//",count)
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	if len(stuck) is 0:
		t=0
		for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		if t is 0:
			break
		#print("break")
	#print("G",d,f,count)

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

Python: n = int(input())
G = {i:[] for i in range(1, n+1)}
for _ in range(n):
    ukv = list(map(int, input().split()))
    u = ukv[0]
    k = ukv[1]
    for i in range(2, 2+k):
        G[u].append(ukv[i])
        
d = [-1 for _ in range(n+1)]
f = [-1 for _ in range(n+1)]
counter = 0

def dfs(n):
    global counter
    counter += 1
    d[n] = counter
    
    for n_i in G[n]:
        if d[n_i]==-1:
            dfs(n_i)
            
    counter += 1
    f[n] = counter

for i in range(1, n+1):
    if d[i]==-1:
        dfs(i)

for id_i, (d_i, f_i) in enumerate(zip(d[1:], f[1:]), 1):
    print(id_i, d_i, f_i)

Python: def dfs(G, u, t, d, visited):
  found = t
  visited.add(u)
  t += 1
  for v in G[u]:
    if v not in visited:
      t = dfs(G, v, t, d, visited)
  complete = t
  d[u] = (found, complete)
  t+=1
  return t


n = int(input())
G = {}
for _ in range(n):
  entries = list(map(int, input().split(' ')))
  u = entries[0]
  k = entries[1]
  G[u] = set()
  for v in entries[2:]:
    G[u].add(v)

d = {}
visited = set()
t = 1
for i in range(1,n+1):
  if i not in visited:
    t = dfs(G, i, t, d, visited)
for i in sorted(d):
  print(i,d[i][0],d[i][1])