Language: C++

#include <iostream>
#include <fstream>
#include <cassert>
#include <string.h>
#include <vector>
#include <map>
#include <stack>

enum Color
{
    kWhite,
    kGray,
    kBlack,
};

// 頂点数.
int n;

// 各頂点の隣接リスト表現.
std::map< int, std::vector<int> > adj_list;

// 各頂点の訪問状態を管理する.
std::vector<Color> c;

// 初回訪問時.
std::vector<int> d;

// 二回目訪問時.
std::vector<int> f;

void makeAdjListFromInput(int n)
{
    for (int i = 0; i < n; ++i)
    {
        int vertex;
        int degree;

        std::cin >> vertex;
        std::cin >> degree;

        assert(vertex <= n);
        assert(degree <= n);

        std::vector<int> adj_vertexes;

        for (int j = 0; j < degree; ++j)
        {
            int adj;
            std::cin >> adj;
            adj_vertexes.push_back(adj - 1);
        }

        adj_list[vertex - 1] = adj_vertexes;
    }
}

int nextVertex(int current)
{
    std::vector< int > adjs = adj_list[current];
    for (std::vector< int >::iterator it = adjs.begin(); it != adjs.end(); ++it)
    {
        if (c[(*it)] == kWhite)
            return (*it);
    }

    // 見つからなかった.
    return INT_MAX;
}

int firstVertex()
{
    for (int i = 0; i < n; ++i)
    {
        if (c[i] == kWhite)
            return i;
    }

    return INT_MAX;
}

void depthFirstSearch()
{
    int counter = 0;
    std::stack<int> tmp_stack;

    // 諸々初期化.
    for (int i = 0; i < n; ++i)
    {
        c.push_back(kWhite);
        d.push_back(0);
        f.push_back(0);
    }

    while (true)
    {
        int first = firstVertex();
        if (first == INT_MAX)
            break;
        tmp_stack.push(first);
        c[first] = kGray;
        d[first] = ++counter;

        while (! tmp_stack.empty())
        {
            int current = tmp_stack.top();
            int next = nextVertex(current);
            if (next != INT_MAX)
            {
                tmp_stack.push(next);
                c[next] = kGray;
                d[next] = ++counter;
            }
            else
            {
                int pop = tmp_stack.top();
                tmp_stack.pop();
                c[pop] = kBlack;
                f[pop] = ++counter;
            }
        }
    }
}

int main()
{
    std::cin >> n;

    makeAdjListFromInput(n);

    depthFirstSearch();

    for (std::map< int, std::vector<int> >::iterator it = adj_list.begin(); it != adj_list.end(); ++it)
    {
        int v = it->first;
        std::cout << v + 1 << " " << d[v] << " " << f[v] << std::endl;
    }

    return 0;
}






Compiler output:
a.cc: In function 'int nextVertex(int)':
a.cc:67:12: error: 'INT_MAX' was not declared in this scope
   67 |     return INT_MAX;
      |            ^~~~~~~
a.cc:8:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    7 | #include <stack>
  +++ |+#include <climits>
    8 | 
a.cc: In function 'int firstVertex()':
a.cc:78:12: error: 'INT_MAX' was not declared in this scope
   78 |     return INT_MAX;
      |            ^~~~~~~
a.cc:78:12: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'void depthFirstSearch()':
a.cc:97:22: error: 'INT_MAX' was not declared in this scope
   97 |         if (first == INT_MAX)
      |                      ^~~~~~~
a.cc:97:22: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:107:25: error: 'INT_MAX' was not declared in this scope
  107 |             if (next != INT_MAX)
      |                         ^~~~~~~
a.cc:107:25: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'