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;

int main(){
        vector<int> v[1005];
        bool used[1005];
        int dis[1005];
        memset(used, 0, sizeof(used));
        memset(dis, -1, sizeof(dis));
        int N ; cin >> N;
        for(int j = 0; j < N; j++){
            int id, t, tmp; cin >> id >> t;
            for (int i = 0; i < t; i++){
                cin >> tmp;
                v[id].push_back(tmp);
                //v[tmp].push_back(id);
            }
        }
        queue<int> q;
        q.push(1);
        used[1] = 1;
        dis[1] = 0;
        while(!q.empty()){
            int k = q.front();
            q.pop();
            for (int x : v[k]){
                if(!used[x]){
                    q.push(x);
                    dis[x] = dis[k] + 1;
                    used[x] = 1;
                }
            }
        }
        for (int i = 1; i <= N; i++)
            cout << i << " " << dis[i] << '\n';

}

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

using Graph = std::vector<std::vector<int>>;

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

    Graph g(n);
    for(int i = 0; i < n; ++i){
        int u, k;
        std::cin >> u >> k;
        --u;
        for(int j = 0; j < k; ++j){
            int v;
            std::cin >> v;
            --v;
            g.at(u).push_back(v);            
            //g.at(v).push_back(u); 
        }
    }

    std::vector<int> dist(n, -1);
    std::queue<int> queue;
    dist.at(0) = 0;
    queue.push(0);

    while(!queue.empty()){
        int v = queue.front();
        queue.pop();

        for(auto nextV : g.at(v)){
//            if(-1 != dist.at(nextV)){
//                continue;
//            }
//            queue.push(nextV);
//            dist.at(nextV) = dist.at(v) + 1;
            if(-1 == dist.at(nextV)){
                queue.push(nextV);
                dist.at(nextV) = dist.at(v) + 1;
            }
        }
    }

    for(int i = 0; i < n; ++i){
        std::cout << i + 1 << " " << dist.at(i) << std::endl;
    }

    return 0;
}

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]);
    d[0][i]=min(d[0][i],d[0][j]+d[i][j]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ';
   if(d[0][i]==100000){
     cout << -1 << endl;
   }else{
    cout << d[0][i] << endl;
   }
 }
 
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1 << 21;
const ll MOD = 1e9 + 7;
static const int N = 100;

int n;
int M[N][N];
int d[N]; // 距離で訪問状態を管理する

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    rep(i, n)
    {
        d[i] = INF;
    }
    d[s] = 0;
    int u;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        rep(v, n)
        {
            if (M[u][v] == 0) { // u->vに行けないなら飛ばす
                continue;
            }
            if (d[v] != INF) { // d[v]に行ったことがあるなら飛ばす
                continue;
            }
            d[v] = d[u] + 1; // u->vが始めてなら距離を更新
            q.push(v); // queueにvを入れる
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int u, k, v;
    cin >> n;
    rep(i, n)
    {
        rep(j, n)
        {
            M[i][j] = 0;
        }
    }
    rep(i, n)
    {
        cin >> u >> k;
        u--;
        rep(j, k)
        {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0); // 0から出発

    return 0;
}

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

int n,m,k,l,a[101][101],d[101],t;
queue<int> q;

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>l>>m;
        for(int j=0;j<m;j++){
            cin>>k;
            a[l][k]=1;
        }
        d[i+1]=-1;
    }
    d[1]=0;
    q.push(1);
    while(!q.empty()){
        t=q.front();
        q.pop();
        for(int i=1;i<=n;i++){
            if(a[t][i]){
                if(d[i]==-1){
                    d[i]=d[t]+1;
                    q.push(i);
                }
            }
        }
    }
    for(int i=1;i<=n;i++){cout<<i<<' '<<d[i]<<endl;}
}