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;

vector<int> bfs(
    int p,
    const vector<vector<int>> graph)
{
    queue<int> Q;
    vector<bool> status(graph.size());
    vector<int> distance(graph.size());
    Q.push(p);
    status[p] = true;
    while (!Q.empty())
    {  
        int k = Q.front();
        Q.pop();
        for (const auto& q : graph[k]) {
            if (!status[q]) {
                Q.push(q);
                status[q] = true;
                distance[q] = distance[k]+1;
            }
        }
    }
    for (int i = 0; i < status.size(); ++i) {
        if (status[i] == 0) {
            distance[i] = -1;
        }
    }
    return distance;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    for (auto&& g : graph) {
        int id;
        cin >> id;
        int size;
        cin >> size;
        g.reserve(size);
        for (int j = 0; j < size; ++j) {
            int k; 
            cin >> k;
            g.push_back(k-1);
        }
    }
    auto result = bfs(0, graph);
    for (int i = 0; i < result.size(); ++i) {
        cout << i+1 << " " << result[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------


int main() {
   int n;
   cin>>n;
   vi ans(n,-1);
   vi g[n];
   rep(i,n){
       int h,k;
       cin>>h>>k;
       rep(j,k){
           int t;
           cin>>t;
           //cout<<i<<" "<<t-1<<endl;
           g[i].push_back(t-1);
       }
   }
  
   ans.at(0)=0;
   queue<int> q ;
   q.push(0);
   
   while(!q.empty()){
      int from=q.front();
      q.pop();
      for(int to : g[from]){
          if(ans.at(to)!=-1)continue;
          ans.at(to)=ans.at(from)+1;
          q.push(to);
      }
   }
   rep(i,n){
       cout<<i+1<<" "<<ans.at(i)<<endl;
   }
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nFromMeTo;

  StDist(int nDist) : m_nDist(nDist), m_nFromMeTo(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    if (rvoDist[nx].m_nDist == nIMPOS)  continue;
    if (rvoDist[nx].m_nFromMeTo == 1 )  continue; 

    rvoDist[nx].m_nFromMeTo = 1;
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
      if (cnrvvnAdjcy[nx][ny] == 1)
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(rvoDist[ny].m_nDist, nDist);

        rqnBFSrch.push(ny);
      }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define Lens 105
using namespace std;
int N;
int A[Lens][Lens];
int d[Lens];
char c[Lens];
queue<int> Q;

int main()
{
	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		for (int i = 0; i < n; i++)
		{
			int j;
			cin >> j;
			Q.push(j);
			A[k][j] = 1;
		}
	}
	int j = Q.front();
	for (int i = 1; i <= N; i++)
	{
		while (A[i][j] == 1 && c[j] == 'w')
		{
			Q.pop();
			d[j] = d[i] + 1;
			c[j] = 'g';
			if (Q.size() == 0)
				break;
			j = Q.front();
			while (c[j] == 'g')
			{
				Q.pop();
				if (Q.size() == 0)
					break;
				j = Q.front();
			}

		}

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

C++: #include<bits/stdc++.h>

using namespace std;

vector<int> G[110];

const int INF=1e9;

int d[110];
bool used[110];

void bfs(int x)
{
    fill(d,d+110,INF);
    d[x]=0;

    queue<pair<int,int> > q;

    q.push(make_pair(0,x));

    used[x]=true;

    while(!q.empty())
    {
        pair<int,int> tmp=q.front();q.pop();

        int dd=tmp.first,now=tmp.second;
        for(int i=0;i<G[now].size();i++)
        {
            if(!used[G[now][i]])
            {
                d[G[now][i]]=dd+1;
                q.push(make_pair(dd+1,G[now][i]));
                used[G[now][i]]=true;
            }
        }
    }
}


int main()
{
    int n;

    cin>>n;

    for(int i=0;i<n;i++)
    {
        int a,b,c;

        cin>>a>>b;

        for(int j=0;j<b;j++)
        {
            cin>>c;

            G[a].push_back(c);
        }
    }

    bfs(1);

    for(int i=1;i<=n;i++)
    {
        cout<<i<<" ";

        if(d[i]==INF)
        {
            cout<<"-1";
        }else
        {
            cout<<d[i];
        }
        cout<<endl;
    }

    return 0;
}