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<bits/stdc++.h>
#define INF 10000000
using namespace std;

int z[100],n;
int a[100][100];

void bfs(){
  int m;
  z[0]=0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    m=q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(a[m][i]==1){
	q.push(i);
	z[i]=min(z[i],z[m]+1);
      }
    }
  }
}


int main(){
  int p,x;
  for(int i=0;i<100;i++){
    z[i]=INF;
    for(int j=0;j<100;j++) a[i][j]=0;
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>p>>p;
    for(int j=0;j<p;j++){
      cin>>x;
      a[i][x-1]=1;
    }
  }
  bfs();
  for(int i=0;i<n;i++){
    if(z[i]!=INF) cout<<i+1<<" "<<z[i]<<endl;
    else cout<<i+1<<" "<<-1<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,m[101][101];
int d[101];
void bfs(int s){
  queue<int>q;
  q.push(s);
  for(int i=0;i<n;i++){
    d[i]=INF;
  }
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n; v++){
      if(m[u][v]==0){
	continue;
      }
      if(d[v]!=INF){
	continue;
      }
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  }
}
int main(){
  int u,k,v;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v;
      m[u-1][v-1]=1;
    }
  }
  bfs(0);
  return(0);
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int G[101][101];
int d[101];

void explore(int id, int n,int cnt)
{
	if (d[id] == -1)
	{
		d[id] = cnt;
	}
	else
	{
		d[id] = min(d[id], cnt);
	}

	for (int i = 1; i <= n; i++)
	{
		if (G[id][i] == 1)
		{
			explore(i, n, cnt + 1);
		}
	}
}

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

	for (int i = 0; i < n; i++)
	{
		int id, k;
		cin >> id >> k;

		for (int i = 0; i < k; i++)
		{
			int v;
			cin >> v;
			G[id][v] = 1;
		}
	}

	for (int i = 1; i <= 100; i++)
	{
		d[i] = -1;
	}

	explore(1, n, 0);

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

	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

static const int N =100;
static const int INFTY =(1<<21);

void bfs(int s){
  queue<int> q;
  q.push(s);
  for(int i=0;i<n;i++) d[i]=INFTY;
  d[s]=0;
  int u;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==1){
        d[v]=d[u]+1;
        q.push(v);
      }
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==INFTY)? (-1):d[i])<< endl;
  }
}

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

C++: #include <iostream>
//#define int long long
//#define _DEBUG
#include <queue>
#include <vector>
using namespace std;
int N, T[107][107], D[107]; //T[i][j]:i->T[i][j]の辺
int const infty = -1, none = -9;
queue<int> Q;
void solve(){
    Q.push(0);
    D[0] = 0;
    while(true){
#ifdef _DEBUG
        cout << "Q1 ";
        int length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        int top = Q.front(); Q.pop();
#ifdef _DEBUG
        cout << "top " << top << endl << "Q2 ";
        length = Q.size();
        for(int k=0; k<length; k++){
            int qf = Q.front(); Q.pop();
            cout << qf << " ";
            Q.push(qf);
        }cout << endl; 
#endif
        for(int j=0; j<N; j++){
#ifdef _DEBUG
            cout << j << ", " << T[top][j] << endl;
            cout << "top " << top << endl << "Q3 ";
            length = Q.size();
            for(int k=0; k<length; k++){
                int qf = Q.front(); Q.pop();
                cout << qf << " ";
                Q.push(qf);
            }cout << endl; 
#endif
            if(T[top][j] == none)break;
            if(D[ T[top][j] ] != infty)continue;
            Q.push( T[top][j] );
            D[ T[top][j] ] = D[top]+1;
        }
#ifdef _DEBUG
        cout << "D " ;
        for(int k=0; k<N; k++) cout <<D[k]<<" ";
        cout << endl;
#endif
        if(Q.empty()) break;
    }
}
void get(){
    cin >> N;
    for(int i=0; i<N; i++){
        D[i] = infty;
        for(int j=0; j<N; j++){
            T[i][j] = none;
        }
    }
    for(int i=0; i<N; i++){
        int i_, k;
        cin >> i_ >> k;
        for(int j=0; j<k; j++){
            int vj;
            cin >> vj;
            T[i][j] = vj-1;
        }
    }
}

signed main(){
    get();
    
#ifdef _DEBUG
    for(int k=0; k<N; k++) cout << D[k] << " ";
    cout << endl;
    for(int k=0; k<N; k++){
        for(int l=0; l<N; l++){
            if(T[k][l] == none)break;
            cout << T[k][l] << " ";
        }
        cout << endl;
    }
#endif
    solve();
    for(int i=0; i<N; i++){
        cout << i+1 << " " << D[i] << endl;
    }
}

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

using namespace std;

int D[100100];//始点からの距離を入れる

int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  // 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(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  cout << 1 <<" "<< 0 <<enl;
  for (int i = 1; i < n; ++i)
  {
    if(D[i]==0){
      cout << i+1 << " "<< -1 <<endl;
    }else{
      cout << i+1 <<" "<< D[i]<<endl;
    }
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}