Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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

using namespace std;


int main() {
    int n, u, k; cin >> n;
    vector<vector<int> > v(n);
    for (int i = 0; i < n; ++i) {
        cin >> u >> k;
        vector<int> v0(k);
        for (int j = 0; j < k; ++j) {
            cin >> v0[j];
            --v0[j];
        }
        v[u-1] = v0;
    }
    queue<pair<int, int> > q;
    vector<bool> visited(n, false);
    q.push(make_pair(0, 0));
    visited[0] = true;
    vector<int> ans(n, -1);
    ans[0] = 0;
    while(!q.empty()) {
        pair<int, int> x = q.front(); q.pop();
        int cnt = x.second + 1;
        for (int i = 0; i < v[x.first].size(); ++i) {
            int j = v[x.first][i];
            if (!visited[j]) {
                q.push(make_pair(j, cnt));
                visited[j] = true;
                ans[j] = cnt;
            }
        }
    }
    for (int i = 0; i < n; ++i) cout << i + 1 << " " << ans[i] << endl;
    return 0;
}

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

using namespace std;

const static int N=100;
const static int INFTY=(1<<21);

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[u]!=0){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,v,k;
    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; k++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main(){
  queue<int> q;
  int n,a,k,u,c[101];
  vector<int>  d[101];
  cin>>n;for(int i=0;i<n;i++){
    cin>>u>>k;c[i]=-1;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }q.push(1),c[1]=0;
  while(!q.empty()){
   a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1) q.push(d[a][i]),c[d[a][i]]=c[a]+1;
  } for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

#define NO -1

using namespace std;


int adjacencyList[MAX + 1][MAX + 1];
int color[MAX + 1];
int d[MAX + 1];
int n;



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

	for (int i = 0; i < n; i++) {
		cin >> u >> k;

		for (int j = 0; j < k; j++) {
			cin >> v;
			adjacencyList[u][v] = 1;
		}

	}

	bfs(1);

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


	return 0;
}

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

#define INFTY (1 << 30)

void BFS(bool mT[][100], int firstVerVal, int verNum, int distance[]) {
	queue<int>  verQ;
	verQ.push(firstVerVal);
	for (int i = 0; i < verNum; i++)
		distance[i] = INFTY;
	distance[firstVerVal] = 0;

	while (!verQ.empty()) {
		int curVerVal = verQ.front();
		verQ.pop();
		for (int i = 0; i < verNum; i++) {
			if (!mT[curVerVal][i] || distance[i] != INFTY) continue;
			distance[i] = distance[curVerVal] + 1;
			verQ.push(i);
		}
	}
}

int main() {
	bool matrixTable[100][100];
	int distance[100];
	int verNum;
	cin >> verNum;

	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++)
			matrixTable[i][j] = false;
		
	for (int i = 0; i < verNum; i++) {
		int anyVerVal, anyVerNum;
		cin >> anyVerVal >> anyVerNum;
		for (int j = 0; j < anyVerNum; j++) {
			int verValue; cin >> verValue;
			matrixTable[anyVerVal - 1][verValue - 1] = true;
		}
	}

	BFS(matrixTable, 0, verNum, distance);

	for (int i = 0; i < verNum; i++) {
		cout << i + 1 << " ";
		if (distance[i] == INFTY)
			cout << -1 << endl;
		else
			cout << distance[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>
 
using namespace std;
 
int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離
 
void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] == -1){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}
 
int main(){
    scanf("%d", &N);
        int id, c;
        for (int i = 0; i < N; i++){
            scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
            scanf("%d", V + id);
            for (int j = 0; j < V[id]; j++){
                scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
                G[id][c] = 1;
            }
        }//データの読み込み完了。
    bfs(0);
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

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;
}