Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 10000
#define NIL -1
static const int N = 100;
static const int INFTY = 10000000;

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] = INFTY;
  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] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for(int i = 0;i<n;i++){
    cout << i+1 << " "<< ((d[i] == INFTY) ? (-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);

}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n;
  cin>>n;
  vector<vector<int> > G(n,vector<int>());
  for(int i=0;i<n;i++){
    int u,k;
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      int c;
      cin>>c;
      c--;
      G[u].push_back(c);
    }
  }
  int d[n];
  memset(d,-1,sizeof(d));
  queue<int> q;
  d[0]=0;
  q.push(0);
  while(!q.empty()){
    int v=q.front();q.pop();
    for(int u:G[v]){
      if(d[u]<0){
	d[u]=d[v]+1;	
	q.push(u);
      }
    }
  }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<d[i]<<endl;;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

int dh[4] = {1,0,-1,0};
int dw[4] = {0,1,0,-1};

int main()
{
   int n;
   cin >> n;
   vector<vector<int>>G(n);
   for(int i = 0; i < n; i++)
   {
       int k;
       cin >> k >> k;
       for(int j = 0; j < k; j++)
       {
           int v;
           cin >> v;
           v--;
           G[i].push_back(v);
       }
   }
   vector<int>dist(n,-1);
   dist[0] = 0;
   queue<int>que;
   que.push(0);
   while(!que.empty())
   {
       int v = que.front();
       que.pop();
       for(auto nv: G[v])
       {
           if(dist[nv] != -1) continue;
           dist[nv] = dist[v] + 1;
           que.push(nv);
       }
   }
   for(int i = 0; i < n; i++)
   {
       cout << i+1 << " " << dist[i] << endl;
   }
}