Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[101][101];
int d[101];
int searchPos[101];
int color[101];
int n;
queue<int> q;

/*int nextPoint(int curPoint){
    for(int i=searchPos[curPoint];i<n;++i){
        searchPos[curPoint] += 1;
        if(M[curPoint][i]==1 && color[i] == WHITE){
            return i;
        }
    }
    return -1;
}*/

void bfs(int point){
    q.push(point);
    d[point] = 0;
    color[point] = GRAY;
    int cur, next;
    while(q.size()>0){
        cur = q.front();
        q.pop();
        color[cur] = BLACK;
        for(int i=0;i<n;++i){
            if(M[cur][i] != 0 && color[i] == WHITE){
                q.push(i);
                color[i] = GRAY;
                d[i] = d[cur]+1;
            }
        }
    }
}

int main(){
    int u, z, y;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        searchPos[i] = 0;
        color[i] = WHITE;
        d[i] = 0;
        for(int j=0;j<n;++j){
            M[i][j] = 0;
        }
    }
    for(int i=0;i<n;++i){
        cin >> u >> z;
        for(int j=0;j<z;++j){
            cin >> y;
            M[u-1][y-1] = 1;
        }
    }

    bfs(0);
    for(int i=0;i<n;++i){
        if(color[i]==WHITE){
            // can't reach
            d[i] = -1;
        }
    }
    for(int i=0;i<n;++i){
        printf("%d %d\n",i+1,d[i]);
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[i] = -1;
    }

    color[u] = black;
    d[u] = 0;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

    for(int i=0; i<n; i++){
        cout << i+1 << " " << d[i] << endl;
    }
    
    return 0;
}

C++: #include<stdio.h>

int main()
{
  int n, h, i, j, k, l, m, z, flag, Q[100], Z[100];
  int G[100][100];
  scanf("%d", &n);
  
  for(i = 0; i < 100; i++ ) Z[i] = -1;
  
  for( i = 0; i < n; i++){
    scanf("%d", &G[i][0]);
    scanf("%d", &G[i][1]);
    for( j = 2; j < 2 + G[i][1]; j++){
      scanf("%d", &G[i][j]);
    }
  }
  
  m = 0; 
  h = 0; 
  Q[h] = 1;
  z = 0;
  Z[1] = z;
  z++;
  for( i = 0; i < n; i++){
    flag = 0;
    k = Q[m];
    l = G[k - 1][1];
    
    for( j = 0; j < l; j++){
      if( Z[ G[k - 1][2 + j] ] == -1 ){
        h++;
        Q[h] = G[k - 1][2 + j];
        Z[ G[k - 1][2 + j] ] = z;
        flag = 1;
      }
    }
    if( flag == 1 ) z++;
    m++;
    if( Q[m] == 0 ) i = i + n;
  }
  
  for(i = 1; i <= n ; i++){
    printf("%d %d\n", i, Z[i] );
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#define NUM 100
#define NIL -1
using namespace std;

int V[NUM + 1][NUM + 1];
int dist[NUM + 1];
int node_num;
int t;
int pos;
queue<int> Q;

void init(){
    int V[NUM + 1][NUM + 1];
    for(int i = 1; i <= NUM; i++){
        for(int j = 1; j <= NUM; j++){
            V[i][j] = 0;
        }
    }

    for(int i = 0; i <= NUM; i++) {
        dist[i] = NIL;
    }
    
    t = 0;
}

void get_adj(){
    scanf("%d", &node_num);

    for(int i = 0; i < node_num; i++){
        int num1;
        scanf("%d", &num1);

        int n;
        scanf("%d", &n);

        int num2;
        for(int j = 0; j < n; j++){
            scanf("%d", &num2);
            V[num1][num2] = 1;
        }
    }
}

void touchNextNode(int n){
    printf("before: %d\n", n);
    t += 1;
    for(int i = 1; i <= node_num; i++){
        if(V[n][i] == 1 && dist[i] == NIL){
            printf("i %d\n", i);
            Q.push(i);
            dist[i] = dist[n] + 1;
        }
    }
    Q.pop();
}

void BFS(){
    Q.push(1);
    dist[1] = t;
    touchNextNode(1);
    
    while(Q.size()){
        int pos = Q.front();
        touchNextNode(pos);
    }
}

int main(void){
    init();
    get_adj();
    BFS();
    
    for(int i = 1; i <= node_num; i++){
        printf("%d %d\n", i, dist[i]);
    }

}

C++: #include <iostream>
#include <vector>
#include <numeric>
#include <cassert>
#include <queue>

const int MAXINT = 1000000000;

void bfs(std::vector<std::vector<int> >& adj_list, std::vector<int>& d, int root = 0 ){
  std::queue<int> target_q;
  // std::queue<int> dist_q;
  target_q.push(root);
  d[root] = 0;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      target_q.push(u);
      if(d[vtx] + 1 < d[u]){
        // std::cout<< "/* message */" << std::endl;
        d[u] = d[vtx] + 1;
      }
    }
  }
}


int main(int argc, char const *argv[]) {
  int n; // .. 100
  std::cin >> n;
  std::vector<std::vector<int> > adj_list(n);
  std::vector<int> d(n, MAXINT);

  for(int q=0; q<n; ++q){
    int i_plus_1, k;
    std::cin >> i_plus_1;
    std::cin >> k;
    for(int p=0; p<k; ++p){
      int j_plus_1;
      std::cin >> j_plus_1;
      // adj[i_plus_1-1][j_plus_1-1] = true;
      adj_list[i_plus_1 - 1].push_back(j_plus_1-1);
    }
  }
  bfs(adj_list, d, 0);

  for(int i=0; i<n; ++i){

    // if(d[i] < MAXINT){
      std::cout << (i+1)
        << " " << (d[i]==MAXINT ? -1 :: d[i])
        << std::endl;
  }


  // for(auto it = adj_list.begin(); it != adj_list.end(); ++it){
  //   std::cout << ": ";
  //   for(auto jt = (*it).begin(); jt != (*it).end(); ++jt){
  //     std::cout << (*jt) << ' ';
  //   }
  //   std::cout << std::endl;
  // }

  return 0;
}