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<iostream>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

int main() {
	int n, u, k, v;
	cin >> n;
	vector<int> ans(n,1000000000);
	vector<vector<int>> adj(n);
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		while (k--)cin >> v, adj[u-1].push_back(v-1);
	}
	queue<pair<int, int>> q;//<idx,path>
	q.push(pair<int, int>(0, 1));
	while (!q.empty()) {
		auto tmp = q.front(); q.pop();
		ans[tmp.first] = min(ans[tmp.first], tmp.second);
		if (ans[tmp.first] != tmp.second)continue;
		for (int i = 0; i < adj[tmp.first].size(); i++) q.push(pair<int, int>(adj[tmp.first][i], tmp.second+1));
	}
	for (int i = 0; i < n; i++)	cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i]-1) << endl;
	
}

C++: #include <iostream>
#include <queue>

using namespace std;

int D[100100];//始点からの距離を入れる
int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  for (int i = 0; i < n; ++i)
  {
    cout << i+1 <<" "<<D[i]<<endl;
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}

C++: #include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> E;

int INF = -1;

int main() {
  int n, u, k;
  cin >> n;
  int** G = new int*[n+1];
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    G[u] = new int[k+1];
    G[u][0] = k;
    for (int j = 1; j <= k; j++) {
      cin >> G[u][j];
    }
  }

  int cnt = 0;
  int* visit = new int[n+1];
  for (int i = 1; i <= n; i++) {
    visit[i] = INF;
  }

  queue<E> q;
  q.push(E(1, cnt));
  while (q.size()) {
    E e = q.front();
    q.pop();
    if (visit[e.first] != INF)
      continue;
    visit[e.first] = e.second;

    int len = G[e.first][0];
    for (int i = 1; i <= len; i++) {
      q.push(E(G[e.first][i], e.second + 1));
    }
  }

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

C++: #include <iostream>
#include <queue>

using namespace std;

const int no_edge = -1;
const int edge = 1;
const int first_visit_label = 1;
const int no_distance = -1;

int main() {
  int vertex_num;
  cin >> vertex_num;

  vector<vector<int>> matrix_graph(
    vertex_num,
    vector<int>(vertex_num, no_edge)
  );

  for(int i = 0; i < vertex_num; i++) {
    int vertex_label;
    int adjacency_num;
    cin >> vertex_label >> adjacency_num;
    for(int j = 0; j < adjacency_num; j++) {
      int adjacency_label;
      cin >> adjacency_label;
      matrix_graph[vertex_label - 1][adjacency_label - 1] = edge;
    }
  }

  queue<int> will_visit_labels;
  vector<int> distances(vertex_num, no_distance);
  distances[first_visit_label - 1] = 0;
  will_visit_labels.push(first_visit_label);

  while(!will_visit_labels.empty()) {
    int current_label = will_visit_labels.front();
    will_visit_labels.pop();

    for(int i = 0; i < vertex_num; i++) {
      int will_visit_label = i + 1;
      if(matrix_graph[current_label - 1][will_visit_label - 1] == no_edge) {
        continue;
      }
      if(distances[will_visit_label - 1] != no_distance) {
        continue;
      }
      distances[will_visit_label - 1] = distances[current_label - 1] + 1;
      will_visit_labels.push(will_visit_label);
    }
  }

  for(int i = 0; i < vertex_num; i++) {
    cout << i + 1 << ' ' << distances[i] << endl;
  }
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ' <<d[0][i] << endl;
 }
 
  return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = -1;

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()
{
	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;
}