Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;


bool is_connected[150][150];
int min_distance[150];

void init() {
    int i, j;

    rep(i, 150) {
        min_distance[i] = 150;
    }
    min_distance[0] = 0;
    min_distance[1] = 0;
    rep(i, 150) rep(j, 150) is_connected[i][j] = false;
}

void solveDepth(int src_id, int cur_depth) {
    int dst_id;
    rep(dst_id, 150) {
        if (is_connected[src_id][dst_id]) {
            if (cur_depth + 1 < min_distance[dst_id]) {
                min_distance[dst_id] = cur_depth + 1;
                solveDepth(dst_id, cur_depth + 1);
            }
        }
    }
}

int main() {
    int n, i, j, u, v, k;
    cin >> n;

    init();

    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v;
            is_connected[u][v] = true;
        }
    }

    solveDepth(1, 0);

    rep(i, n) {
        cout << i + 1 << " ";
        if (min_distance[i + 1] == 150) {
            cout << -1 << endl;
        } else {
            cout << min_distance[i + 1] << endl;
        }
        
    }

    return 0;
}

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

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

  StDist(int nDist) : m_nDist(nDist), m_nDmy(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();
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (!cnrvvnAdjcy[nx][ny])          continue;
      if (rvoDist[ny].m_nDist != nIMPOS) continue; 

      rvoDist[ny].m_nDist = rvoDist[nx].m_nDist + 1;
      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 <bits/stdc++.h>

using namespace std;

int main() {

	int n;
	cin >> n;
	vector < vector<int>> G(n + 1);

	//グラフ隣接リスト読み込み
	int a, b, c;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			G[a].push_back(c);
		}
	}

	//幅優先探索処理
	vector<bool> seen(n + 1, false);	//発見したらtrue
	deque<int> todo;					//行く所のスタック

	int s=1;							//今いる場所
	vector<int> dist(n+1,0);			//1からの距離（この問題で使う変数）

	int j = 1; //この問題では１から到達できるところを調べる
//	for (int j = 1; j <= n; j++) {		//全ての頂点を調べる
		if (seen[j] == false) {			//訪れてなかったら処理すすめる
			seen[j] = true;
			todo.push_back(j);

			while (todo.empty() != true) {				//スタックが空になるまで処理	
				s = todo.front();						//処理番号を指定
				int tugiflg = false;					//次があったらtrueにするフラグ
				for (int i = 0; i < G[s].size(); i++) {
					if (seen[G[s][i]] != true) {
						seen[G[s][i]] = true;
						todo.push_back(G[s][i]);
						dist[G[s][i]] = dist[s]+1;		//この問題でのみ使う（1からの距離）
						tugiflg = true;
					}
				}
				todo.pop_front(); //次を全部探したので削除する
			}
		}
//	}

	for (int i = 1; i <= n; i++) {
		if (i != 1 && dist[i] == 0) { dist[i] = -1; }
		cout << i << " " << dist[i]  << endl;
	}

	return 0;

}

C++: #include<cstdio>
#include<queue>
using namespace std;
static const int INF=(1<<21);
static const int MAXN=101;
int main()
{
	int n,m[MAXN][MAXN],i,j,u,k,v,l,t[MAXN],c;
	queue<int> q;
	bool used[MAXN]={};
	scanf("%d",&n);
	for(i=1;i<n+1;i++){
		for(j=1;j<n+1;j++){
			m[i][j]=INF;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(j=0;j<k;j++){
			scanf("%d",&v);
			m[u][v]=1;
		}
	}
	t[1]=0;
	used[1]=true;
	q.push(1);
	while(!q.empty()){
		l=q.front();
		q.pop();
		for(i=1;i<n+1;i++){
			if(m[l][i]==1&&used[i]==false){
				q.push(i);
				used[i]=true;
				t[i]=t[l]+1;
			}
		}
	}
	for(i=1;i<n+1;i++){
		if(used[i]==true){
			printf("%d %d\n",i,t[i]);
		}
		else{
			printf("%d -1\n",i);
		}
	}
	return 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) {
		check_id = 0;
		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);
	}
	check_id = 0;
	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;
}