Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
#include<queue>

using namespace std;

#if 0
所用時間:

感想:

#endif

int N;
int D[101] = {};
int mat[101][101] = {}; //mat[Y][X]

void solve();

int main(){
    cin >> N;

    for(int i = 0; i < N; ++i){
        int u, k;
        cin >> u >> k;
        for(int j = 0; j < k; ++j){
        int buff;
        cin >> buff;
        //cout << "Buff: " << buff << endl; 
        mat[u][buff] += 1;
        }
    }

    // for(int i = 1; i <= N; ++i){
    //     for(int j = 1; j <= N; ++j){
    //         if(j==N) cout << mat[i][j];
    //         else cout << mat[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    solve();
}

void solve(){
    queue<int> Q;

    for(int i = 1; i <= N; ++i) D[i]--;

    D[1] = 0;

    Q.push(1);

    while(!Q.empty()){
        int cur = Q.front();
        Q.pop();

        for(int dst = 1; dst <= N; ++dst){
            if((D[dst] < 0) && ( mat[cur][dst] == 1)){
                D[dst] = D[cur] + 1;
                Q.push(dst);
            }
        }
    }

    for(int i = 1; i <= N; ++i){
        cout << i << " " << D[i] << endl;
    }
}

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

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},f[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) f[i]=-1;
  f[1]=0;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
      if(g[di]==0){
	g[di]=1;
	d[u][j]=di;
      }
    }
  }

  q.push(1);

  int cnt=1,fg;
  int a;
  while(!q.empty()){
    fg=0;
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>0){
	fg=1;
	q.push(d[a][i]);
	f[d[a][i]]=cnt;
      }
    }
      q.pop();
    if(fg==1) cnt++;
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<f[i]<<endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
 
struct status{
    int idx_node;
    vector<int> children;
    int visited = 0;
    int depth = -1;
};
 
deque<int> schedule; 
vector<status> graph;

int depth = 1;
void bfs(){
    if (schedule.size()>1){
        
        int now_node = schedule.front();
        schedule.pop_front();
        
        switch(now_node){
            case 0: {
                ++depth;
                schedule.push_back(0);
                break;
            }
            default: {
                for(auto& next_node:graph[now_node-1].children){
                    if(graph[next_node-1].visited) continue;
                    
                    graph[next_node-1].visited = 1;
                    graph[next_node-1].depth = depth;  
                    schedule.push_back(next_node);
                }
            }
        }
        bfs();
    }
}
 

int main(void){cin.tie(0);ios::sync_with_stdio(false);
 
    //stdi
    int nb_nodes; cin >> nb_nodes;
    graph.resize(nb_nodes);
    for(auto& times:graph){
        int idx_node; cin >> idx_node; graph[idx_node-1].idx_node = idx_node;
        int nb_edges; cin >> nb_edges; graph[idx_node-1].children.resize(nb_edges);
        for(auto& a:graph[idx_node-1].children){
            cin >> a;
        }
        sort(graph[idx_node-1].children.begin(),graph[idx_node-1].children.end());
    }
    
    //bfs
    schedule.push_back(1);
    schedule.push_back(0);
    bfs();
    graph[1-1].depth = 0;
     
    //stdo
    for(auto& a:graph){
        cout<<a.idx_node<<" "
            <<a.depth<<"\n";
    }
}

C++: #include <stdio.h>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
 
int main(void) {
  int n, ans = 0, inf = 10000;
  scanf("%d", &n);
  int dis[n];
  rep(i, n) dis[i] = inf;
  dis[0] = 0;
  vector< int > vec[n];
  rep(i, n) {
    int u, k;
    scanf("%d%d", &u, &k);
    u--;
    rep(j, k) {
      int v;
      scanf("%d", &v);
      v--;
      vec[u].push_back(v);
    }
  }
  rep(i, n - 1) {
    rep(j, n) {
      vector< int >::iterator it;
      for(it = vec[j].begin(); it != vec[j].end(); ++it) {
        if(dis[*it] > dis[j] + 1) dis[*it] = dis[j] + 1;
      }
    }
    /*rep(j, n) printf("%d ", dis[j]);
    printf("\n");*/
  }
  rep(i, n) {
    if(dis[i] != inf) printf("%d %d\n", i + 1, dis[i]);
    else printf("%d -1\n", i + 1);
  }
  return 0;
}

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

using namespace std;

const int MAX = 100;
int adj[MAX][MAX] = {};

class BFS{
    int num, d[MAX], status[MAX];
public:
    BFS(int n) : num(n){
        for(int i=0;i<n;i++){
            d[i] = -1;
            status[i] = 0;
        }
    }

    void run(int node){
        queue<int> Q;
        Q.push(node);
        d[node] = 0;
        status[node] = 1;
        while(!Q.empty()){
            node = Q.front(); Q.pop();
            for(int i=0;i<num;i++){
                if(adj[node][i] && status[i] == 0) {
                    Q.push(i);
                    d[i] = d[node] + 1;
                    status[i] = 1;
                }
            }
        }
    }

    void print(){
        for(int i=0;i<num;i++){
            cout << i+1 << " " << d[i] << endl;
        }
    }
};

int main(){
    int r, c, n_c, n;
    cin >> n;
    BFS bfs(n);

    for(int i=0;i<n;i++){
        cin >> r >> n_c;
        for(int j=0;j<n_c;j++){
            cin >> c;
            adj[r-1][c-1]++;
        }
    }

    bfs.run(0);
    bfs.print();
}