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 <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == check_id) return -1;
	else check_id = id;

	if (id == this->u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  
  int n,a,b,c,ans[101];
  vector<int> vec[101];
  queue<int> que;
  
  fill(ans,ans+101,105);
  ans[1] = 0;
  
  cin >> n;
  for(int i=0;i<n;i++){ 
    cin >> a >> b;
    for(int j=0;j<b;j++){
      cin >> c;
      vec[a].push_back(c);
    }
  }

  que.push(1);
  
  while(!que.empty()){
    
    int d = que.front();
    que.pop();
    
    for(int i=0;i<vec[d].size();i++){
      int e = vec[d][i];
      if(ans[e] > ans[d]+1){
	ans[e] = ans[d]+1;
	que.push(e);
      }
    }
    
  }
  for(int i=1;i<=n;i++){
    cout << i << ' ' ;
    if(ans[i]==105) cout << -1 << endl;
    else cout << ans[i] << endl;
  }
  return (0);
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N];

void bfs( int s ){
  queue<int> q;
  q.push(s);
  for( int i = 0 ; i < n ; i++ ){
    d[i] = INFTY;
  }
  d[s] = 0;int u;
  while( !q.empty() ){
    u = q.front();
    q.pop();
    for( int v = 0 ; v < n ; v++ ){
      if( M[u][v] == 0 )continue;
      if( d[v] != INFTY )continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for( int i = 0 ; i < n ; i++ ){
    cout << i + 1 << " " <<((d[i] == INFTY) ? ( -1 ) : d[i]) << endl;
  }
}

int main( void ){
  int u,k,v;
  cin >> n:
  for( int i = 0 ; i < n ; i++ ){
    for( int j = 0 ; j < n ; j++ ){
      M[i][j] = 0;
    }
  }

  for( int i = 0 ; i < n ; i++ ){
    cin >> u >> k;
    u--;
    for( int j = 0 ; j < k ; j++ ){
      cin >>v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int n,M[N][N];
int d[N];//通过距离管理访问状态(color)

void bfs(int s){
    queue<int> q;//使用标准库中的queue
    q.push(s);
    for(int i=0; i<n; i++) d[i] = INFTY;
    d[s] = 0;
    int u;
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(int v=0; v<n; v++){
            if(M[u][v]==0) continue;
            if(d[v]!=INFTY) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }

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



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

    cin>>n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++) M[i][j] = 0;
    }

    for(int i=0; i<n; i++){
        cin>>u>>k;
        u--;
        for(int j=0; j<k; j++){
            cin>>v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
static const int N = 100;

int n;
int M[N][N];
int vis[N];
int seq[N];

void bfs_print() {
	for (int i = 0; i < n; i++) {
		cout << i + 1 << ' ' << seq[i]<<endl;
	}
}

int main() {
	cin >> n;
	int u, k;
	int index;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			M[i][j] = 0;
		}
	}

	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> index;
			M[u - 1][index - 1] = 1;
		}
	}
	for (int i = 0; i < n; i++) {
		seq[i] = 0;
		vis[i] = 0;
		if(i >0) seq[i] = -1;
	}

	queue<int> q;
	seq[0] = 0;
	int t;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (M[i][j] == 1) {
				q.push(j);
			}
		}
		cout << endl;
		while (not q.empty()) {
			if (vis[q.front()] == 0) {
				seq[q.front()] = seq[i] + 1;
				vis[q.front()] = 1;
			}
			q.pop();
		}
	}

	bfs_print();
}
//
//void dfs_visit(int u) {
//	color[u] = GRAY;
//	d[u] = ++tt;
//	for (int v = 0; v < n; v++) {
//		if (M[u][v] == 1 && color[v] == WHITE) {
//			dfs_visit(v);
//		}
//	}
//	color[u] = BLACK;
//	f[u] = ++tt;
//}
//
//void dfs_print() {
//	for (int i = 0; i < n; i++) color[i] = WHITE;
//	tt = 0;
//	for (int i = 0; i < n; i++) {
//		if (color[i] == WHITE) {
//			dfs_visit(i);
//		}
//	}
//
//	for (int i = 0; i < n; i++) {
//		cout << i + 1 << ' ' << d[i] << ' ' << f[i] << endl;
//	}
//}
//

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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i=0; i<N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j=0; j<n; j++) {
      int t;
      cin >> t; t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int> > que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front(); que.pop();
    int x = tmp.first, depth = tmp.second;
    if (N < depth) continue;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      ans[x] = min(ans[x], depth);
    }
    for (int i=0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

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