Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N=100;
static const int INFTY=2000000000;
int n,M[N+1][N+1];
int d[N+1];
void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=1;i<=n;i++)d[i]=INFTY;
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int v=1;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=1;i<=n;i++)
        cout<<i<<" "<<((d[i]==INFTY)?-1:d[i])<<"\n";

}

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