Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: //さっさと済ませられるところはさっさと済ましてしまう．ただし注意深く
#include <cstdio>
#include <iostream>
#include <queue>
#define MAX_VERTEXES 100
#define ZERO 0
#define NIL -1

using namespace std;

//think easy make easy

int n;//num of vertexes(頂点)
int matrix_graph[MAX_VERTEXES+1+1][MAX_VERTEXES+1+1];//1オリジン
int min_path[MAX_VERTEXES+1+1];


int main(){
	scanf("%d",&n);

	int v_num;
	int k;//v_numの出次数

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			matrix_graph[i][j] = ZERO;
		}
	}

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

		for(int j=0;j<k;j++){
			int v_num2;
			scanf("%d",&v_num2);
			matrix_graph[v_num][v_num2] = 1;
		}
	}

	for(int i=1;i<=n;i++){
		min_path[i] = NIL;
	}
	min_path[1] = 0;

	int s = 1;
	queue<int> q;
	q.push(1);
	while(!q.empty()){
		s = q.front();
		q.pop();
		for(int i=1;i<=n;i++){
			if(min_path[i] == NIL && matrix_graph[s][i] == 1){
				q.push(i);
				min_path[i] = min_path[s] + 1;
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout <<i <<" " <<min_path[i] <<endl;
	}

	return 0;
}

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

using namespace std;

const int N = 100;


int main() {
    int A[N][N] = {{0}};

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int u;
        cin >> u;

        int k;
        cin >> k;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            A[u-1][x-1] = 1;
        }
    }

    vector<int> d(n, -1);
    using int_pair = pair<int, int>;
    queue<int_pair> Q;
    Q.push(int_pair(0, 0));
    while (!Q.empty()) {
        auto p = Q.front();
        Q.pop();

        int k = p.first;
        int t = p.second;
        if (d[k] == -1) {
            d[k] = t;
        } else if (d[k] < t) {
            continue;
        } else {
            d[k] = t;
        }
        for (int i = 0; i < n; ++i) {
            if (A[k][i] != 0) {
                Q.push(int_pair(i, t + 1));
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << i + 1 << " " << d[i] << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200] ={false};

 



int main(){
  int n,u;
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u;
    cin >> k[u];
    for(int i=0;i<k[u];i++){
      cin >> imap[u][i];
    }
  }
  
  Queue que = Queue();

  que.push(1);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }


  }


 
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ans[i+1] << endl;
  }


  return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src)
{
    cerr<<"bfs root = "<<src<<endl;
    queue<int> q;
    q.push(src);
    D[src]=0;
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        cerr<<"visiting "<<cur<<' '<<D[cur]<<endl;
        for(int dst=0;dst<n;dst++)
        {
            if(matrix[cur][dst]==1&&D[dst]==-1)
            {
                D[dst]=D[cur]+1;
                q.push(dst);
            }
        }
    }
};

int main()
{
    vector<int> l[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int tmp;
            cin>>tmp;
            l[u].push_back(tmp);
        }
    }
    
    for(int i=0;i<100;i++)
    {
        D[i]=-1;
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=0;
        }
    }
    
    for(int i=1;i<n+1;i++)
    {
        for(int j=0;j<l[i].size();j++)
        {
            matrix[i-1][l[i][j]-1]=1;
        }
    }
    
    bfs(0);
    
    for(int i=0;i<n;i++)
    {
        cout<<(i+1)<<' '<<D[i]<<endl;
    }
}

C++: #include<cstdio>
#include<queue>
using namespace std;
#define INFTY -1
#define MAX 100

void InitializeBoolMatrix(int nmbr_of_rows, int nmbr_of_columns, bool mtrx[][MAX]) {
	for (int row = 0; row < nmbr_of_rows; ++row) {
		for (int column = 0; column < nmbr_of_columns; ++column)
			mtrx[row][column] = false;
	}
}

void ListToMatrix(int number_of_vertices, bool mtrx[][MAX]) {
	int vertex_from, vertex_to, degree_out;	
	for (int vertex_i = 0; vertex_i < number_of_vertices; ++vertex_i) {
		scanf("%d %d", &vertex_from, &degree_out);
		vertex_from--; //0?????????????????????
		for (int vertex_out_i = 0; vertex_out_i < degree_out; ++vertex_out_i) {
			scanf("%d", &vertex_to);
			vertex_to--; //0?????????????????????
			mtrx[vertex_from][vertex_to] = true;
		}
	}
}

void BuildAdjacencyMatrix(int n, bool adjmtrx[][MAX]) {
	InitializeBoolMatrix(n, n, adjmtrx);
	ListToMatrix(n, adjmtrx);
}

void Bfs(int n, int depth[], bool adjmtrx[][MAX]) {
	bool yet_to_be_visited[n];
	for (int i = 0; i < n; ++i) {
		yet_to_be_visited[i] = true;
		depth[i] = INFTY;
	}

	depth[0] = 0;
	queue<int> next_vertex_to_visit;
	next_vertex_to_visit.push(0);

	while( !next_vertex_to_visit.empty() ) {
		int u = next_vertex_to_visit.front();
		next_vertex_to_visit.pop();
		for (int v = 0; v < n; ++v)	{
			if( adjmtrx[u][v] && yet_to_be_visited[v]){
				yet_to_be_visited[v] = false;
				depth[v] = depth[u] + 1;
				next_vertex_to_visit.push(v);
			}
		}
	}
}

int main(){
	int n;
	scanf("%d", &n);
	bool adjmtrx[MAX][MAX];
	BuildAdjacencyMatrix(n, adjmtrx);
	int depth[n];
	Bfs(n, depth, adjmtrx);
	for (int id = 0; id < n; ++id)
		printf("%d %d\n", id + 1, depth[id]);
	return 0;
}