Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 1
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	g := graph{}
	g.ee = make([]Edges, n)
	g.visited = make([]bool, n)
	g.found = make([]int, n)
	g.leave = make([]int, n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner) - 1
		k := getNextInt(scanner)
		for j := 0; j < k; j++ {
			v := getNextInt(scanner) - 1
			g.ee[u] = append(g.ee[u], Edge{
				to: v,
				id: u,
			})
		}
	}
	for i := 0; i < n; i++ {
		sort.Sort(g.ee[i])
	}
	for i := 0; i < n; i++ {
		if g.visited[i] {
			continue
		}
		g.dfs(i)
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d %d", i+1, g.found[i], g.leave[i]))
	}
}

// Edge ...
type Edge struct {
	to, id int
}

type Edges []Edge

func (a Edges) Len() int           { return len(a) }
func (a Edges) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Edges) Less(i, j int) bool { return a[i].id < a[j].id }

type graph struct {
	ee           []Edges
	t            int
	n            int
	visited      []bool
	found, leave []int
}

func (g *graph) dfs(i int) {
	g.visited[i] = true
	g.t++
	g.found[i] = g.t
	for _, e := range g.ee[i] {
		if g.visited[e.to] {
			continue
		}
		g.dfs(e.to)
	}
	g.t++
	g.leave[i] = g.t
}

C++: #include <bits/stdc++.h>
using namespace std;
bool A[105][105],vist[105]={0};
int d[105],f[105],n,t=0;

void dfs(int v){
	d[v] = ++t;
	vist[v] = 1;
	for(int i=1;i<=n;i++)
		if(A[v][i] && !vist[i])		
			dfs(i);
	f[v] = ++t;  
}
int main()
{	
	memset(A,0,sizeof(A));
	int v,u,du;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>v>>du;
		while(du--)
			cin>>u,
			A[v][u] = 1;
	}
	for(int i=1;i<=n;i++)
		if(!vist[i]) dfs(i);
	for(int i=1;i<=n;i++)
		printf("%d %d %d\n",i,d[i],f[i]);
	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
 
#define WHITE 10000
#define BLACK 10001
#define GRAY 10002
#define TRUE 1
#define FALSE 0
#define NUL NULL
 
void dfs(void);
void visit(int);
void push(int);
void pop(void);
int isEmpty(void);
 
int v[101][101],s[101],time1,n;
int *d,*f,*color;
int top = 0;
 
int main(){
  int u,i,j,w,k;
 
  scanf("%d",&n);
 
  d = malloc(sizeof(int) * (n + 1));
  f = malloc(sizeof(int) * (n + 1));
  color = malloc(sizeof(int) * (n + 1));
 
  for(i = 1;i <= n;i++){
    scanf("%d %d",&u,&k);
    for(j = 1;j <= k;j++){
      scanf("%d",&w);
      v[i][w] = 1;
    }
  }
 
  dfs();
 
  for(i = 1;i <= n;i++){
    printf("%d %d %d\n",i,d[i],f[i]);
  }
 
  return 0;
}
 
void dfs(){
  int i = 1,j = 1;
  for(i = 1;i <= n;i++){
      color[i] = WHITE;
  }
 
  time1 = 0;
 
  for(i = 1;i <= n;i++){
    if(color[i] == WHITE){
      visit(i);
    }
  }    
 
}
 
void visit(int r){
  int u,vv=NUL,i;
 
  push(r);
  color[r] = GRAY;
  d[r] = ++time1;
 
  while(isEmpty() == FALSE){
    vv=NUL;
    u = s[top];
    for(i = 1;i <= n;i++){
    if(v[u][i] == 1 && color[i] == WHITE){
      vv = i;
      break;
    }
     
    }
    if(vv != NUL){
      if(color[vv] == WHITE){
    push(vv);
    color[vv] = GRAY;
    d[vv] = ++time1;
      }
    }
    else{
      pop();
      color[u] = BLACK;
      f[u] = ++time1;
    }
  }
}
 
void push(int x){
  top++;
  s[top] = x;
}
 
int isEmpty(){
  if(top == 0){
    return TRUE;
  }
  else{
    return FALSE;
  }
}
 
void pop(){
  top--;
}

C++: #include <stdio.h>
#define N 100
#define W 0
#define X 1
#define Z 2

int n,m[N][N];
int c[N],d[N],f[N],tt;

void dfs_visit(int u){
  int i;
  c[u] = X;
  ++tt;
  d[u] = tt;
  for(i=0;i<n;i++){
    if(m[u][i] == 0) continue;
    else if(c[i]==W){
      dfs_visit(i);
    }
  }
  c[u]=Z;
  ++tt;
  f[u]=tt;
}

void dfs(){
  int i;
  for(i=0;i<n;i++){
    c[i] = W;
  }
  tt = 0;

  for(i=0;i<n;i++){
    if(c[i]==W)dfs_visit(i);
  }
  for(i = 0 ;i<n;i++){
    printf("%d %d %d\n", i+ 1,d[i],f[i]);
  }
}

int main(){
  int u,v,k,i,j;

  scanf("%d",&n);
  for(i = 0;i<n;i++){
    for(j=0;j<n;j++)m[i][j]=0;
  }

  for(i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      m[u][v]=1;
    }
  }
  dfs();

  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
stack<int> st;
int  v[102][101], n;
int  time[102][2];
int ct = 1;
inline void search(int t) {
	if(!time[t][0]) time[t][0] = ct++;
	int tf = 0;
	for (int a = 1; a <= n; a++) {
		if (v[t][a]) {
			st.push(a);
			for (int i = 1; i <= n; i++) {
				v[i][a] = 0;
			}
			search(st.top());
			tf = 1;
		}
	}
		
	if(!tf) {
		st.pop();
		time[t][1] = ct++;
		if(!st.empty()) search(st.top());
	}
}
int main() {
	for (int i = 1; i <= n; i++) {
		time[i][0] = 0;
	}
	st.push(1);
	int u, k, s;
	cin >> n;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			v[i][j] = 0;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u;
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> s;
			v[u][s] = 1;
		}
	}
	search(1);
	for (int i = 1; i <= n; i++) {
		cout << i << " " << time[i][0] << " " << time[i][1] << endl;
	}
	return 0;
}

C++: n=int(input())

l=[[0 for i in range(n)] for j in range(n)]##静的確保 隣接行列
d=[0 for i in range(n)]##発見
f=[0 for i in range(n)]##完了
stack=[]##スタック
check=[]

for i in range(n):
    p=[int(x) for x in input().split()]
    
    for j in range(n):
        if j+1 in p[2:]:
            l[i][j]=1
##print("---隣接行列---")
##print(l)

time=1
d[0]=1
stack+=[1]
check+=[-1]

while len(stack)!=0:
    ##print("----------while---------")
    for i in range(n):
        if l[stack[-1]-1][i]==1 and not i+1 in check:
            time+=1
            d[i]=time
            stack+=[i+1]

           ## print("stack:",stack)
           ## print("d[",i,"]:",d[i])
            break
        elif i==n-1:
##最深    
            check+=[stack[-1]]
            time+=1
            f[stack.pop()-1]=time

            ##print("f[",i,"]:",f[i])
            ##print("check:",check)
            


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