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 <cstdio>
#include <stdlib.h>
#include <queue>
using namespace std;
static const int N = 100, WHITE = 0, GREY = 1, BLACK = 2;

int M[N][N], color[N], d[N], n, dis;
queue<int> q;


void label_not_reach()
{
	for(int u = 0; u < n; u++)
	{
		if(color[u] == WHITE) d[u] = -1;
	}
}


void bfs()
{
	int u = 0;
	
	q.push(u);
	
	color[u] = GREY;
	
	while(!q.empty())
	{
		u = q.front();
		for(int v = 0; v < n; v++)
		{
			if(M[u][v] == 1 && color[v] == WHITE)
			{
				color[v] = GREY;
				q.push(v);
				d[v] = d[u] + 1;
			}
		}
		color[u] = BLACK;
		q.pop();	
	}
	
	label_not_reach();
}




int main() 
{
	dis = 0;
	scanf("%d", &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++)
	{
		color[i] = WHITE;
		d[i] = 0;
	}
	
	for(int i = 1; i <= n; i++)
	{
		int u,k,v;
		scanf("%d %d", &u, &k);
		for(int j = 0; j < k; j++)
		{
			scanf("%d", &v);
			M[u-1][v-1] = 1;
		}
	}
	
	bfs();
	
	for(int i = 0; i < n; i++)
	{
		printf("%d %d\n", i+1, d[i]);
	}
	
//	system("pause");
	return 0;
}

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

int G[223][223];
int dis[223];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  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();
    if(dis[u.pos] < 1000000) continue;

    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[u.pos][i] == 0) 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 <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;
static const int N=100;

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


void bfs(int s){
    queue<int> Q;
    Q.push(s);
    d[s]=0;
    int u,v;
    while(!Q.empty()){
        u=Q.front();Q.pop();
        for(v=0;v<n;v++){
            if(M[u][v]==0) continue;
            if(d[v]!=-1) continue;
            d[v]=d[u]+1;
            Q.push(v);
        }
    }
    for(int i=0;i<n;i++) printf("%d %d\n",i+1,d[i]);
}

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;
        }
    }
    for(int i=0;i<n;i++) d[i]=-1;
    bfs(0);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, 0, k) {
            int v; cin >> v;
            v--;
            G[u].push_back(v);
        }
    }
    vector<int> dist(N, -1);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;
    while(!Q.empty()) {
        int cur = Q.front(); Q.pop();
        for(auto &to : G[cur]) {
            if(dist[to] != -1) continue;
            Q.push(to);
            dist[to] = dist[cur] + 1;
        }
    }
    rep(i, 0, N) cout << i + 1 << " " << dist[i] << '\n';
    return 0;
}

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

#define MAX 100
#define NIL -1

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

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

int main(void){

  make_graph();
  check_depth();
  print();
  
  return 0;
}

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

using namespace std;

static const int N=100;
static const int INF=1e+9;

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]=INF;
	}
	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]!=INF)continue;

			d[v]=d[u]+1;
			q.push(v);
		}
	}

	for(int i=0;i<n;i++){
		if(d[i]==INF){
			cout<<i+1<<" "<<-1<<endl;
		}else{
			cout<<i+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 <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[100],f[100],w,h,n,sx,sy,gx,gy,e,g[100];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=0;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}