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;

typedef pair< int,int > P;
int N;
vector< vector< int > > Edge;
vector< int > Csum;

void bfs(){
  Csum.assign(N+1,-1);
  queue< P > que;
  que.push(P(0,0));
  Csum[0] = 0;
  while(!que.empty()){
    int now = que.front().second;
    int cost = que.front().first;
    que.pop();
    for(int i=0;i<Edge[now].size();i++){
      if(Csum[Edge[now][i]] != -1) continue;
      Csum[Edge[now][i]] = cost+1;
      que.push(P(cost+1,Edge[now][i]));
    }
  }
}

int main(){
  cin >> N;
  Edge.resize(N+1);
  for(int i=0;i<N;i++){
    int u,k; cin >> u >> k;
    for(int j=0;j<k;j++){
      int e; cin >> e; e--;
      Edge[i].push_back(e);
    }
  }
  bfs();
  for(int i=0;i<N;i++){
    cout << i+1 << " " << Csum[i] << endl;
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int M[N][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 k,u,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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

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);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty())
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
	int u, k, v;

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(j, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}



	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N]; // 頂点0からの距離

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] && d[v] == INFTY) {
                // 未訪問
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

    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 rep(i, n) for (int i = 0; i < (int)(n); i++)

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

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

int n, M[N][N];
int color[N], d[N], f[N], tt;

// void dfs_visit(int u) {
//    color[u] = GRAY;
//    d[u] = ++tt;
//    rep(v, n) {
//       if (M[u][v] == 0) continue;

//       if(color[v] == WHITE) {
//          dfs_visit(v);
//       }
//    }
//    color[u] = BLACK;
//    f[u] = ++tt;
// }

// void dfs() {
//    int u;
//    for(u = 0; u < n; u ++) {
//       color[u] = WHITE;
//    }

//    for(u = 0; u < n; u ++) {
//       if(color[u] == WHITE) dfs_visit(u);
//    }

//    for(u =0; u<n;u++) {
//       printf("%d %d %d\n", u+1, d[u], f[u]);
//    }
// }

// void bfs_visit() {

// }

void bfs(int s) {
   queue<int> q;

   rep(i, n) {
      d[i] = INFTY;
      color[i] = WHITE;
   }

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

   while (!q.empty()) {
      int u = q.front(); q.pop();
      rep(v, n) {
         if (M[u][v] && color[v] == WHITE) {
            color[v] = GRAY;
            d[v] = d[u] + 1;
            q.push(v);
         }
      }
      color[u] = BLACK;
   }

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

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

   // graphを0で初期化
   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 ++) {
      scanf("%d %d", &u, &k);
      u--;
      for(int j = 0; j < k; j++) {
         scanf("%d", &v);
         v--;
         M[u][v] = 1;
      }
   }

   bfs(0);
   // dfs();
   return 0;
}

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

struct Tree
{
    int p, start, end, min;
    bool isSearched;
    vector<int> v;
};

void DFS( Tree* t, int a, int cnt )
{
    if( t[a].isSearched && t[a].min <= cnt )
        return;
    
    t[a].isSearched = true;

    if( t[a].min > cnt )
        t[a].min = cnt;

    cnt++;

    for( int i = 0; i < t[a].v.size(); i++ )
        DFS( t, t[a].v[i], cnt );
}


int main()
{
    int n, a, b, c;
    Tree t[101];

    for( int i = 0; i < 101; i++ )
    {
        t[i].p = -1;
        t[i].min = 1000;
        t[i].isSearched = false;
    }
    
    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> a >> b;
        for( int j = 0; j < b; j++ )
        {
            cin >> c;
            t[a].v.push_back(c);
            t[c].p = a;
        }
    }

    int cnt = 0;
    DFS( t, 1, cnt );

    for( int i = 1; i <= n; i++ )
    {
        if( t[i].min == 1000 )
            cout << i << " " << -1 << endl;
        else
            cout << i << " " << t[i].min << endl;
    }
    return 0;
}