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++: /**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N]={-1};

void bfs(int n, int dd) {
	int k = adj[n][0];
	d[n] = dd++;
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	bfs(1, 0);

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

	return 0;
}

C++: #include<stdio.h>
struct
{
    int Q[10000];
    int head=0,last=0;
    void push_Back(int x)
    {
        Q[last]=x;
        last=(last+1)%10000;
    }
    int pop()
    {
        int x=Q[head];
        head=(head+1)%10000;
        return x;
    }
    bool Empty()
    {
     return head==last;
    }
}myqueue;
const int N=100;
const int nil=10000000;
int n;
int tu[N][N];
int juli[N];
void BFS()
{
    for (int i=0;i<n;i++)
        juli[i]=nil;
  myqueue.push_Back(0);
  juli[0]=0;
  int u;
  while(!myqueue.Empty())
  {
      u=myqueue.pop();
      for (int v=0;v<n;v++)
      {
          if(tu[u][v]==0) continue;
          if(juli[v]!=nil) continue;
          juli[v]=juli[u]+1;
          myqueue.push_Back(v);
      }
  }
  for (int i=0;i<n;i++)
  {
      printf("%d %d\n",i+1,((juli[i]==nil)?(-1):juli[i]));
  }
}
int main()
{
    int u,v,k;
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
        scanf("%d%d",&u,&k);
        u--;
        for (int j=0;j<k;j++)
        {
            scanf("%d",&v);
            v--;
            tu[u][v]=1;
        }
    }
    BFS();
    return 0;
}

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

using namespace std;

static const int MAX=100;
static const int INF =(1<<21);

int n,BY[MAX][MAX];
int dist[MAX];//距離で訪問状態を管理する

void bfs(int);

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

    scanf("%d",&n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++) BY[i][j]=0;
    }
    for(i=0;i<n;i++){
        scanf("%d%d",&u,&k);
        u--;
        for(j=0;j<k;j++){
            scanf("%d",&v);
            v--;
            BY[u][v]=1;
        }
    }
    bfs(0);

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

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


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

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

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] == 0) continue;
            if (d[v] != INFTY) continue;
            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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll tmp1,tmp2,tmp3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;
void swap(ll *x,ll *y){
  ll tmp;
  tmp = *x;
  *x = *y;
  *y = tmp;
}
//vector<ll> visited(100,0);
vector<ll> vec[100];
vector<ll> cou(100,-1);
ll sum = 0;
queue<pair<ll,ll>> q;
void bfs(ll k,ll num){
  q.push(make_pair(k,num));
  pair<ll,ll> piv;
  while(!q.empty()){
    piv = q.front();
    q.pop();
    if(cou[piv.first] != -1){
      continue;
    }
    cou[piv.first] = piv.second;
    for(auto itr:vec[piv.first]){
      q.push(make_pair(itr,piv.second+1));
    }
  }
  cou[k] = num;

}
int main(){
  ll n;
  cin >> n;
  ll u,k,v;
  rep(i,n){
    cin >> u >> k;
    u--;
    rep(j,k){
      cin >> v;
      v--;
      vec[u].push_back(v);
    }
  }
  bfs(0,0);
  rep(i,n){
    cout << i+1 << " " << cou[i] << endl;
  }
}

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

using Graph = std::vector<std::vector<uint32_t>>;

int main() {
  uint32_t n = 0;
  std::cin >> n;
  Graph G(n + 1);
  for (size_t i = 1; i < n + 1; i++) {
    uint32_t k = 0;
    std::cin >> i >> k;
    if (k > 0) G[i] = std::vector<uint32_t>(k);
    for (auto&& g : G[i]) std::cin >> g;
  }

  std::vector<int32_t> dist(n + 1, -1);
  dist[1] = 0;
  std::queue<uint32_t> Q({1});
  while (!Q.empty()) {
    uint32_t node = Q.front();
    Q.pop();
    for (auto&& to : G[node]) {
      if (dist[to] != -1) continue;
      Q.push(to);
      dist[to] = dist[node] + 1;
    }
  }

  for (size_t i = 1; i < dist.size(); i++) {
    std::cout << i << " " << dist[i] << std::endl;
  }

  return 0;
}

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

int n;
int k;
int m;

int main() {
  cin >> n;
  int V[n];

  // int型の2次元配列(3×4要素の)の宣言
  vector<vector<int>> A(n, vector<int>(n));

  for (int i=0; i<n; i++) {
    cin >> V[i];
    cin >> k;
    if (k==0) {
      continue;
    } else {
      for (int j =0; j<k; j++) {
        cin >> m;
        A[i][m-1] = 1;
      }
    }
  }

  int D[n];
  for (int i=0; i<n; i++) D[i] = -1;
  D[0] = 0;

  queue<int> Q;
  Q.push(0);
  while(! Q.empty()) {
    int cur = Q.front();
    Q.pop();

    for (int i=0; i<n; i++) {
      if (A[cur][i] == 1 && D[i] == -1) {
        D[i] = D[cur] + 1;
        Q.push(i);
      }
    }
  }

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