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>
using namespace std;

struct{
	int link;
	int stg;
}typedef LinkStage;

class Vertex{
	private:
		int* links;
		int index;
		int num;
		bool arrived;
		int id;
		int d;
	public:
		Vertex(int u,int k):id(u), num(k),links(new int[k]),index(0),arrived(false),d(-1){}
		void addLink(int);
		void enQueueLinks(queue<LinkStage>*,int);	
		void print();
};

void Vertex::addLink(int link){
	links[index] = link - 1;
	index++;
}

void Vertex::enQueueLinks(queue<LinkStage>* taskQueue,int dist){
	if(arrived)
		return;

	for(int i = 0; i < index;i++){
		LinkStage stage;
		stage.link = links[i];
		stage.stg = dist + 1;
		taskQueue->push(stage);
	}
	arrived = true;
	d = dist;
}

void Vertex::print(){
	cout << id << " " << d << endl;
}

int main(){
	int n;
	cin >> n;
	Vertex** vertexes = new Vertex*[n];
	for(int i = 0;i < n;i++){
		int u;
		cin >> u;
		int k;
		cin >> k;
		Vertex* vertex = new Vertex(u,k);
		for(int j = 0;j < k;j++){
			int v;
			cin >> v;
			vertex->addLink(v);
		}
		vertexes[u - 1] = vertex;
	}
	queue<LinkStage> task;
	vertexes[0]->enQueueLinks(&task,0);
	while(!task.empty()){
		LinkStage curr = task.front();
		task.pop();
		int nextI = curr.link;
		int d = curr.stg;
		vertexes[nextI]->enQueueLinks(&task,d);
	}
	for(int i = 0; i < n;i++){
		vertexes[i]->print();
	
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n;
vector<vector<int> > G;
vector<int> D;

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    D[src] = 0; // ?????????
    while(!Q.empty()){
        int cur = Q.front(); // ??????????´???????????????????
        Q.pop();
        for(int dst = 0; dst < n; ++dst){ // ???????????? dst ????????????
            if(G[cur][dst] == 1 && D[dst] == -1){ // cur??????dst???????????????,dst???????¨??????????
                D[dst] = D[cur] + 1;
                Q.push(dst); // dst????¨??????????????????????
            }
        }
    }
}

int main(){
    cin >> n;
    G = vector<vector<int> >(n, vector<int>(n, 0));
    D = vector<int>(n, -1);
    for(int i=0; i<n; ++i){
        int u;
        cin >> u;
        // 1~n???index?????¨????????????????????????????????????1????????????0~n-1????????????
        u -= 1;
        int k;
        cin >> k;
        for(int j=0; j<k; ++j){
            int v;
            cin >> v;
            v -= 1;
            G[u][v] = 1;
        }
    }
    bfs(0);
    for(int i=0; i<n; ++i){
        cout << i+1 << " " << D[i] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define pd(a,n) for(int i=0;i<n;i++)cout<<a[i]<<((i==n-1)?"\n":" ");cout<<endl;
#define pdd(a,n,m)for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define mp make_pair
#define pb push_back
#define ll long long
//#define __int64 lng long
#define P pair<int,int>
#define PP pair<P,int>

vector<int>G[111];
int dist[111];
int n;

int main(){
  cin>>n;
  rep(i,n)dist[i]=-1;

  rep(i,n){
    int a,k;
    cin>>a>>k;
    a--;
    rep(i,k){
      int x;
      cin>>x;
      x--;
      G[a].pb(x);
    }
    sort(G[a].begin(),G[a].end());
  }
  queue<P>bfs;
  bfs.push(mp(0,0));

  while(!bfs.empty()){
    int now=bfs.front().first;
    int cost=bfs.front().second;
    bfs.pop();
    if(dist[now]!=-1)
      continue;
    dist[now]=cost;
    for(int i=0;i<G[now].size();i++){
      int next=G[now][i];
      bfs.push(mp(next,cost+1));
    }
  }
  rep(i,n)cout<<i+1<<" "<<dist[i]<<endl;
  return 0;
}

C++: #include <cmath>
#include <queue>
#include <iostream>
using namespace std;

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

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

int main(){
 cin >> n;
 int u, k;
 int v[100000];
 for(int i=0; i<n; i++){
  cin >> u >> k;
  for(int j=0; j<k; j++){
   cin >> v[j];
   G[i+1][v[j]] = 1;
  }
 }
 bfs(1);
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
vector<vector<int>> G(100);
vector<int> d(100);
const int INF = 1<<28;
void bfs(){
    queue<int> Q;
    Q.push(0);
    fill(d.begin(),d.end(),INF);
    d[0]=0;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        for(auto a:G[u]){
            if(d[a]!=INF) continue;
            d[a]=d[u]+1;
            Q.push(a);
        }
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=0;i<n;++i){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;++j){
            int v;
            cin>>v;
            G[u-1].push_back(v-1);
        }
    }
    bfs();
    for(int i=0;i<n;++i)
        cout<<i+1<<' '<<(d[i]==INF?-1:d[i])<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MK make_pair 
typedef pair<int,int> pi;


int main(){
  const int MX=1e9+7;
  int a[1000][1000];
  fill(&a[0][0],&a[999][1000],MX);
  for(int i=0;i<1000;i++){
    a[i][i]=0;
  }
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int w;
    cin>>w;
    int m;
    cin>>m;
    for(int t=0;t<m;t++){
      int e;
      cin>>e;
      a[w][e]=1;
    }
  }
  for(int i=1;i<=n;i++){
    for(int t=1;t<=n;t++){
      for(int k=1;k<=n;k++){
	a[t][k]=min(a[t][k],a[t][i]+a[i][k]);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<(a[1][i]==MX? -1:a[a][i])<<endl;
  }


  
  return 0;
}