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

"""

input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0

output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

"""
import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2


def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None


def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = map(lambda x: x.split(), _input[1:])

    # config length = (v_num + 1)
    # stack = []
    visited = [UNVISITED] * (v_num + 1)
    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))
    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1
    timer = 1
    dfs_init()

    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print(index, *time_info) """

input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0

output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

""" import sys sys UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 UNVISITED 0 VISITED_IN_STACK 1 POPPED_OUT 2 def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None dfs global timer stack = list() stack list() list stack.append(v_init) stack.append stack append v_init d_time[v_init] += timer d_time[v_init] d_time v_init timer while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

     stack current = stack[-1] current stack[-1] stack -1 v_table = adj_table[current] v_table adj_table[current] adj_table current visited[current] = VISITED_IN_STACK visited[current] visited current VISITED_IN_STACK adj = v_table.pop() if v_table else None adj v_table.pop() if v_table else None v_table v_table.pop() v_table.pop v_table pop None if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

     adj if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
         visited[adj] is UNVISITED visited[adj] visited adj UNVISITED visited[adj] = VISITED_IN_STACK visited[adj] visited adj VISITED_IN_STACK timer += 1 timer 1 d_time[adj] += timer d_time[adj] d_time adj timer stack.append(adj) stack.append stack append adj stack.pop() stack.pop stack pop visited[current] = POPPED_OUT visited[current] visited current POPPED_OUT timer += 1 timer 1 f_time[current] += timer f_time[current] f_time current timer return None None v_init v_init def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1 dfs_init global timer for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1 v range(v_num) range v_num if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1 visited[v + 1] == UNVISITED visited[v + 1] visited v + 1 v 1 UNVISITED dfs(v + 1) dfs v + 1 v 1 timer += 1 timer 1 if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = map(lambda x: x.split(), _input[1:])

    # config length = (v_num + 1)
    # stack = []
    visited = [UNVISITED] * (v_num + 1)
    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))
    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1
    timer = 1
    dfs_init()

    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print(index, *time_info) __name__ == '__main__' __name__ '__main__' _input = sys.stdin.readlines() _input sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines v_num = int(_input[0]) v_num int(_input[0]) int _input[0] _input 0 vertices = map(lambda x: x.split(), _input[1:]) vertices map(lambda x: x.split(), _input[1:]) map lambda x: x.split() x.split() x.split x split x _input[1:] _input 1: 1 visited = [UNVISITED] * (v_num + 1) visited [UNVISITED] * (v_num + 1) [UNVISITED] UNVISITED v_num + 1 v_num 1 [0] * (v_num + 1) for _ in range(2) for _ in range(2) _ range(2) range 2 for _ in range(2) [0] * (v_num + 1) [0] 0 v_num + 1 v_num 1 d_time, f_time = ([0] * (v_num + 1) for _ in range(2)) d_time ([0] * (v_num + 1) for _ in range(2)) f_time adj_table = tuple([] for _ in range(v_num + 1)) adj_table tuple([] for _ in range(v_num + 1)) tuple [] for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1
     v_info vertices v_index, adj_num, *adj_list = map(int, v_info) v_index map(int, v_info) map int v_info adj_num *adj_list adj_list adj_table[v_index].extend(sorted(adj_list, reverse=True)) [v_index].extend [v_index] adj_table v_index extend sorted(adj_list, reverse=True) sorted adj_list reverse=True reverse True timer = 1 timer 1 dfs_init() dfs_init for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print(index, *time_info) index time_info enumerate(zip(d_time[1:], f_time[1:]), 1) enumerate zip(d_time[1:], f_time[1:]) zip d_time[1:] d_time 1: 1 f_time[1:] f_time 1: 1 1 print(index, *time_info) print index *time_info time_info v_num = int(_input[0]) int(_input[0]) v_num VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 1 VISITED_IN_STACK visited = [UNVISITED] * (v_num + 1) [UNVISITED] * (v_num + 1) visited f_time = ([0] * (v_num + 1) for _ in range(2)) ([0] * (v_num + 1) for _ in range(2)) f_time adj_table = tuple([] for _ in range(v_num + 1)) tuple([] for _ in range(v_num + 1)) adj_table timer = 1 1 timer d_time, f_time = ([0] * (v_num + 1) for _ in range(2)) ([0] * (v_num + 1) for _ in range(2)) d_time POPPED_OUT = 0, 1, 2 2 POPPED_OUT vertices = map(lambda x: x.split(), _input[1:]) map(lambda x: x.split(), _input[1:]) vertices def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None dfs def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1 def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1 dfs_init UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 0 UNVISITED _input = sys.stdin.readlines() sys.stdin.readlines() _input adj_num, *adj_list = map(int, v_info) map(int, v_info) adj_num v_index, adj_num, *adj_list = map(int, v_info) map(int, v_info) v_index
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""

input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0

output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

"""
import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2


def dfs(v_init):
    global timer

    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end

    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None

        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer

    return None


def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = map(lambda x: x.split(), _input[1:])

    # config length = (v_num + 1)
    # stack = []
    visited = [UNVISITED] * (v_num + 1)
    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))
    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1
    timer = 1
    dfs_init()

    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print(index, *time_info)