Language: C++

/**
 * AOJコンテスト： 
 *  深さ優先探索：Depth First Search
 *
 *  計算量：O(n + Σk)
 *
 *  入力例：
 *  u k v1 v2 ... vk
 *  (※u：頂点の番号)
 *  (※k：出次数)
 *  (※vi：uに隣接する頂点の番号)
 *
    4
    1 1 2
    2 1 4
    3 0
    4 1 3
 *
 *  出力例：
 *  id d f
 *  (※id：頂点の番号)
 *  (※d：頂点発見時刻)
 *  (※f：頂点完了時刻)
    1 1 8
    2 2 7
    3 4 5
    4 3 6
 */


#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;


enum VisitColor {
  //未訪問
  WHITE,
  //訪問のみ未完了
  GRAY,
  //完了
  BLACK,
};

static const int kNotExist = -1;

int getNext(const vector<vector<int>>& matrixAdjacency,vector<int>* current_time,int apex_num,int current_id) {
  for (int t = (*current_time)[current_id]; t < apex_num; ++t) {
    (*current_time)[current_id] = t + 1;
    //隣接先を見受けたらその要素番号を返す
    if (matrixAdjacency[current_id][t]) return t;
  }
  //隣接先がない場合
  return kNotExist;
}


void Dfs(const vector<vector<int>>& matrixAdjacency,const int apex_num, vector<pair<int,int>>* time_vector) {
  //未訪問を白で初期化
  vector<int> color(apex_num,WHITE);
  vector<int> current_time(apex_num,0);
  //経過時間
  int elapsed_time = 0;
  int root_id = 0;

  stack<int> stack_id;
  stack_id.push(root_id);

  //初訪問時の設定 WHITE~GRAY,発見時刻の登録
  color[root_id] = GRAY;
  (*time_vector)[root_id].first = ++elapsed_time;

   //未訪問の地（白の場所）を訪れ、GRAY(発見)またはBLACK(完了)に変更する
  for (int id=0; id<apex_num; ++id) {
    if (color[id] == WHITE) {
      while (!stack_id.empty()) {
        /*
        * スタックに含まれる要素番号の推移
        * 0 1 3 2 - 0 1 3 - 0 1 - 0 - 空
        */
        int idxNow = stack_id.top();
        int idxNowNext = getNext(matrixAdjacency, &current_time, apex_num, idxNow);
        //子がいない場合
        if (idxNowNext == kNotExist) {
          stack_id.pop();
          color[idxNow] = BLACK;
          (*time_vector)[idxNow].second = ++elapsed_time;
        }
        //子がまだいる場合    
        else {
          if (color[idxNowNext] == WHITE) {
            color[idxNowNext] = GRAY;
            (*time_vector)[idxNowNext].first = ++elapsed_time;
            stack_id.push(idxNowNext);
          }
        }
      }
    }
  }
}  

void CreateMatrixAdjacency(vector<vector<int>>* matrixAdjacency, int apex_num){
  for (int i = 0; i < apex_num; i++) {
    int apex_id = 0;
    int adj_num = 0;
    cin >> apex_id >> adj_num;
    apex_id--;
    for (int j = 0; j < adj_num; j++) {
      int adj_id = 0;
      cin >> adj_id;
      adj_id--;
      (*matrixAdjacency)[apex_id][adj_id] = 1;
    }
  }
}

int main() {
  
  int apex_num;
  cin >> apex_num;
  vector<vector<int>> matrixAdjacency(apex_num, vector<int>(apex_num));

	CreateMatrixAdjacency(&matrixAdjacency,apex_num);
  
  //first:find_time, second:finish_time
  vector<pair<int,int>> time_vector(apex_num);
  Dfs(matrixAdjacency,apex_num, &time_vector);

  for (int i = 0; i < apex_num; i++) {
    cout << i + 1 << " " << time_vector[i].first << " " << time_vector[i].second << endl;
  }
  return 0;
}
/**
 *  AOJコンテスト：
 *  幅優先探索：Breadth First Search
 *
 *  計算量：O(apex_num + Σk)
 *
 *  入力例：
 *  u k v1 v2 ... vk
 *  (※u：頂点の番号)
 *  (※k：出次数)
 *  (※vi：uに隣接する頂点の番号)
 *
    4
    1 2 2 4
    2 1 4
    3 0
    4 1 3
 *
 *  出力例：
 *  id d
 *  (※id：頂点の番号)
 *  (※d：頂点1からその頂点までの距離)
    1 0
    2 1
    3 2
    4 1
 */


#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

//未登録
static const int kNotRegister = -1;

void Bfs(const vector<vector<int>>& matrixAdjacency, const int apex_num, const int idx_root, vector<int>* dst) {
  queue<int> que;
  que.push(idx_root);
  (*dst)[idx_root] = 0;

  while (!que.empty()) {
    /*
     * キューに含まれる要素番号の推移
     * 0 - 空 - 1 - 1 3 - 3 - 2
     */
    int idx_now = que.front();
    que.pop();
    /*
     * idxNowの行内に隣接先がある場合にpushする
     */
    for (int idx_row = idx_root+1; idx_row < apex_num; idx_row++) {
      //隣接先でないもしくは、一度登録したことがある場合は continue
      if ((matrixAdjacency[idx_now][idx_row] == 0) || ((*dst)[idx_row] != kNotRegister)) continue;
      (*dst)[idx_row] = (*dst)[idx_now] + 1;
      que.push(idx_row);
    }
  }
}

/**
 * matrixAdjacency
 * [0]:0 1 0 1
 * [1]:0 0 0 1
 * [2]:0 0 0 0
 * [3]:0 0 1 0
 * 
 */ 
void CreateMatrixAdjacency(vector<vector<int>>* matrixAdjacency, const int apex_num){
	int apex_id, adj_num, adj_id;
  for (int i = 0; i < apex_num; i++) {
    cin >> apex_id >> adj_num;
    apex_id--;
    for (int j = 0; j < adj_num; j++) {
      cin >> adj_id;
      adj_id--;
      (*matrixAdjacency)[apex_id][adj_id] = 1;
    }
  }
}

int main() {
	int apex_num;
  cin >> apex_num;
  /*隣接行列作成*/
  vector<vector<int> > matrixAdjacency(apex_num, vector<int>(apex_num));
  /*隣接行列作成*/
  CreateMatrixAdjacency(&matrixAdjacency, apex_num);

  vector<int> dst(apex_num, kNotRegister);
  Bfs(matrixAdjacency,apex_num,0, &dst);
  
  for (int i = 0; i < apex_num; i++) {
    cout << i + 1 << " ";
    if(dst[i] == kNotRegister){
      cout << -1 << endl;
    }
    else{
      cout << dst[i] << endl;
    }
  }

  return 0;
}

Compiler output:
a.cc:206:6: error: redefinition of 'void CreateMatrixAdjacency(std::vector<std::vector<int> >*, int)'
  206 | void CreateMatrixAdjacency(vector<vector<int>>* matrixAdjacency, const int apex_num){
      |      ^~~~~~~~~~~~~~~~~~~~~
a.cc:104:6: note: 'void CreateMatrixAdjacency(std::vector<std::vector<int> >*, int)' previously defined here
  104 | void CreateMatrixAdjacency(vector<vector<int>>* matrixAdjacency, int apex_num){
      |      ^~~~~~~~~~~~~~~~~~~~~
a.cc:219:5: error: redefinition of 'int main()'
  219 | int main() {
      |     ^~~~
a.cc:119:5: note: 'int main()' previously defined here
  119 | int main() {
      |     ^~~~