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;

int G[123][123];
int dis[123];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for(int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for(int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st) {
    1, 0
  });

  while(!q.empty()) {

    st u = q.front();
    q.pop();
    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[i][u.pos] == 0) continue;
      if(dis[i] <= u.v) continue;
      q.push((st){i, u.v + 1});
    }
  }

  for(int i=1;i<=n;i++) {
    printf("%d %d\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

C++: #include <queue>
#include <map>
#include <cstdio>
using namespace std;
main(){
	int t=0,T,i,j,k;
	map<int,vector<int> >a;
	for(scanf("%d",&T);t<T;t++)for(scanf("%d%d",&i,&k);k--;a[i].push_back(j))scanf("%d",&j);
	map<int,int>m;
	queue<int>q;
	for(q.push(1),m[1]=1;!q.empty();){
		k=q.front();q.pop();
		for(i=0;i<a[k].size();i++)if(!m[a[k][i]])m[a[k][i]]=m[k]+1,q.push(a[k][i]);
	}
	for(i=1;i<=T;i++)printf("%d %d\n",i,m[i]-1);
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;

  queue<int> Q;
  Q.push(0);

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

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i] != INF){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

  fill(G[0],G[MAX],INF);
  for(int i = 0 ; i < n ; i++){
    int u,k,v;
    cin >> u >> k;
    for(int j = 0 ; j < k ; j++){
      cin >> v;
      G[u-1][v-1] = 1;
    }
  }

  bfs();

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

  return 0;
}

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

// ------ Class ------ //
class Graph1 {
public:
	// ------ Variables ------ //
	static const int INF = 1000000000;
	int V, E; vector<vector<int> > G;

	// ------ Constructors ------ //
	Graph1() : V(0), E(0), G(vector<vector<int> >()) {}
	Graph1(int v) : V(v), E(0), G(vector<vector<int> >(v)) {}
	Graph1(vector<vector<int> > g) : V(g.size()), G(g) { for (int i = 0; i < g.size(); i++) E += g[i].size(); }

	// ------ Basic Functions ------ //
	int size() { return V; }
	void add1(int v1, int v2) { G[v1].push_back(v2); E++; }
	void add2(int v1, int v2) { add1(v1, v2); add1(v2, v1); }

	// ------ Operators ------ //
	friend bool operator==(const Graph1& g1, const Graph1& g2) { return g1.G == g2.G; }
	friend bool operator!=(const Graph1& g1, const Graph1& g2) { return g1.G != g2.G; }
	vector<int> operator[](int x) { return G[x]; }

	// ------ Algorithms ------ //
	vector<int> dist(int s) {
		queue<int> que; que.push(s);
		vector<int> d(V, INF); d[s] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i = 0; i < G[u].size(); i++) {
				if (d[G[u][i]] == INF) {
					d[G[u][i]] = d[u] + 1;
					que.push(G[u][i]);
				}
			}
		}
		return d;
	}
};

// ------ Main ------ //
int n, u, k, v;
int main() {
	cin >> n; Graph1 g(n);
	for(int i = 0; i < n; i++) {
		cin >> u >> k;
		for(int j = 0; j < k; j++) cin >> v, g.add1(u - 1, v - 1);
	}
	vector<int> d = dist(0);
	for(int i = 0; i < n; i++) cout << i + 1 << ' ' << d[i] << endl;
}

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<100;i++){
        for(int j=0;j<100;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

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

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    std::cout<<"\n";

    //output
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<std::endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#define P pair<int, int>

using namespace std;

int n, d[109];
vector<int> G[109];
bool check[109];


int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    int u, k, v;
    cin >> u >> k;
    for(int i=0;i<k;i++){
      cin >> v;
      G[u].push_back(v);
    }
  }
  memset(check, false, sizeof(check));
  memset(d, -1, sizeof(d));
  queue<P> Q;
  Q.push(P(1, 0));
  check[1]=true;
  while(!Q.empty()){
    P p = Q.front();Q.pop();
    int n=p.first, dis = p.second;
    d[n] = dis;
    for(int i=0;i<G[n].size();i++){
      if(!check[G[n][i]]){
        check[G[n][i]]=true;
        Q.push(P(G[n][i], dis + 1));
      }
    }
  }
  for(int i=1;i<=n;i++)cout << i << ' ' << d[i] << endl;
}