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>


using namespace std;

bool adjmatrix[101 * 101];
int costs[101];



void BFS(int vertex, int cur_cost, int N) {
  if (costs[vertex] < cur_cost) return;
  costs[vertex] = cur_cost;
  for (int i =1; i<= N; i++) {
    if(adjmatrix[vertex*101 + i]) {
      BFS(i, cur_cost + 1, N);
    }
  }
}
  

int main() {
  for (int i = 0; i < 101; i++) {
    costs[i] = 101;
  }
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    int vertex, degree;
    cin >> vertex >> degree;

    for (int j = 0; j < degree; j++) {
      int outvertex;
      cin >> outvertex;
      adjmatrix[vertex * 101 + outvertex] = true;
    }
  }


  /*
  //function to print adjmatrix
  for (int i = 0; i <= N; i++) {
    for (int j= 0; j <= N; j++) {
      if (adjmatrix[i * 101 + j]) {
	cout << "1 ";
      } else {
	cout << "0 ";
      }
    }
    cout << endl;
    }*/

  BFS(1, 0, N);

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

 

}

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

vector< vector<int> > L(105);
vector<int> d(105,-1);
vector<bool> checked(105,0);

void BFS(int s){
	queue<int> Q;
	int i;
	int v;
	
	Q.push(s);
	d[s]=0;
	while(!Q.empty()){
		v=Q.front();
		Q.pop();
		
		for (i=0;i<L[v].size();i++){
			if (d[L[v][i]]==-1){
				Q.push(L[v][i]);
				d[L[v][i]]=d[v]+1;
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,i,j;
	cin >> n;
	
	int id, deg, v;
	for (i=0;i<n;i++){
		cin >> id >> deg;
		for (j=0;j<deg;j++){
			cin >> v;
			L[id-1].push_back(v-1);
		}
	}
	
	BFS(0);
	
	for (i=0;i<n;i++){
		cout << i+1 << " " << d[i] << endl;
	}
	return 0;
	
}

C++: #include <iostream>
#include <queue>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

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

void bfs(int s){
  for(int i = 0 ; i < n ; i++){
    color[i] = WHITE;
    d[i] = 1000000000;
  }
  color[s] = GRAY;
  d[s] = 0;
  queue<int> Q;
  Q.push(s);

  while(!Q.empty()){
    int u = Q.front();
    Q.pop();
    for(int v = 0; v < n ; v++){
      if(M[u][v] && color[v] == WHITE){
        color[v] = GRAY;
        d[v] = d[u] + 1;
        Q.push(v);
      }
    }
    color[u] = BLACK;
  }
  for(int i = 0 ; i < n ; i++){
    cout << i+1 << " " << ((d[i] == 1000000000) ? (-1) :d[i]) << "\n";
  }
}

int main(){
  int u , k , A;
  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 >> A;
      M[u][A-1] = 1;
    }
  }

  bfs(0);
  return 0;
}

C++: 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>


int n;
int a[105][105];
std::queue<int>S;
int dis[105];
int color[105];

void bfs(int k){
    int i;
    for(i=1;i<=n;i++){
        if(i!=k&& a[k][i]==1 &&color[i]==0){
            S.push(i);
            dis[i]=dis[k]+1;
            color[i]=1;
        }
    }
}


int main(){
    scanf("%d",&n);
    int i,j;
    for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;
    for(i=1;i<=n;i++){
        int p,k,q;
        scanf("%d %d",&p ,&k);
        for(j=1;j<=k;j++){
            scanf("%d",&q);
            a[p][q]=1;
        }
    }
    for(i=1;i<=n;i++)dis[i]=-1;
    dis[1]=0;
    for(i=1;i<=n;i++)color[i]=0;//0???????¨????
    S.push(1);
    color[1]=1;
    while(!S.empty()){
        int front=S.front();
        S.pop();
        bfs(front);
    }
    for(i=1;i<=n;i++){
        printf("%d %d\n",i,dis[i]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

using Graph = vector<vector<int> >;

vector<int> dist;
queue<int> que;

void bfs(const Graph &G){
  while(!que.empty()){
    int v = que.front();
    que.pop();

    for(int nv : G[v]){
      if(dist[nv] != -1) continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }
}

int main(){
  int n; cin >> n;

  Graph G(n+1);
  rep(i, n){
    int u, k; cin >> u >> k;
    rep(j, k){
      int v; cin >> v;
      G[u].push_back(v);
    }
  } 

  dist.assign(n+1, -1);
  dist[1] = 0;
  que.push(1);
  bfs(G);

  REP(i, 1, n){
    cout << i << " " << dist[i] << endl;
  }

  return 0;
}

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

const int INF = -1;

int G[110][110] = {};
int d[110] = {};

void bfs(int s, int n)
{
  int v;
  queue<int> que;

  for(int i = 1;i <= n;i++) d[i] = INF; //init

  que.push(s);
  d[s] = 0;
  
  while(que.size()){
    v = que.front();
    que.pop();

    for(int i = 1;i <= n;i++){
      if(G[v][i] != 1) continue;
      if(d[i] != INF) continue;

      que.push(i);
      d[i] = d[v]+1;
    }
  }
}

int main()
{
  int n;
  int i,j;
  int u,k,v;

  scanf("%d", &n);

  for(i = 0;i < n;i++){
    scanf("%d%d", &u, &k);

    for(j = 0;j < k;j++){
      scanf("%d", &v);
      G[u][v] = 1;
    }
  }

  /*for(i = 1;i <= n;i++)
    if(d[i] == 0) bfs(i, n);
  */
  bfs(1, n);
  
  for(i = 1;i <= n;i++)
    printf("%d %d\n", i, d[i]);

  return 0;
}