Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

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

	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<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;

using namespace std;

static const int N=100;
static const int INF =(1<<21);
int n,M[N][N];
int d[N];


void dfs(int s){
    queue<int> q;
    q.push(s);
    rep(i,n)d[i]=INF;
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(M[u][i]==0)continue;
            if(d[i]!=INF)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
    }
    rep(i,n){
        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;
        }
    }
    
    dfs(0);
    
    
    

return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i) {
        size_t k;
        scanf("%*d %zu", &k);
        for (size_t j=0; j<k; ++j) {
            size_t v;
            scanf("%zu", &v);
            --v;

            connect(g, i, v);
        }
    }

    queue<size_t> q; q.push(0);
    vector<size_t> d(n, -1); d[0]=0;
    while (!q.empty()) {
        size_t v=q.front(); q.pop();
        for (Edge &e: g[v]) {
            if (d[e.dst] > d[v] + 1) {
                d[e.dst] = d[v] + 1;
                q.push(e.dst);
            }
        }
    }

    for (size_t i=0; i<n; ++i)
        printf("%zu %zd\n", i+1, d[i]);

    return 0;
}

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

using namespace std;

static const int MAX = 100;
int n, m[MAX][MAX], vst[MAX];
int d[MAX];

struct ddat
{
	int nm;
	int d;
};


int next(int u) {

	for (int v = 0;v < n;v++) {
		if (m[u][v]) {
			return v;
		}
	}

	return -1;
}


void bfs() {

	queue<ddat> que;
	ddat  d1; d1.nm = 0;;d1.d = 0;
	que.push(d1);



	while (!que.empty())
	{
		ddat d2 = que.front();
		que.pop();
		vst[d2.nm] = 1;
		d[d2.nm] = min(d2.d, d[d2.nm]);

		for (int i = 0; i < n; i++)
		{
			if (m[d2.nm][i] != 0 && vst[i] == 0) {
				ddat dch;
				dch.nm = i;dch.d = d2.d + 1;
				
				que.push(dch);
			}
		}

	}

	return;
}


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;
		for (int j = 0;j < k;j++) {
			cin >> v;
			m[u - 1][v - 1] = 1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		d[i] = 999'999;
	}

	bfs();

	for (int i = 0; i < n; i++) {
		if (d[i] == 999999) d[i] = -1;

		cout << i + 1 << " " << d[i] << endl;
	}


	return 0;
}

C++: /*
問題: "Breadth First Search"
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp
所要時間: 30分
学んだこと: 幅優先探索の実装はキューを用いて行えばよいこと。
着席位置: 左・中央
*/

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

//constexpr int Inf = 1 << 30;

class Graph {
protected:
	vector< vector<int> > adjl;
public:
	Graph(int n) : adjl(n) {}
	void PushAdjl(int pl, int val) { adjl[pl].push_back(val); }
};

class BFGraph : public Graph {
private:
	vector<int> distance;
	queue<int> que;
public:
	BFGraph(int n) : Graph(n), distance(n, -1) {}
	void BreadthFirstSearch(int begin);
	void ShowDistanceAll();
};

void BFGraph::BreadthFirstSearch(int begin = 0) {
	que.push(begin);
	distance[begin] = 0;
	while (!que.empty()) {
		int f = que.front();
		que.pop();
		for(auto i = adjl[f].begin();i!=adjl[f].end();++i)
			if (distance[*i] == -1) {
				que.push(*i);
				distance[*i] = distance[f] + 1;
			}
	}
}

void BFGraph::ShowDistanceAll() {
	for (int i = 0; i < distance.size(); i++)
		cout << i + 1 << " " << distance[i] << endl;
}

int main() {
	int n;
	cin >> n;
	BFGraph bfg(n);
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			bfg.PushAdjl(u-1, t-1);
		}
	}
	bfg.BreadthFirstSearch();
	bfg.ShowDistanceAll();
}

C++: //广度优先搜索，队列实现 
#include <iostream>
#include <queue>

using namespace std;

const int N = 100;

const int NIL = -1;

int n;
bool graph[N+1][N+1] = {false}; //关系矩阵图 
char *color;//标记点的状态 B代表访问结束，W代表未访问

int *d; //点0到点u距离

//广度优先搜索 
void bfs(int u) {
	//初始化距离 
	for (int i = 1;i<=n;i++) {
		if (i == u) {
			d[i] = 0;
		} else {
		    d[i] = -1;
	    }
	}
	queue<int> q;
	color[u] = 'B';
	q.push(u);
	
	while (!q.empty()) {
		u = q.front();q.pop();
		for (int v = 1;v<=n;v++) {
		  if (graph[u][v] && color[v] == 'W') {
			  d[v] = d[u] + 1;
			  color[v] = 'B';
			  q.push(v);
		  }
	   }
	}
}

int main() {
	cin >> n;
	color = new char[n+1]; 
	d = new int[n+1]; 
	//初始化 
	for (int i = 1;i<=n;i++) {
		color[i] = 'W';
	}
	
	for (int i=0;i<n;i++) {
		int x,k;
		cin >> x >> k;
		for (int j = 0;j<k;j++) {
			int y;
			cin >> y;
			graph[x][y] = true; 
		}
	}
	
	bfs(1); //从1点开始搜索 
	
	for (int i = 1;i<=n;i++) {
		cout << i << " " << d[i] << endl;
	}
	delete[] color;
	delete[] d;
}

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

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

 for(int i=1; i<=n; i++){
  if(i!=1 && D[i]==0){
   D[i]=-1;
  }
  cout << i << D[i] << endl;
 }
}

int main(){
 cin >> n;
 int u, k;
 int v[100000];
 for(int i=0; i<n; i++){
  cin >> u >> k;
  for(int j=0; j<k; j++){
   cin >> v[j];
   G[i+1][v[j]] = 1;
  }
 }
 bfs(1);
}