Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i <= n; i++)

int n;
queue<int> q;
vector<int> depth;

void dfs(int start, vector<vector<int>> a) {
    depth[start] = 0;
    q.push(start);

    while (!q.empty()) {
        int current = q.front();
        q.pop();
        rep(adj, n) {
            if (a[current][adj] == 1 && depth[adj] == -1) {
                depth[adj] = depth[current] + 1;
                q.push(adj);
            }
        }
    }
}

int main() {
    scanf("%d", &n);
    depth.resize(n+1, -1);
    vector<vector<int>> a(n+1, vector<int>(n+1, 0));
    rep(i, n) {
        int u;
        scanf("%d", &u);
        int k;
        scanf("%d", &k);
        rep(j, k) {
            int adj;
            scanf("%d", &adj);
            a[u][adj] = 1;
        }
    }
    dfs(1, a);
    rep(i, n) {
        printf("%d %d\n", i, depth[i]);
    }
}

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] < D[cur]){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}

int main(){
	while (~scanf("%d", &N) && 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;


vector<int>v[105];
int t[105]={};
bool flag[105]={};
void BFS(int p)
{
	queue<int>Q;	
	Q.push(p);
	flag[p]=true;
	while(!Q.empty())
	{
		p=Q.front();
		Q.pop();
		for(int i=0;i<v[p].size();i++)
		{
			if(!flag[v[p][i]])
			{
				t[v[p][i]]=t[p]+1;
				Q.push(v[p][i]);
			}
			flag[v[p][i]]=true;
		}
	}
}
int main()
{	
	int n,a,b,k;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a>>k;
		for(int j=0;j<k;j++)
		{
			cin>>b;
			v[a].push_back(b);
		}
	}
	BFS(1);
	for(int i=1;i<=n;i++)
	{
		cout<<i<<" ";
		if(i!=1&&t[i]==0)cout<<-1<<endl;
		else cout<<t[i]<<endl;
	}
	return 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;
            //cout <<v<<" "<<d[v];
            d[v] = d[u] + 1;
            //cout <<"->"<< d[v] << endl;
            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;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG

int main() {
  int n;
  cin >> n;
  vector<vector<int>> dst(n + 1);
  for (int i = 1; i <= n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      dst[i].push_back(v);
    }
  }
  queue<int> Q;
  vector<int> ans(n + 1, -1);
  ans[1] = 0;
  Q.push(1);
  while (Q.size()) {
    int p = Q.front();
    for (int d : dst[p]) {
      if (ans[d] == -1) {
        ans[d] = ans[p] + 1;
        Q.push(d);
      }
    }
    Q.pop();
  }
  for (int i = 1; i <= n; i++) {
    cout << i << ' ' << ans[i] << endl;
  }
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph=vector<vector<int>>;
typedef long long ll;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int main(){
    int n;cin>>n;
    Graph G(n+2);
    for(int i=0;i<n;i++){
        int u,k;cin>>u>>k;
        for(int j=0;j<k;j++){
            int v;cin>>v;
            G[u].push_back(v);
        }
    }
    bool ans_check=false;
    vector<int> b(n+2,1000000);
    queue<int> q;
    b[1]=0;
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int A=0;A<G[x].size();A++){
            int check_x=G[x][A];
            if(b[check_x]==1000000){
                q.push(check_x);
                b[check_x]=b[x]+1;
            }
        }
    }
    for(int i=1;i<n+1;i++){
        if(b[i]==1000000) b[i]=-1;
        cout<<i<<' '<<b[i]<<endl;
    }
}