Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INF = (1<<21);

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++){
		cout << i+1 << " " <<((d[i] == INF)?(-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<iostream>
#include<queue>
using namespace std;
const int MAX = 100;
int n, G[MAX + 1][MAX + 1];
bool book[MAX + 1] = { false };
int path[MAX + 1];
void breathFSearch(int orign)
{
	queue<int> Q;
	Q.push(orign);
	book[orign] = true;
	while (!Q.empty())
	{
		int u = Q.front();
		for (int v = 1; v <= n; v++)
		{
			if (G[u][v] && !book[v])
			{
				Q.push(v);
				book[v] = true;
				if(path[u]!=-1)
					path[v] = path[u]+1;
			}
		}
		Q.pop();
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 1; j <= d; j++)
		{
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
		path[i] = -1;
	path[1] = 0;
	for(int i=1;i<=n;i++)
		if(!book[i])
			breathFSearch(i);
	for (int i = 1; i <= n; i++)
		cout << i << " " << path[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

vector<vector<int> > G;
// bfs 幅優先探索
int bfs(int s)
{
    vector<int> cost(G.size(), -1);

    cost[s] = 0;
    queue<int> pos_queue;
    pos_queue.push(s);

    while (pos_queue.size())
    {
        int pos = pos_queue.front();
        pos_queue.pop();
        rep(i, G[pos].size())
        {
            int next = G[pos][i];

            if (cost[next] == -1)
            {
                cost[next] = cost[pos] + 1;
                pos_queue.push(next);
            }
        }
    }
    rep(i, G.size())
    {
      cout << i + 1 << " " << cost[i] << endl;
    }
}

int main()
{
  int N;
  cin >> N;
  G.resize(N);
  rep(i, N)
  {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k)
    {
      int v;
      cin >> v;
      G[u].push_back(v - 1);
    }
  }
  bfs(0);
  return 0;
}

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

int main(){
  int n, u, k, tmp, lp;
  vector<int> d;
  vector< vector<int> > v;
  vector< queue<int> > q;
  
  cin >> n;
  v.resize(n+1);
  d.resize(n+1, 1000000007);
  q.resize(1);
  d[1]=0;
  q[0].push(1);
  
  for (int r = 0; r < n; r++) {
	cin>> u >> k;
    for(int i=0;i<k;i++){
      cin>> tmp;
      v[u].push_back(tmp);
    }
  }
  
  for(int r=0;r<n;r++){
    //cout<< "r=" << r <<endl;
    q.resize(q.size()+1);
    lp=q[r].size();
    for(int i=0;i<lp;i++){
      tmp=q[r].front();
      //cout<< "tmp=" << tmp <<endl;
      q[r].pop();
      for(int j=0;j<v[tmp].size();j++){
        //cout<< "push " << v[tmp][j] << "  val " << min(v[tmp][j], d[tmp]+1) <<endl;
        if(d[v[tmp][j]]>d[tmp]+1){
          q[r+1].push(v[tmp][j]);
      		d[v[tmp][j]]=min(d[v[tmp][j]], d[tmp]+1);
        }
      }
    }
  }
  
  for(int i=1;i<=n;i++){
    cout<< i << " ";
    if(d[i]==1000000007) cout<< "-1" <<endl;
    else cout<< d[i] <<endl;
  }
  
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;

int table[101][101];

void init(){
    REP(i,101){
        REP(j,101){
            table[i][j]=1e9;
            if(i==j)table[i][j]=0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    init();
    int u,k,v;
    REP(i,n){
        cin >> u >> k;
        REP(j,k){
            cin >> v;
            table[u-1][v-1]=1;
        }
    }

    REP(k,n){
        REP(i,n){
            REP(j,n){
                table[i][j]=min(table[i][j],table[i][k]+table[k][j]);
            }
        }
    }
    REP(i,n){
        cout << i+1 << " " << ((table[0][i]<n)?table[0][i]:-1);
        cout << endl;
    }
}

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

int main() {
    vector<int> G[101];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        while(k > 0) {
          int v; cin >> v; 
          G[u].push_back(v);
          k--;
        }
    }

    vector<int> dist(N+1, -1);
    queue<int> Q;
    Q.push(1);
    dist[1] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 1; i < N+1; i++) cout << i << ' ' << dist[i] << endl;
}

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]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

  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;
}