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 <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_map>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

#define INF -1

struct Node {
    int d;
    int n;
    Node(){}
    Node(int n, int d){
        this->d = d;
        this->n = n;
    }
};

int main(){
    int n; cin >>n;
    vector<int> v;
    vector<vector<int>> edges(n,v);
    rep(i, n) {
        int id; cin >> id;
        int num; cin >> num;
        rep(j, num) {
            int dst;
            cin >> dst;
            dst--;
            edges[i].push_back(dst);
         }
    }
    queue<Node> q;
    vector<int> dst(n,INF);

    q.push(Node(0,0));
    while(!q.empty()) {
        Node node = q.front();
        q.pop();
        if(dst[node.n] == INF) {
            dst[node.n] = node.d;
            // cout << "popped " << node.n << endl;
        } else {
            continue;
        }
        for(auto it = edges[node.n].begin(); it != edges[node.n].end(); it++) {
            // cout << "pushed " << *it << endl;
            q.push(Node(*it, node.d+1));
        }
    }
    rep(i, n) {
        cout << i+1 << " " << dst[i] << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<algorithm>
#include<utility>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF = pow(10, 8);
int main(){
  int n;cin>>n;
  vector<vector<int> > edge(n, vector<int>());
  for(int i=0;i<n;i++){
    int u, k;cin>>u>>k;u--;
    for(int j=0;j<k;j++){
      int v;cin>>v;edge[i].push_back(v-1);
    }
  }
  vector<int> dis(n, INF);
  dis[0]=0;
  queue<int> que;
  que.push(0);
  while(que.size()){
    int v = que.front();que.pop();
    for(int &u : edge[v]){
      if(dis[u]>dis[v]+1){
        dis[u]=dis[v]+1;
        que.push(u);
      }
    }
  }
  for(int i=0;i<n;i++){
    if(dis[i]==INF)cout<<i+1<<" "<<-1<<endl;
    else cout<<i+1<<" "<<dis[i]<<endl;
  }
  return 0;
}

C++: # include <bits/stdc++.h>
using namespace std;

int n,v,u,k,d=0;
int l[110],f[110];
int b[110][110];
queue<int> q;
void bfs(int a)
{
	while(!q.empty()){
		a=q.front();q.pop();
		for(int i=1;i<=n;i++){
			if(b[a][i]==1&&f[i]==0){
				f[i]=1;
				q.push(i);
				l[i]=l[a]+1;
			}
		}
	}
	return ;
}
int main()
{	
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>u;
		cin>>k;
		for(int j=1;j<=k;j++){
			cin>>v;
			//cout<<v<<endl;
			b[u][v]=1;
		} 
	}
	
	
	//cout<<"@"<<b[1][3]<<"@"<<endl;
	l[1]=0;
	f[1]=1;
	q.push(1);
	bfs(1);
	for(int i=1;i<=n;i++){
		if(l[i]==0&&i!=1){
			cout<<i<<" "<<-1<<endl;
			continue;
		}
		cout<<i<<" "<<l[i]<<endl;
	}
	return 0;
 }

C++: //
//  main.cpp
//  breadth_first_search
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>

#define n_max 100
#define INFTY 1000
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

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

queue<int> Q;

void encode_graph(){
    int i, j, id, num, node;
    
    //initialize with 0
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
    
    for (i = 0; i < n; i++){
        scanf("%d", &id);
        scanf("%d", &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            M[id-1][node-1] = 1;
        }
    }
}

void bfs(){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    //starting point
    int s = 0;
    color[s] = GRAY;
    d[s] = 0;
    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;
    }
    
    
    
}

int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    
    encode_graph();
    bfs();
    
    for(int i = 0; i < n; i++){
        cout<<i+1<<" "<<((d[i] == INFTY) ? (-1): (d[i]))<<endl;
    }
    
//    std::cout << "Hello, World!\n";
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

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

int main(){
	scanf_s("%d", &N);
	int id, c;
	for (int i = 0; i < N; i++){
		scanf_s("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
		scanf_s("%d", V + id);
		for (int j = 0; j < V[id]; j++){
			scanf_s("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
			G[id][c] = 1;
		}
	}//データの読み込み完了。
	bfs(0);
}