Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

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

#define N 100
#define INFTY 1000000

int M[N][N],d[N];
int i,j,n,u,k,v;

void bfs(int s){

  queue <int> Q;

  for( i = 0; i < N; i++ ) d[i] = INFTY;
  

  d[s] = 0;
  Q.push(s);
    
  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( i = 0; i < n; i++ ){
    cout << i+1 << " ";
    if ( d[i] == INFTY ) cout << -1;
    else cout << d[i];
    cout << endl;
  }

}


int main(){
  for( i = 0; i < n; i++ ){
    for( j = 0; j < n; j++ ) M[u][v] = 0;
  }
  cin >> n;
  for( i = 0; i < n; i++ ){
    cin >> u;
    u--;
    cin >> k;
    for( j = 0; j < k; j++ ){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  
  return 0;
}

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

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},f[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) f[i]=-1;
  f[1]=0;
  g[1]=1;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
      if(g[di]==0){
	g[di]=1;
	d[u][j]=di;
      }
    }
  }

  q.push(1);

  int cnt=1,fg;
  int a,c[101]={};
  while(!q.empty()){
    fg=0;
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>0){
	fg=1;
	q.push(d[a][i]);
	f[d[a][i]]=c[a]+1;
	c[d[a][i]]=c[a]+1;
      }
    }
      q.pop();
    if(fg==1) cnt++;
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<f[i]<<endl;
  return 0;
}

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

int n;
int a[100][100] = {0};
int k[100] = {0};
int d[100];

int kansu( int i, int t ){
	if ( d[i] != -1 && d[i] <= t ) return 0;
	d[i] = t;
	t++;
	for ( int j = 0; j < k[i]; j++ ) {
		kansu( a[i][j], t );
	}
	return 0;
}

int main(){
	cin >> n;
	int s;
	for ( int i = 0; i < n; i++ ) {
		cin >> s;
		cin >> k[i];
		for ( int j = 0; j < k[i]; j++ ) {
			cin >> s;
			a[i][j] = s-1;
		}
	}
	for ( int i = 0; i < n; i++ ) {
		d[i] = -1;
	}
	kansu(0,0);
	for ( int i = 0; i < n; i++ ) {
		cout << i+1 << " " << d[i] << endl;
	}
	return 0;
}

C++: /*
AizuOJ ALDS1_11_C

???;BFS;????´¢;?????????;?¨????
*/
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;

const int MAXN=110, MAXM=110*110, INF=0x3f3f3f3f;
int n;
int adj[MAXN]; // 1 .. n
int to[MAXM], enext[MAXM]; // 1 .. m
int d[MAXN];

int s, v;
void bfs(int start) {
    queue<int> q;
    q.push(start);
    while (!q.empty()) {
        s=q.front();
        q.pop();
        for (int e=adj[s]; e!=0; e=enext[e]) {
            v=to[e];
            if (d[v]>d[s]+1) {
                d[v]=d[s]+1;
                q.push(v); // ?????????????????? !visited
            }
        }
    }
}

void add_edge(int no, int u, int v) {
    to[no]=v;
    enext[no]=adj[u];
    adj[u]=no;
}

int m=0, k, tmp;
//int v;
int main() {
    memset(d, INF, sizeof(d));
    d[1]=0;
    scanf("%d", &n);
    for (int u=1; u<=n; u++) {
        scanf("%d%d", &tmp, &k);
        while (k--) {
            scanf("%d", &v);
            add_edge(++m, u, v);
        }
    }
    bfs(1);
    for (int u=1; u<=n; u++) {
        printf("%d %d\n", u, d[u]==INF ? -1 : d[u]);
    }
    return 0;
}

C++: #include<stdio.h>
int n=0;
int u=0;
int k[101]={0};
int v[101][101]={0};
int d[101];//d[]=short[].
int cur=0;
int fptr=0;
int lptr=0;
int queue[1024]={};
int q2[1024]={};
int max=sizeof(queue)/sizeof(queue[0]);    //データの数/1データの使用メモリ
void enqueue(int p,int m){
	queue[lptr]=p;
	q2[lptr]=m;
	lptr=(lptr+1)%max;//エンキュー
}
void bfs(void){
	int id=0,m=0;
	while(lptr!=fptr){
	id=queue[fptr];
	m=q2[fptr];
	fptr=(fptr+1)%max;//デキュー
	if(d[id]!=-1){
		continue;
	}
	d[id]=m;
	for(int i=0;i<k[id];i++){
		enqueue(v[id][i],m+1);
	}
	}
	return;
	}
	int main(void)
{
	for(int r=0;r<101;r++){
	d[r]=-1;
	}
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&u);
		scanf("%d",&k[u]);
		for(int j=0;j<k[u];j++){
			scanf("%d",&v[u][j]);
		}
	}
		enqueue(1,0);
		
		bfs();
	for(int i=1;i<n+1;i++){
		printf("%d %d\n",i,d[i]);
	}
	return(0);
}

C++: #include<iostream>
using namespace std;
#define NIL -1
int n,ch[200],V[200][200],d[200],a;
void count(int x,int reach){
    d[x]=reach;
    for(int i=1;i<=ch[x];i++){
        int p=V[x][i];
        if(d[p]==NIL||d[p]>reach+1) count(p,reach+1);
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a>>ch[i];
        d[i]=NIL;
        for(int j=1;j<=ch[i];j++){
            cin>>V[i][j];
        }
    }
    count(1,0);
    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);
}