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: N = 100
WHITE = 0
GRAY = 1
BLACK = 2

d = [-1] * N
f = [-1] * N

def dfs_visit(n, u, tt, color):
    global d
    global f
    color[u] = GRAY
    tt += 1
    d[u] = tt
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            tt = dfs_visit(n, v, tt, color)
    color[u] = BLACK
    tt += 1
    f[u] = tt
    return tt

def dfs(n):
    global d
    global f
    color = []
    for u in range(n):
        color.append(WHITE)
    tt = 0

    for u in range(n):
        if color[u] == WHITE:
            tt = dfs_visit(n, u, tt, color)
    
    for u in range(n):
        print(u+1, d[u], f[u])

n = int(input())

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

for i in range(n):
    I = list(map(int, input().split()))
    u = I[0] - 1
    k = I[1]
    for j in range(k):
        v = I[j+2] - 1
        M[u][v] = 1

#print(M)

dfs(n)

Python: n = int(input())

m = [[0 for i in range(n)] for j in range(n)]

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

color = ["white" for i in range(n)]

d = [0 for i in range(n)]
f = [0 for i in range(n)]
time = 0

def dfs(u):
    global time
    time += 1
    color[u] = "gray"
    d[u] = time
    for i in range(u, n):
        if m[u][i] and color[i] == "white":
            dfs(i)
    color[u] = "black"
    time += 1
    f[u] = time

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

Python: import sys
sys.setrecursionlimit(2000000)

n = int(input())
m = [list(map(int, input().split()))[2:] for _ in range(n)]

def dfs(x, time):
    first[x] = time
    time += 1
    for v in m[x]:
        if first[v-1] == 0:
            time = dfs(v-1, time)
    finish[x] = time
    time += 1
    return time

first = [0] * n
finish = [0] * n

time = 1
for i in range(n):
    if first[i] == 0:
        time = dfs(i, time)

for i, xy in enumerate(zip(first, finish)):
    print(i+1, *xy)

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
while True:
	count+=1
	p=0
	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

	if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0
	if p is 0:
		#print("//")
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0

	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")

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

Python: import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(input())

adj = [[] for _ in range(N)]

for i in range(N):
    line = list(map(int, input().split()))
    u = line[0] - 1
    if line[1] == 0:
        continue
    vs = line[2:]
    for v in vs:
        adj[u].append(v - 1)

d = [-1] * N
f = [-1] * N

def dfs(v, t):
    d[v] = t
    nx = t + 1
    for u in adj[v]:
        if d[u] > -1:
            continue
        nx = dfs(u, nx)
    f[v] = nx
    return f[v] + 1

nx = 1
for i in range(N):
    if d[i] > - 1:
        continue
    nx = dfs(i, nx)

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

Python: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def walk(index, tm, L, d, f):
    tm += 1
    d[index] = tm
    for i in L[index]:
        if not d[i-1]:
            tm = walk(i-1, tm, L, d, f)
    tm += 1
    f[index] = tm
    return tm


def main():
    num = int(stdin.readline())

    L = []
    for _ in xrange(num):
        L.append([int(s) for s in stdin.readline().split()[2:]])

    d = [0] * num
    f = [0] * num
    tm = 0
    for i in xrange(num):
        if not d[i]:
            tm = walk(i, tm, L, d, f)

    for i in xrange(num):
        print(i+1, d[i], f[i])


main()

Python: n = int(input())
G = [[] for _ in range(n)]
u1 = 0
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u

time = 1
u = u1
D = []
F = []
out = [[i+1, 0, 0] for i in range(n)]
T = []
while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for f in F:
        if f in G[u - 1]:
            idx = G[u - 1].index(f)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        F.append(u)
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        if G[u - 1][0] not in D:
            time += 1
        u = G[u - 1].pop(0)

for ni in range(n):
    print(*out[ni])

Python: class Vertex:
    _id = -1
    _children = []
    _visited = False
    _visited_time = 0
    _all_visited_time = 0

    def __init__(self, i, c):
        self._id = i
        self._children = c

    def getId(self):
        return self._id

    def getChildren(self):
        return self._children

    def isVisited(self):
        return self._visited

    def getVisitedTime(self):
        return self._visited_time

    def setVisitedTime(self, t):
        self._visited_time = t
        self._visited = True

    def getAllVisitedTime(self):
        return self._all_visited_time

    def setAllVisitedTime(self, t):
        self._all_visited_time = t

    def print(self):
        print("{} {} {}".format(self._id, self._visited_time, self._all_visited_time))


n = int(input())
graph = [0 for i in range(n)]
time = 0

# 頂点登録
for i in range(n):
    u = list(map(int, input().split(' ')))
    adj = []
    if u[1] > 0:
        adj = u[2:u[1]+2]
    graph[u[0]-1] = Vertex(u[0], adj)

# DFS
def dfs(v):
    global time
    time += 1
    v.setVisitedTime(time)
    for child in v.getChildren():
        if not graph[child-1].isVisited():
            dfs(graph[child-1])
    time += 1
    v.setAllVisitedTime(time)

# DFS実行
for v in graph:
    if not v.isVisited():
        dfs(v)

# 回答を出力
for v in graph:
    v.print()