Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(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;
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

int main()
{
    int n; cin >> n;
    vector<int> to[n];
    rep(_,n){
        int u; cin >> u; --u;
        int k; cin >> k;
        rep(i,k){
            int v; cin >> v; --v;
            to[u].push_back(v);
        }
    }

    int dist[n];
    rep(i,n) dist[i] = -1;

    queue<int> q;
    q.push(0);
    dist[0] = 0;

    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int v: to[u]){
            if(dist[v] == -1){
                dist[v] = dist[u]+1;
                q.push(v);
            }
        }
    }
    rep(i,n){
        printf("%d %d\n", i+1, dist[i]);
    }
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[100],f[100],w,h,n,sx,sy,gx,gy,e,g[100];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[0]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}