Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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

int n;
vector<int> d;
vector<vector<int>> graph;
vector<bool> bfsflg(n, 0);
void bfs(int z){
    queue<int> q;
    bfsflg[z] = 1;
    d[1]=0;
    q.push(z);

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

        for(int i : graph[v]){
            if(!bfsflg[i]){
                bfsflg[i] = 1;
                d[i]=d[v]+1;
                q.push(i);
            }
        }
    }
}

int main() {
    cin >> n;
    d.assign(n+1, -1);
    graph.assign(n+1, {});
    bfsflg.assign(n+1, 0);
    int u, k, tmp;
    for(int i=1; i<=n; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> tmp;
            graph[i].push_back(tmp);
        }
    }
    
    bfs(1);

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

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  int v[n];
  rep(i,n){
    m[i]=10000;
    v[i]=0;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
        if(d[v-1][u-1]!=1){
          d[u-1][v-1]=1;
        }
      }
    }
  }   

  q1.push(0);
  while(!q1.empty()){
    int u = q1.front(); q1.pop();
      rep(j,n){
        if(d[u][j]==1 && v[j]==0){
          q1.push(j);
          m[j] = min(m[j],m[u]+1);
        }
      }
    v[u]=1;
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}

C++: #include <vector>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int n, u, k, v, dp[101];
bool li[101][101];


int main(){
	fill(dp, dp + 101, -1);
	for (int i = 0; i < 101; i++) {
		fill(li[i], li[i] + 101, false);
	}
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			li[u][v] = li[v][u] = true;
		}
	}
	vector<int> l = vector<int>(1, 1);
	vector<int> l2 = vector<int>();
	dp[1] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < l.size(); j++) {
			for (int k = 1; k <= n; k++) {
				if (li[l[j]][k] && dp[k] < 0) {
					dp[k] = i;
					l2.push_back(k);
				}
			}
		}
		l2.swap(l);
		vector<int>().swap(l2);
	}
	for (int i = 1; i <= n; i++) {
		cout << i << " " << dp[i] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define PI 3.14159265358979323846
#define vll vector<long long>
#define vvll vector<vector<long long>>
using namespace std;
using ll = long long;
using ld = long double;

// ----本問について----
// 問題：Breadth First Search
// 問題文URL：https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C
// 所要時間：
// 学んだこと：
// ------------------

// ----ここから本問のソースコード----
static const ll N_MAX = 110;

// グローバル変数（入力）
ll n, A[N_MAX][N_MAX];

// 入力
void input() { cin >> n; }

// 幅優先探索
void bfs() {
  queue<ll> q;

  // 距離を-1で初期化
  vll d(n + 1, -1);

  // 始点の処理
  d[1] = 0;
  q.push(1);

  // 探索
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    rep(i, n) {
      if (A[v][i + 1] == 1 && d[i + 1] == -1) {
        q.push(i + 1);
        d[i + 1] = d[v] + 1;
      }
    }
  }
  rep(i, n) { cout << i + 1 << " " << d[i + 1] << endl; }
}

void solve() {
  // 隣接リストを隣接行列に変換して二次元配列Aに格納
  rep(i, n) {
    ll v, k;
    cin >> v >> k;
    rep(j, k) {
      ll v2;
      cin >> v2;
      A[v][v2] = 1;
    }
  }

  bfs();
}

int main() {
  input();
  solve();
  return 0;
}

C++: #include <iostream>
#include <ios>
#include <vector>
#include <utility>

using namespace std;

void read_data(int n, vector<vector<int>> & Es){
    int u = 0, k = 0, v = 0;
    while (n--){
        cin >> u;
        cin >> k;
        while (k--){
            cin >> v;
            Es[u - 1].push_back(v - 1);
        }
    }
}

void wfs(int node, vector<int> & dist, vector<vector<int>> & Es){
    int d = 0;
    vector<int> frontiers;
    frontiers.push_back(node);
    while (!frontiers.empty()){
        vector<int> new_frontiers;
        for (auto u : frontiers){
            if (dist[u] == -1){
                dist[u] = d;
                for (auto v : Es[u]){
                    new_frontiers.push_back(v);
                }
            }
        }
        frontiers = move(new_frontiers);
        ++d;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    vector< vector<int> > Es(n);
    vector<int> dist(n, -1);
    read_data(n, Es);
    wfs(0, dist, Es);
    for (int i = 0; i != n; ++i) cout << i + 1 << ' ' << dist[i] << endl;
    
    return 0;
}

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

#define N 100
#define MAX 100000

int A[N + 1][N + 1] = {};
int n, u, k, v;
int t1[N + 1]; //発見時刻

//sは始点,tは時間
void bfs(int s, int t) {
    t1[s] = t;
    for(int i = 1; i <= n; ++i) {
        if(A[s][i] == 1 && t < t1[i]) {
            bfs(i, t + 1);
        }
    }
}

int main(void) {
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        cin >> u >> k;
        for(int j = 0; j < k; ++j) {
            cin >> v;
            A[u][v] = 1;
        }
    }
    
    for(int i = 0; i <= n; ++i) t1[i] = MAX;
    
    bfs(1, 0);
    
    for(int i = 1; i <= n; ++i) {
        cout << i << ' ';
        if(t1[i] == MAX) cout << -1 << endl;
        else cout << t1[i] << endl;
    }
    
    return 0;
}