Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

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

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

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

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

using namespace std;
using Graph=vector<vector<int>>;

void breadth_firstsearch(Graph &graph,int vertex,queue<int> qdata,vector<int> &distance){
  distance.at(vertex)=0;
  qdata.push(vertex);
  while(!qdata.empty()){
    int ver=qdata.front();
    qdata.pop();
    for(int v : graph.at(ver)){
      if(distance.at(v)!=-1) continue;
      distance.at(v)=distance.at(ver)+1;
      qdata.push(v);
    }
  }
}


int main(void) {
	int size, vnum, adjnum, temp;
	cin >> size;
	Graph graph(size);
	queue<int> qdata;
	vector<int> distance(size,-1);

	for (int i = 0; i < size; i++) {
		cin >> vnum >> adjnum;
		for (int j = 0; j < adjnum; j++) {
			cin >> temp;
			graph.at(vnum - 1).push_back(temp - 1);
		}
	}
  breadth_firstsearch(graph,0,qdata,distance);

	for (int i = 0; i < size; i++) {
		cout << i + 1 << " " <<distance.at(i)<< endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0;i<(n);i++)
const int INF = 1001001001;
int n, u, k;
int D[100];
vector<int> edges[100];
void bfs(int src) {
	
	
	queue<int> Q;	
	Q.push(src);
	D[src] = 0;//D[*]が始点から点*への距離
	while (!Q.empty()) {
		int cur = Q.front();//curは今取り扱ってる点
		Q.pop();
		
		for (int dst = 0; dst < n; dst++) {//dstはcurから移動可能な点
			vector<int>::iterator result = find(edges[cur].begin(), edges[cur].end(), dst);
			if (result != edges[cur].end() && D[dst] == INF) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
}

int main() {
	cin >> n; //n頂点
	

	for (int i = 0; i < n; i++)//iは頂点
	{
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			int l;
			cin >> l;
			edges[u - 1].push_back(l-1);
		}//入力終わり
		D[i] = INF;
	}
		bfs(0);
	for (int i = 0; i < n; i++) {
		if (D[i] != INF)cout << i + 1 << " " << D[i] << endl;
		else cout << i + 1 << " " << -1 << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
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;
        rep(i,n) {
            rep(j,n) {
                M[i][j] = 0;
            }
        }
        cin >> n;
        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<vector>
#include<queue>
#define WHITE 0 //未発見
#define GRAY 1 //発見
#define BLACK 2 //探索済み
#define NIL -1
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector< vector<int> > table;

void bfs(table& m,int s)
{
    vector<int> color(m.size(),WHITE);
    vector<int> dist(m.size(),NIL);
    queue<int> q;
    q.push(s);
    color[s]=GRAY;
    dist[s]=0;

    while (!q.empty())
    {
        int u=q.front();q.pop();
        rep(v,m.size())
        {
            if(dist[v]!=NIL)continue;
            if(m[u][v]==1 && color[v]==WHITE)
            {
                dist[v]=dist[u]+1;
                color[v]=GRAY;
                q.push(v);
            }
        }
        color[u]=BLACK;
    }
    
    rep(i,m.size()) cout<<i+1<<" "<<dist[i]<<endl;
}

int main()
{
    int n;
    cin>>n;
    table m(n,vector<int>(n,0));
    rep(i,n)
    {
        int u,k;
        cin>>u>>k;
        rep(j,k)
        {
            int v;
            cin>>v;
            m[u-1][v-1]=1;
        }
    }

    bfs(m,0);

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