Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

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