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<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

int main(){

  int n;
  cin >> n;

  vector<vector<int>> G(n);

  for(int i=0;i<n;i++){
    int u,k;
    cin >> u >> k;
    for(int j=0;j<k;j++){
      int a;
      cin >> a;
      G[u-1].push_back(a-1);
    }
  }

  vector<int> d(n,-1);
  queue<int> que;

  que.push(0);
  d[0]=0;

  while(!que.empty()){
    int v=que.front();
    que.pop();

    for(int i=0;i<G[v].size();i++){
      if(d[G[v][i]]==-1){
        d[G[v][i]]=d[v]+1;
        que.push(G[v][i]);
      }
    }
  }

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


}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define N 100
#define INF 1e9

queue<int> Q;
int n,M[N][N];
int dist[N];

void bsf(int l){
	Q.push(l);
	for(int i = 0;i < n;i++) dist[i] = INF;

	dist[l]=0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<n;i++){
			if(M[u][i] == 0)continue;
			if(dist[i] != INF)continue;
			dist[i] = dist[u] + 1;
			Q.push(i);
		}
	}

	for(int i=0;i<n;i++){ 
		if(dist[i] == INF)dist = -1;
		
		cout<<i+1<<" "<<dist[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;
		}
	}
	bsf(0);
	return 0;
}

C++: #include<bits/stdc++.h>
#include<string.h>
#define MAX 101
#define WHITE -1
#define GRAY 2
#define BLACK 1
#define EMPTY -1
#define INF 10000000
using namespace std;

int G[MAX][MAX]={},d[MAX]={},color[MAX]={};
queue<int> Q;

void print(int n){
  for(int i=1;i<n+1;i++){
      if(i!=1&&d[i]==0)cout<<i<<" "<<-1<<endl;
    else cout<<i<<" "<<d[i]<<endl;
  }
}

void bfs(int n,int s){
    int u,v;
    color[s]=GRAY;
    Q.empty();
    Q.push(s);
    while(!Q.empty()){
            u=Q.front();
            Q.pop();
            for (int v = 1; v <= n; ++v) {
                if(G[u][v]&&color[v]==WHITE){
                    color[v]=GRAY;
                    d[v]=d[u]+1;
                    Q.push(v);
                }
            }
            color[u]=BLACK;
        }
}

int main(){
  int n,i,k,j,A,B,V[MAX]={};
  cin>>n;
  for(i=0;i<MAX;i++){
    for(j=0;j<MAX;j++){
        G[i][j]=0;
    }
  }
  for(i=0;i<n;i++){
    cin>>A>>k;
    for(j=0;j<k;j++){
      cin>>B;
      G[A][B]++;
    }
  }

    for(int u=0;u<=n;u++){
        color[u]=WHITE;
        d[u]=0;
    }
  bfs(n,1);
  print(n);
}

C++: #include <bits/stdc++.h>

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

int n;
int u, k, v;
int hour = 0;
int pos;
int d[101];
int gra[101][101] = {};
int color[101] = {};
queue<int>que;



int main(void) {
	cin >> n;
	
	FOR(i, 1, n) {
		cin >> u >> k;
		FOR(t, 1, k) {
			cin >> v;
			gra[u][v] = 1;
		}
	}

	FOR(i, 1, n) {
		d[i] = -1;
	}

	que.push(1);
	d[1] = hour;
	color[1] = 1;
	
	while (que.empty() != 1) {
		pos = que.front();
		que.pop();
		FOR(i, 1, n) {
			if (gra[pos][i] == 1 && color[i] == 0) {
				que.push(i);
				d[i] = d[pos] + 1;
				color[i] = 1;
			}
		}
	}

	FOR(i, 1, n) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

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

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

int M[N][N] = {0}, d[N], n;

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

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

  for (i = 0; i < n; i++) {
    d[i] = INFTY;
  }

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs();

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using pll=pair<ll,ll>;

#define fi first
#define se second
#define pb push_back
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define MOD 1000000007

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 (b<a) { a=b; return 1; } return 0; }
vector<vector<int> > G(110, vector<int>());

int main() {
	int N;
	cin >> N;
	int u,k;
	rep(i,N) {
		cin >> u >> k;
		u--;
		G[u] = vector<int>(k);
		rep(j,k) cin >> G[u][j], G[u][j]--;
	}

	queue<int> q;
	vector<int> dist(N, 1e9);
	dist[0] = 0;
	q.push(0);
	while(!q.empty()) {
		int p = q.front();
		q.pop();
		for(auto k : G[p]) {
			if(dist[k] > dist[p] + 1) {
				dist[k] = dist[p] + 1;
				q.push(k);
			}
		}
	}
	rep(i,N) if(dist[i] == 1e9) dist[i] = -1;
	rep(i,N) cout << i+1 << " " << dist[i] << endl;


  return 0;
}