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 <iostream>
#include <vector>
#include <queue>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
    // 頂点数と辺数
    int N ; cin >>N;

    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < N ;i++) {
        int a, b; cin >> a >> b;
        for(int j=0;j<b;j++) {
          int c; cin >>c;
          G[a-1].push_back(c-1);
        }
    }
     // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 0; v < N; ++v) cout << v+1 << " " << dist[v] << endl;
}

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

using namespace std;

int N;
int D[100+10] = {};
int U = 0;
int K[100+10] = {0};
int V = -1;
int G[100+10][100+10] = {0};



void bfs(int src){
  //cerr << "bfs root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()){
    int cur = Q.front();
    Q.pop();
    //cerr << "visiting " << cur << " " << D[cur] << endl;
    for(int dst=1;dst<=N;++dst){
      if(D[dst] == -1 && G[cur][dst] == 1){
	D[dst] = D[cur]+1;
	Q.push(dst);
      }
    }
  }
}

int main(){
  cin >> N;
  for(int i=0;i<101;++i){
    D[i] = -1;//?????????
  }

  for(int i=0;i<N;++i){
    cin >> U;
    cin >> K[U];
    for(int j=0;j<K[U];++j){
      cin >> V;
      G[U][V] = 1;
    }
  }
  /*
  for(int j=1;j<=N;++j){
    for(int i=1;i<=N;++i){
      cout << G[i][j] << " ";
    }
    cout << endl;
   }
*/

  bfs(1);
  
  for(int i=1;i<=N;++i){
    cout << i << " " << D[i] << endl;
  }

}

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;

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int n;
int a[105][105];
int d[105];
void bfs(int x){
  d[x]=0;
  queue<P > q;
  int y=x;
  for(int i=1;i<=n;i++){
    if(a[y][i])q.push(P(i,d[y]+1));
  }
  while(!q.empty()){
    P p=q.front();q.pop();
    y=p.first;
    if(d[y]!=-1)continue;
    d[y]=p.second;
    for(int i=1;i<=n;i++){
      if(a[y][i])q.push(P(i,d[y]+1));
    }
  }
}
int main(){
  for(int i=0;i<105;i++){
    d[i]=-1;
  }
  cin >> n;
  for(int i=0;i<n;i++){
    int u,m;
    cin >> u >> m;
    for(int j=0;j<m;j++){
      int v;
      cin >> v;
      a[u][v]=1;
    }
  }
  bfs(1);
  for(int i=1;i<=n;i++){
    cout << i<< " " <<d[i] << endl;
  }
  return 0;
}

C++: //＃いんｃぅで＜ｓｔぢお。ｈ＞
#include<stdio.h>
#include<iostream>
using namespace std;
int n=0,u=0;
int k[101],v[101][101],d[101];
int lptr=0,fptr=0;//lptr==enqueue,fptr==dequeue...
int t1[10000];
int s1[10000];
int c=0;//current
int nx=sizeof(t1)/sizeof(t1[0]);
void Enqueue(int s,int t){
	t1[lptr]=t;
	s1[lptr]=s;
	lptr=(lptr+1)%nx;
}
void bfs(int l){
	while(fptr!=lptr){
		l=t1[fptr];
		c=s1[fptr];
		fptr=(fptr+1)%nx;
		if(d[l]!=-1){
			continue;
		}
		for(int i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(c+1,v[l][i]);
			}
		}
		d[l]=++c;
	}
}
int main(void){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u;
		cin>>k[u];
		for(int j=0;j<k[u];j++){
			cin>>v[u][j];
		}
	}
	Enqueue(-1,1);
	for(int i=0;i<n+1;i++){
		d[i]=-1;
	}
	bfs(0);
	
	for(int i=1;i<n+1;i++){
		cout<<i<<" "<<d[i]<<endl;
	}
	
	return 0;
}

C++: /**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N];

void bfs(int n, int dd) {
	int k = adj[n][0];
	d[n] = dd++;
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
		d[i]=-1;
	}

	bfs(1, 0);

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

	return 0;
}

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

#include <cassert>
#include <climits>

#include <string.h>

#include <vector>
#include <map>
#include <stack>
#include <deque>

enum Color
{
    kWhite,
    kGray,
    kBlack,
};

int n;
int start_vertex = 0;
std::vector< std::vector<int> > matrix;
std::vector<Color> c;
std::vector<int> d;

void breadthFirstSearch()
{
    for (int i = 0; i < n;++i)
    {
        c.push_back(kWhite);
        d.push_back(-1);
    }

    std::deque<int> tmp_queue;
    tmp_queue.push_back(start_vertex);
    c[start_vertex] = kGray;
    d[start_vertex] = 0;

    while (! tmp_queue.empty())
    {
        int current = tmp_queue.front();
        tmp_queue.pop_front();
        c[start_vertex] = kBlack;

        std::vector<int> line = matrix[current];
        for (int i = 0; i < line.size(); ++i)
        {
            if (line[i] > 0 && c[i] == kWhite)
            {
                tmp_queue.push_back(i);
                c[i] = kGray;
                d[i] = d[current] + 1;
            }
        }
    }
}

int main()
{
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::vector<int> line;
        for (int j = 0; j < n; ++j)
        {
            line.push_back(0);
        }
        matrix.push_back(line);
    }

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

        std::cin >> u;
        std::cin >> k;

        for (int j = 0; j < k; ++j)
        {
            int tmp;
            std::cin >> tmp;
            matrix[u - 1][tmp - 1] = 1;
        }
    }

    breadthFirstSearch();

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

    return 0;
}