Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
void bfs(int);
int n,M[N][N],d[N],u,i,j;
string color[N];
int main(){
  int inu,k,v;
  cin >> n;
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      M[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    cin >> inu >> k;
    for(j=0;j<k;j++){
      cin >> v;
      M[inu][v]=1;
    }
  }
  bfs(1);
  for(i=1;i<=n;i++){
    if(d[i]>1000000) cout << i << ' ' << -1 << endl;
    else cout << i << ' ' << d[i] << endl;
  }
  return 0;
}
void bfs(int s){
  queue<int> q;
  for(i=1;i<=n;i++){
    color[i]="WHITE";
    d[i]=1000000000;
  }
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=true){
    u=q.front();
    q.pop();
    for(i=1;i<=n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class Graph{
public:
  int v;
  int cost = inf;
  int ftime = -1;
  int ltime = -1;
  vector<int> e;
};
vector<Graph> g;
int cnt = 0;
void dfs(int pos){
  if( g[pos].ftime != -1 ) return;
  cnt++;
  g[pos].ftime = cnt;
  for(int i=0; i<g[pos].e.size(); i++){
    dfs(g[pos].e[i]);
  }
  cnt++;
  g[pos].ltime = cnt;
}
void bfs(int pos){
  for(int i=0; i<g[pos].e.size(); i++){
    // if( g[ g[pos].e[i] ].cost != inf ) continue;
    g[ g[pos].e[i] ].cost = min(g[ g[pos].e[i] ].cost, g[pos].cost + 1);
    bfs(g[pos].e[i]);
  }
}

int main(void) {

  int n;
  cin >> n;
  g.resize(n);
  int u, k;
  for(int i=0; i<n; i++){
    cin >> u >> k;
    u--;
    g[u].v = u;
    g[u].e.resize(k);
    for(int j=0; j<k; j++){
      cin >> g[u].e[j];
      g[u].e[j]--;
    }
    sort(g[u].e.begin(), g[u].e.end());
  }

  g[0].cost = 0;
  bfs(0);
  for(int i=0; i<n; i++) cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl;

  // for(int i=0; i<n; i++) dfs(i);
  // for(int i=0; i<n; i++){
  //   cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl;
  // }



  return 0;
}

// EOF

C++: #include <iostream>
#include <stdio.h>
#include <queue>
 
using namespace std;
#define N 100
#define INF 2000000
 
int n, M[N][N];
int d[N];
 
void bfs(int s){
    int i,u,v;
 
    queue<int> q;
    q.push(s);
    for(i = 0; i < n; i++){
        d[i] = INF;
    }
 
    d[s] = 0;
 
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(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(i = 0; i < n; i++){
        if(d[i] == INF){
            printf("%d -1\n", i+1);
        }else{
            printf("%d %d\n", i+1, d[i]);
        }
    }
}
 
 
int main(void){
    int i,j,u,k,v;
 
    scanf("%d", &n);
 
    for(i = 0;i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
 
    for(i = 0; i < n; i++){
      cin >> u >> k;
        u--;
        for(j=0;j<k;j++){
          cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
 
    bfs(0);
 
    return 0;
}

C++: #include<iostream>
#include<stdio.h>

using namespace std;
int d[101];

int n;
int M[101][101];
int Q[101];
int head = 0;
int tail = 0;

void enqueue(int x) {
	Q[tail] = x;
	if (tail == (tail - head)) {
		tail = 1;
	}
	else {
		tail = tail + 1;
	}
}

void BFS(int s) {
	enqueue(s);
	d[s] = head;
	while (head != tail) {
		s = Q[head];
		head = head + 1;
		
		for (int j = 1; j < n + 1; j++) {
			if (M[s][j] == 1) {
				if(d[j] == -1){
					d[j] = d[s] + 1;
					enqueue(j);
				}

			}
		}

	}
}

int main() {
	int  u, k, v;//頂点数、頂点の番号、出次数、隣接する頂点の番号
	

	cin >> n;



	//初期化
	for (int i = 0; i < 101; i++) {
		d[i] = -1;
		
		for (int j = 0; j < 101; j++) {
			M[i][j] = 0;

		}
	}


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


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

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1};
int dx[] = {1, -1, 0, 0};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int table[n + 1][n + 1]; rep(i, n + 1) rep(j, n + 1) table[i][j] = 0;
    rep(j, n) {
        int u, k; cin >> u >> k;
        int v;
        rep(l, k) {
            cin >> v;
            table[u][v] = 1;
        }
    }
    int mc[n + 1]; rep(i, n + 1) mc[i] = inf;
    queue<int> q;
    q.push(1);
    mc[1] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        rrep(i, 2, n + 1) {
            if (table[now][i] == 1 && mc[i] == inf) {
                mc[i] = mc[now] + 1;
                q.push(i);
            }
        }
    }
    rrep(i, 1, n + 1) {
        cout << i << " " << (mc[i] == inf ? -1 : mc[i]) << endl;
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

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