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 <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

struct dd{
    int id,f;
    dd(){
        id=0;
        f=0;
    }
    dd(int a,int b){
        id=a;
        f=b;
    }
};
bool data[101][101]={};

void f1(dd*,int,int&,int);

int main()
{
    int N,M,num1,num2;
    dd ddata[101]={};
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>num1;
        cin>>M;
        for(int j=0;j<M;j++){
            cin>>num2;
            data[num1][num2]=true;
        }
    }
    int time=1;
    queue<dd> que;
    bool vd[101]={};
    que.push(dd(1,0));
    int val[101]={};
    vd[1]=true;
    for(dd ddata=que.front();!que.empty();que.pop(),ddata=que.front()){
        val[ddata.id]=ddata.f;
        for(int i=1;i<=N;i++){
            if(data[ddata.id][i]&&!vd[i]){
                vd[i]=true;
                que.push(dd(i,ddata.f+1));
            }
        }
    }
    for(int i=1;i<=N;i++){
        cout<<i<<' '<<val[i];
        cout<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
#include <queue>
//#include "../cxx-prettyprint/prettyprint.hpp"


int main() {
    int n;
    std::cin >> n;

    std::unordered_map<int, std::vector<int>> Adj(n);

    for (int i = 0; i < n; i ++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> V(k);
        for (int& v : V) {
            std::cin >> v;
        }
        Adj[u] = V;
    }

    std::set<int> nodes_unvisited;
    std::unordered_map<int, int> distances;
    for (int i = 1; i <= n; i ++) {
        nodes_unvisited.insert(i);
        distances[i] = -1;
    }

    nodes_unvisited.erase(1);
    distances[1] = 0;
    std::queue<int> queue;
    queue.push(1);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop();
        for (int v : Adj[u]) {
            if (nodes_unvisited.find(v) != nodes_unvisited.end()) {
                nodes_unvisited.erase(v);
                distances[v] = distances[u] + 1;
                queue.push(v);
            }
        }
    }

    for (int i = 1; i <= n; i ++) {
        std::cout << i << " " << distances[i] << std::endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
int n;
int v,k,ttm;
vector<int> graph[105];
int dis[105];//到其他点的距离
int vis[105];//纪律顶点是否被访问
void bfs(int u)
{
    queue<int> qu;
    qu.push(u);
    dis[u]=0;
    vis[u]=1;
    while(!qu.empty())
    {
        int v=qu.front();qu.pop();
        for(int i:graph[v])
        {
            if(vis[i]){continue;}
            vis[i]=1;qu.push(i);
            dis[i]=dis[v]+1;
        }
    }
}
int main()
{
    clr(vis,0);
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>v>>k;
        while(k--)
        {
            cin>>ttm;
            graph[v].push_back(ttm);//连接的点
        }
    }
    bfs(1);
     cout<<1<<" "<<dis[1]<<endl;
    for(int i=1;i<=n;++i)
    {
         cout<<i<<" "<<(dis[i]?dis[i]:-1)<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;

struct Info{
	Info(){m_node = m_distance = 0;}
	Info(int node,int distance){
		m_node = node;
		m_distance = distance;
	}
	int m_node,m_distance;
};

class InfoQueue{
public:
	InfoQueue(){
		info = new Info[10000];
		num_of_data = head = tail = 0;
	}
	bool isEmpty(){
		return num_of_data == 0;
	}
	void enqueue(Info data){
		info[tail] = data;
		tail = (tail+1)%10000;
		num_of_data++;
	}
	Info dequeue(){
		Info ret = info[head];
		head = (head+1)%10000;
		num_of_data--;
		return ret;
	}
private:
	Info* info;
	int head,tail,num_of_data;
};


int main(){

	int n,row,col,num;
	scanf("%d",&n);

	int table[n+1][n+1];
	int check_table[n+1];

	for(int i=1;i<=n;i++){
		check_table[i] = -1;
		for(int k=1;k<=n;k++)table[i][k] = 0;
	}

	for(int i=1;i<=n;i++){
		scanf("%d %d",&row,&num);
		for(int k=1;k<=num;k++){
			scanf("%d",&col);
			table[row][col]=1;
		}
	}

	check_table[1] = 0;

	InfoQueue Q;
	Q.enqueue(Info(1,0));

	Info tmp;
	while(!Q.isEmpty()){
		tmp = Q.dequeue();
		for(int i=1;i<=n;i++){
			if(table[tmp.m_node][i]==1 && check_table[i] == -1){
				check_table[i] = tmp.m_distance+1;
				Q.enqueue(Info(i,tmp.m_distance+1));
			}
		}
	}

	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,check_table[i]);
	}

    return 0;
}

C++: //ALDS1_11C
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

static const int N=100;
static const int INF=(1<<21);
int n,d[N];
vector<vector<int> > M(N,vector<int>(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;
 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<queue>
#define N 100
#define INFTY (1<<21)
using namespace std;

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

  return 0;
}