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++: # -*- coding: utf-8 -*-
from collections import deque

N = int(input())
adj = [[0]*N for _ in range(N)]
unsearched = set(range(1, N))
result = {n:-1 for n in range(N)}

for n in range(N):
    inp = list(map(int, input().split()))
    for i in inp[2:]:
        adj[inp[0]-1][i-1] = 1

que = deque([[0, 0]]) # edge distance


while len(que):
    q = que.pop()
    result[q[0]] = q[1]
    next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)]
    unsearched = unsearched.difference([i[0] for i in next_])
    que.extendleft(next_)

for n in range(N):
    print("{} {}".format(n+1, result[n]))

C++: // J4180449 三宅亮輔
// 問題名
// - Breadth First Search
// 問題URL
// - https://onlinejudge.u-aizu.ac.jp/#/problems/ALDS1_11_C
// 所要時間
// -
// 学んだこと
//
//
//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int n, d[100];

vector<int> v[100];

queue<int> q;

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        d[i] = -1;
        int u, k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; j++) {
            int x;
            cin >> x;
            x--;
            v[u].push_back(x);
        }
    }

    d[0] = 0;
    q.push(0);

    while(!q.empty()) {
        int vi = q.front();
        q.pop();

        for(int i = 0; i < v[vi].size(); i++) {
            int next = v[vi][i];
            if(d[next] == -1) {
                d[next] = d[vi] + 1;
                q.push(next);
            }
        }
    }

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

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

struct VRTX {
	int v[100] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[100];

void breadthFirstSearch(int x, int dist);

int main() {
	int i, j, id, dim;
	
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> dim;
		for (j = 0; j < dim; j++) {
			cin >> u[id].v[j];
		}
	}

	breadthFirstSearch(1, 0);

	for (i = 1; i <= n; i++) {
		cout << i << " " << u[i].dist << endl;
	}

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist <= dist)return;
	else u[x].dist = dist;
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}

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

const int nIMPOS = -1;
struct StDir
{
  int m_nBgn;
  int m_nEnd;

  StDir(int nBgn, int nEnd) : m_nBgn(nBgn), m_nEnd(nEnd)
  { }
};

void fnInput(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  int nxSiz;
  cin >> nxSiz;

  rvnDst.resize(nxSiz + 1, nIMPOS);
  rvnDst[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;
      if (nx != ny)
      {
        StDir oDir(nx, ny);
        rqoBFSrch.push(oDir);
      }
    }
  }
}

void fnBreadthFirstSearch(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();

    if (rvnDst[oDir.m_nBgn] == nIMPOS)  continue;

    int nDst = rvnDst[oDir.m_nBgn] + 1;
    if (rvnDst[oDir.m_nEnd] == nIMPOS)
      rvnDst[oDir.m_nEnd] = nDst;
    else
      rvnDst[oDir.m_nEnd] = min(rvnDst[oDir.m_nEnd], nDst);
  }
}

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

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(qoBFSrch, vnDst);
  fnBreadthFirstSearch(qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
template<class T> using vv=vector< vector<T> >;
typedef deque<int> di;
typedef deque< deque<int> > ddi;


int main() {
    int n;
    cin >> n;
    vi vlist[101];
    rep(i,n) {
        int u,k;
        cin >> u >> k;
        rep(i,k) {
            int v;
            cin >> v;
            vlist[u].pb(v);
        }
    }

    queue<int> Q;
    int count[101];
    Q.push(1);

    while(!Q.empty()) {
        int i = Q.front();
        Q.pop();
        
        rep(j,vlist[i].size()) {
            if(count[vlist[i][j]] == 0) {
                count[vlist[i][j]] = count[i] + 1;
                Q.push(vlist[i][j]);
            }
        }
    }
    FOR(i,1,n+1) {
        if(i != 1 && count[i] == 0) {
            cout << i << " " << -1 << endl;
        } else {
            cout << i << " " << count[i] << endl;
        }
    }

	return 0;
}