Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string>

using namespace std;

int minDistance(vector<vector<int>>& edge, int st, int en) {
	size_t numNode = edge.size();
	vector<bool> passed(numNode, false);
	queue<int> v;
	queue<int> d;

	v.push(st);
	d.push(0);
	passed[st] = true;

	while(!v.empty()) {

		int nowV = v.front(); v.pop();
		int nowD = d.front(); d.pop();

		if(nowV == en) {
			return nowD;
		}

		for(auto nextV : edge[nowV]) {
			if(passed[nextV]) {
				continue;
			}

			v.push(nextV);
			d.push(nowD + 1);
			passed[nextV] = true;
		}

	}

	return -1;
}

int main() {

	int n;
	cin >> n;

	vector<vector<int>> edge(n);
	for(int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for(int j = 0; j < k; j++) {
			cin >> v;
			edge[u - 1].push_back(v - 1);
		}
	}

	for(int i = 0; i < n; i++) {
		cout << i + 1 << " " << minDistance(edge, 0, i) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INF=-1;
int n,adj[MAX+1][MAX+1];
int color[MAX+1],d[MAX+1];
queue<int> Q;

void bfs(){
  for(int i=1;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[1]=0;
  color[1]=GRAY;
  Q.push(1);
  while(!Q.empty()){
    int u=Q.front(); Q.pop();
    for(int i=1;i<=n;i++){
      if(adj[u][i] && color[i]==WHITE){
        color[i]=GRAY;
        d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BLACK;
  }
}

int main(){
  cin >> n;
  for(int i=1;i<=n;i++){
    int u,k; cin >> u >> k;
    for(int i=1;i<=n;i++) adj[u][i]=0;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      adj[u][v]=1;
    }
  }
  bfs();
  for(int id=1;id<=n;id++)
    printf("%d %d\n",id,d[id]);
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

typedef struct{
  int q[10000];
  int head;
  int tail;
}Queue;

Queue que;
int g[N][N]; 
int n;
int d[N];
bool us[N];
void Enque(int data){
  que.q[que.tail]=data;
  que.tail++;
}

int Deque(void){
  int data;
  data=que.q[que.head];
  que.head++;
  return data;
}

bool isEmpty(){
  if(que.tail-que.head==0)return true;
  else return false;
}


void bfs(int s){
  int u,v;
  Enque(s);
  while(true){

    if(isEmpty()==true) {
      break;
    }
    u=Deque();
    //  cout << "u = " << u << endl;
      for(int i=1;i<= n;i++){
 
      if(us[i]==false && g[u][i] == 1){
	v=i;
	//	cout << "i - " << i <<endl;
	us[i]=true;
	d[v]=d[u]+1;
	Enque(v);
      }
    }
  }
}

int main(){

  int n2,data,u;

 //load n
  cin >>n;

  //initialize
  que.tail=0;
  que.head=0;
  for(int i=0;i<N;i++){
    us[i]=false;
    d[i]=-1;
    for(int j=0;j<N;j++){
      g[i][j]=0;
    }
  }

  //load data
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&n2);
    for(int j=0;j<n2;j++){
      scanf("%d",&data);
      g[u][data]=1;
    }
  }
  //execute

  d[1]=0;
  us[1]=true;
  bfs(1);


  //print
  for(int i=1;i<=n;i++){
    printf("%d %d\n",i,d[i]); 
  }

  return 0;
}

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

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

int main()
{
    int G[101][102];
    int dist[101];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, a, b, e, v1, v2;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        for(j = 0; j <= n + 1; j++){ G[i][j] = 0; } }

    for(i = 1; i <= n; i++){
        scanf("%d %d", &a, &e);
		G[i][0] = e;  // number of edge.
        for(j = 1; j <= e; j++){
            scanf("%d", &b); G[a][j] = b; } }  // adjacent representation.

    for(i = 1; i <= n; i++) dist[i] = -1;
    f_insert(head, 1);
    dist[1] = 0;

    while(1){
        v1 = b_pop(tail);
		e = G[v1][0];
        for(i = 1; i <= e; i++){
            v2 = G[v1][i];
            if(dist[v2] < 0){
                dist[v2] = dist[v1] + 1;
                f_insert(head, v2);
            }
        }
        if(isEmpty(head, tail)) break;
    };

    for(i = 1; i <= n; i++){ printf("%d %d\n", i, dist[i]); }

    return 0;
}