Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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);
}